Jump to content

Ajax Post.....


Go to solution Solved by 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.

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

  • Solution

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

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.