Pages

Sunday, July 10, 2011

Connect to database with XML HTTP Connection (POST)

Ok, last time I've show you how to use XML HTTP Connection with GET method. Now I'll show you the other method, using POST method. Ok, first, using the same HTML file, but we need to edit the Javascript.. Here is the HTML file for post method
<div id="test2" onClick="getIt('test2')">
2
</div>
<div id="test1" onClick="getIt('test1')">
1
</div>
Using the same database like before, and the same PHP file, but we need to change some code on it. This is for the PHP file.
<?php
$username="mysqlusername";
$password="mysqlpassword";
$database="xml";
$field1=$_POST['field1'];

$con= mysql_connect("localhost","root","");
mysql_select_db($database) or die("unable to select the database..");
if($con)
{
echo "Result of".$field1." \n";
$query="select * from dummydata where id=$field1";
$res=mysql_query($query);
$done=mysql_fetch_array($res);
  if($done['content']!=null)
    {
    echo $done['content'];
    }
  else
    {
    echo "error";
    }
}
else
{
echo "Invalid login information!";
}

mysql_close($con);
?>
Ok, that's all, test it and you'll get the same result like before ;)

No comments:

Post a Comment