PDA

View Full Version : Sending and receiving information with AJAX and php



auriaks
03-07-2010, 10:04 AM
Hi,

I am learning ti use AJAX... I have read tutorial in w3 but I can't put all info in one working script. What I want to do is simply to send two php variables to my script.php page, where they will be added to mysql database, and then I want to receive the Updated information to index.php page.

my csript.php


<?php
if(isset($_POST['add1'])) {
include $_SERVER['DOCUMENT_ROOT'] . '/db_conn.php'; //db
$nick = $_POST['nick'];
echo "$nick";
$nick = mysql_real_escape_string($nick);
mysql_query("UPDATE `testavimas` SET nikas = '$nick' WHERE id='1'");
$msg1 = 'Atnaujinta';
}
if(isset($_POST['add2'])) {
include $_SERVER['DOCUMENT_ROOT'] . '/db_conn.php'; //db
$tekstas = $_POST['tekstas'];
$tekstas = mysql_real_escape_string($tekstas);
mysql_query("UPDATE `testavimas` SET tekstas = '$tekstas' WHERE id='1'");
$msg2 = 'Atnaujinta';
}
?>


Index.php


<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="lt">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>jQuerry Testing</title>
<meta http-equiv="Content-Language" content="lt"/>
<script type="text/javascript" src="jquery.js"></script>

<style type="text/css">
a.test { font-weight: bold; }
</style>
</head>
<body>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function loadXMLDoc(url)
{
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
xmlhttp.open("GET",url,false);
xmlhttp.send(null);
document.getElementById('test').innerHTML=xmlhttp.responseText;
}
</script>
<div id="test">

</div>

<form action='script.php' method='POST'>
<table width='600px' align='center'>
<tr align='center'>
<td align='center'>
<tr>
<td align="right"><font size="1" color='blue' face="Verdana"><b>Nikas &nbsp;&nbsp;</b></font></td>
<td><input MAXLENGTH=20 type="text" size='27' name="nick"><br/><button type="button" onclick="loadXMLDoc('script.php')">add1</button><?php echo "$msg1";?></td>
</tr>
<tr>
<td align="right"><font size="1" color='blue' face="Verdana"><b>Tekstas &nbsp;&nbsp;</b></font></td>
<td><input MAXLENGTH=20 type="text" size='27' name="tekstas"><br/><button type="button" onclick="loadXMLDoc('script.php')">add2</button><?php echo "$msg2";?></td>
</tr>
</tr>
</td>
</table>
</form>
</body>
</html>

I know that jQuery script is wrong... Maybe I need to follow some basic rules in this? I know that i have index.php with form, I send info from form to script.php, it checks info and adds to mysql, and echo's new information to my index.php back :)

Share your news with me. Thanks a lot.

auriaks
03-07-2010, 06:46 PM
or maybe something more simple?