Board index   FAQ   Search  
Register  Login
Board index php forum :: HTML JavaScript

error in send with post method

Javascript coding ..

Moderators: macek, egami, gesf

error in send with post method

Postby majid36 » Mon Jul 09, 2012 6:25 pm

hello
i want send a javascript variable to the php page and i write below code but there is a problem.
i want send x variable in below code with send function.please help me.
Code: Select all
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("POST","ajax-post-form.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var x=25;
xmlhttp.send(x);
}
</script>
</head>
<body>

<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">Request data</button>
<div id="myDiv"></div>
 
</body>
</html>


and php code:
Code: Select all
<?php
$site = $_POST['x'];

echo "value: $site <br />";

?>
 
majid36
New php-forum User
New php-forum User
 
Posts: 2
Joined: Mon Jul 09, 2012 6:14 pm

Re: error in send with post method

Postby msmohan » Tue Jul 10, 2012 1:30 am

Hi,
In your code there is a small mistake. When you sending the parameters should have the name with values,

your code

Code: Select all
var x=25;
xmlhttp.send(x);


proper code

Code: Select all
var parameter = "x=25";
xmlhttp.send(parameter);


you have to mention the x variable in string and pass the whole set then it will work.
msmohan
New php-forum User
New php-forum User
 
Posts: 36
Joined: Sun Jun 05, 2011 9:03 pm

Re: error in send with post method

Postby majid36 » Tue Jul 10, 2012 8:00 am

yes you are right.Thanks dear msmohan.
majid36
New php-forum User
New php-forum User
 
Posts: 2
Joined: Mon Jul 09, 2012 6:14 pm


Return to JavaScript

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.