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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.