Software Archive
Read-only legacy content

Post method do not send data to php page

Malak_A_
Beginner
32,820 Views

 

I used post method to send data (username and password ) from xdk app to php file by XMLHttpRequest object,but i faced a problem. all data i sent by post method undefined in php file.I don't know where the error.

this xdk code

<!DOCTYPE html>
<html lang="en-US">
 
 <head>
  
 <meta charset="UTF-8">
     
 <link rel="stylesheet" href="css/style.css" media="screen" type="text/css" />
 
	 <script type="text/javascript">
		 
		 function createXHR(){
			 if(typeof XMLHttpRequest !="undefined")
				 return new XMLHttpRequest();
			 else
				 return new ActiveXObject("Microsoft.XMLHttp");
			 
		 }
		 function sendRequest(){
			 var pname=document.getElementById("txtName").value;
			  var pGender=document.getElementById("rdoGender").value;
			  var pMail=document.getElementById("txtMail").value;
			  var pCity=document.getElementById("txtCity").value;
			  var pBDate=document.getElementById("txtBDate").value;
			 var str ="txtName="+pname+"&txtgender="+pGender+"&txtemail="+pMail+"&txtcity="+pCity+"&txtdate="+pBDate;
			// alert(str);
			  
			 var oXmlHttp= createXHR();
	oXmlHttp.open("post","http://localhost/hospital/post_data.php","true");
			oXmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			 oXmlHttp.onreadystatechange=Function(){
				 if(oXmlHttp.readyState==4){
					 if(oXmlHttp.status==200 || oXmlHttp.status == 304)
						 divStatus.innerHTML=+ oXmlHttp.responseText;
					 else 
						 divStatus.innerHTML=+ oXmlHttp.statusText; 
				 }//end if
			 }; //end ready state
 	 oXmlHttp.send(str);
			 
		 }
	 </script>
  </head>

  <body>
 <form  method="post" action="http://localhost/hospital/post_data.php"  onsubmit="sendRequest(); return false;" >
	  
	  <p>enter patient information</p>
	  
	 <p>NAME
	 <input type="text" id="txtName"  />
	 </p>
	  <p>Male
		  
	 <input type="radio" value="M" name="rdoGender"  id="rdoGender"  />
		  <p>Female
		  
	 <input type="radio" value="F" name="rdoGender" id="rdoGender"/>
	 </p>
	  <p>EMAIL
	 <input type="email" id="txtMail"/>
	 </p>
	  <p>city
	 <input type="text" id="txtCity"/>
	 </p>
	  <p>date
	 <input type="date" id="txtBDate"/>
	 </p>
	 <input type="submit"     value="save"/>
	 
	 
	  </form>
  <div id="divStatus"></div>
 
</body>

</html>

 

this is php code

<?php
header("Content_Type: text/plain");

        $link = mysqli_connect("localhost", "root", "", "hospital");

        /* check connection */
        if (mysqli_connect_errno()) {
            echo mysqli_connect_error();
            header("HTTP/1.0 500 Internal Server Error");
            exit();
        }
if(isset($_POST["txtName"]))
{
	$pname=$_POST["txtName"];
	$pgender= $_POST["txtgender"];
	$pemail= $_POST["txtemail"];
	$pdate= $_POST["txtdate"];
	$pcity= $_POST["txtcity"];
}
$insert="insert into 'hospital'.'patient' (`PName`, `Gender`, `email`, `BDate`, `city`) VALUES ($pname,$pgender,$pemail,$pdate,$pcity)";
 
  if ($result = mysqli_query($link, $query)) 
	  echo "Patient data added successfully ";
  else 
	  echo "Patient  data not added successfully ";
    mysqli_close($link);
   
?> 

 

0 Kudos
1 Solution
Diego_Calp
Valued Contributor I
32,820 Views

Hi Malak,

There are two things I can say about your code.

1. Seems to me that you are duplicating the post to the server.

Just the form action call to php is enough to post the values. There is no need for the code you call on the onsubmit event.

 

2. The values are undefined because php uses the name field to identify the post value. Try replacing 

  <input type="text" id="txtName"  />

with

  <input type="text" name="txtName"  />

More info about this here:

http://stackoverflow.com/questions/5440823/php-form-sending-value-from-an-id-instead-of-value

Regards,

Diego

View solution in original post

0 Kudos
4 Replies
PaulF_IntelCorp
Employee
32,820 Views

There is no local server that will process your php scripts. There is no server in your mobile device. An XDK app is a Cordova (aka PhoneGap) app and is, therefore, strictly a client app. Search for articles that explain "how to post data to a php server in cordova phonegap app" ; for example > http://stackoverflow.com/questions/22369737/posting-data-from-a-phonegap-app-to-a-server < is such an article.

0 Kudos
Diego_Calp
Valued Contributor I
32,821 Views

Hi Malak,

There are two things I can say about your code.

1. Seems to me that you are duplicating the post to the server.

Just the form action call to php is enough to post the values. There is no need for the code you call on the onsubmit event.

 

2. The values are undefined because php uses the name field to identify the post value. Try replacing 

  <input type="text" id="txtName"  />

with

  <input type="text" name="txtName"  />

More info about this here:

http://stackoverflow.com/questions/5440823/php-form-sending-value-from-an-id-instead-of-value

Regards,

Diego

0 Kudos
Malak_A_
Beginner
32,820 Views

Thank u Mr.Diego 

I tried your suggestion and it worked with me :)

but there is another way( using XMLHttpRequest) to send data to php file by call javascript code in onsubmit event in form tag or by onclick event in button, i need this way because php file will return json format and i need to display these data.

 

can u help me about this way >

Regards>

Malak

0 Kudos
Diego_Calp
Valued Contributor I
32,820 Views

Hi Malak,

You could use jquery ajax to replace your XMLHttpRequest code. Is very simple,  take a look to the accepted answer in this post:

http://stackoverflow.com/questions/2269307/using-jquery-ajax-to-call-a-php-function

Regards,

Diego

 

0 Kudos
Reply