Jump to content

Ajax function help


themistral

Recommended Posts

Hi guys,

 

I've found an ajax script that seemed to work OK.

However I tried to add multiple forms each with their own id's and ran into problems due to the function naming the form - I have named the forms dynamically.

 

The reason I need this is I have a gallery of items, each which can be added as favourites.

 

This is the php/html

<?php
$query = mysqlquery("SELECT field1, field2 FROM table;");
while ($result = mysql_fetch_array($query)) {
?>
<form method="POST" name="myform<?=$result['field1'];?>" id="myform<?=$result['field1'];?>">
<input type="text" name="message" value="<?=$result['field2'];?>"> 
<input value="Post!" type="button" onclick="getMessageResponse(document.myform<?=$result['field1'];?>.message.value);">
</form>
<?php } ?>

<div id="response" name="response">Response will be placed here on submit.</div>

 

and this is the javascript function

 

<script type="text/javascript">

function getMessageResponse(str)
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
      document.getElementById('response').innerHTML=xmlHttp.responseText;
      document.myform.message.value = '';
      }
    }
   var url="new.php";
   url=url+"?message="+str;
   url=url+"&sid="+Math.random();
     xmlHttp.open("GET",url,true);
     xmlHttp.send(null);
  }
</script>

 

I am happy to scrap this code and use something else if it simply won't do the job, but my skills with Javascript are pretty much non-existant!

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/200166-ajax-function-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.