themistral Posted April 29, 2010 Share Posted April 29, 2010 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! Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted April 29, 2010 Share Posted April 29, 2010 Unless you defined the function, it's mysql_query not mysqlquery. Try Google or php.net. =] Quote Link to comment Share on other sites More sharing options...
themistral Posted April 29, 2010 Author Share Posted April 29, 2010 Thanks Ken2k7 - that was just a misprint! I've scrapped that code and have found one that seems to do what I want it to do now I've messed around with it! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.