Jump to content

Ajax Post.....


arunpatal

Recommended Posts

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'; }
?>
Link to comment
https://forums.phpfreaks.com/topic/284173-ajax-post/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/284173-ajax-post/#findComment-1459565
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/284173-ajax-post/#findComment-1459574
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.