arunpatal Posted November 22, 2013 Share Posted November 22, 2013 Hi, Sending value via post method works just fine till i typed & in text feild. It just stop there and dose not show echo anything after & and & Here is the test.php page <html> <head> <script language="JavaScript" type="text/javascript"> function ajax_post(){ // Create our XMLHttpRequest object var hr = new XMLHttpRequest(); // Create some variables we need to send to our PHP file var url = "return.php"; var fn = document.getElementById("first_name").value; var ln = document.getElementById("last_name").value; var vars = "firstname="+fn+"&lastname="+ln; hr.open("POST", url, true); // Set content type header information for sending url encoded variables in the request hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // Access the onreadystatechange event for the XMLHttpRequest object hr.onreadystatechange = function() { if(hr.readyState == 4 && hr.status == 200) { var return_data = hr.responseText; document.getElementById("status").innerHTML = return_data; } } // Send the data to PHP now... and wait for response to update the status div hr.send(vars); // Actually execute the request document.getElementById("status").innerHTML = "processing..."; } </script> </head> <body> <table width="100%"> <td width="50%" valign="top"> <input id="first_name" name="first_name" type="text" /> <input id="last_name" name="last_name" type="text" /><br> <input name="myBtn" type="submit" value="Submit Data" onClick="javascript:ajax_post();"> </td> <td width="50%" valign="top"> <div id="status"></div> </td> </table> </body> </html> And here is the page from which ajax will bring the msg.... <?php if (isset($_POST['firstname'])) { echo 'Thank you '. $_POST['firstname'] . ' ' . $_POST['lastname'] . ', says the PHP file'; } ?> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 22, 2013 Share Posted November 22, 2013 (edited) Try passing fn and ln to encodeURIComponent() . var vars = "firstname="+encodeURIComponent(fn)+"&lastname="+encodeURIComponent(ln); The vars is constructing a url and the & has a spacial meaning. Its the argument separator for the query strings. encodeURIComponent will encode & so it will be handled safely. Edited November 22, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Solution arunpatal Posted November 22, 2013 Author Solution Share Posted November 22, 2013 Try passing fn and ln to encodeURIComponent() . var vars = "firstname="+encodeURIComponent(fn)+"&lastname="+encodeURIComponent(ln); The vars is constructing a url and the & has a spacial meaning. Its the argument separator for the query strings. encodeURIComponent will encode & so it will be handled safely. Working with the help of encodeURIComponent() and yours Thanks alot 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.