i have 2 fields in form , onsubmit() event calling function. when click udpate button not values of "sent", other values fetched correctly.
in case of insert button, sent fetched correctly.
i tracking values using aler button. can done using firebug too?
<html> <head> <meta content="text/html;charset=utf-8" http-equiv="content-type" /> <meta content="utf-8" http-equiv="encoding" /> <script type="text/javascript"> function showuser(form, e) { e.preventdefault(); e.returnvalue=false; var xmlhttp; var submit = form.getelementsbyclassname('submit')[0]; var sent = document.getelementsbyname('sent')[0].value || ''; var id = document.getelementsbyname('id')[0].value || ''; **alert(id); alert(sent); alert(submit.name); alert(submit.value);** if (window.xmlhttprequest) { // code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else { // code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange = function(e) { if (xmlhttp.readystate == 4 && xmlhttp.status == 200){ document.getelementbyid("txthint").innerhtml = xmlhttp.responsetext; } } xmlhttp.open(form.method, form.action, true); xmlhttp.send('sent=' + sent + '&id=' + id + '&' + submit.name + '=' + submit.value); } </script> </head> <body> <form action="ajax_test.php" method="post" onsubmit="showuser(this, event)"> <label>enter sentence: <input type="text" name="sent"></label><br /> <input type="submit" class="submit" name="insert" value="submit" /> </form> <h4>update</h4> <form action="ajax_test.php" method="post" onsubmit="showuser(this, event)"> <pre> <label>enter id:</label><input type="text" name="id"><br> <label>enter sentence:<input type="text" name="sent"></label><br /> </pre> <input type="submit" class="submit" value="submit" name="update"/> </form> <br /> <div id="txthint"> <b>person info listed here.</b> </div> </body> </html>
you're getting first sent
, id
elements because you're using document.getelementsbyname
, need grab ones within form submitted. getting first, values insert form when update form submitted.
var sent = form.elements['sent'].value; var id = form.elements['id'].value;
the above sent
, id
values form submitted, based on form object passed argument.
Comments
Post a Comment