http://www.youtube.com/watch?v=i02wg...eature=related
index.html
Código HTML:
<!DOCTYPE html> <html land="en"> <head> <!--escript ---> <script type="text/javascript" src="/javascript/jquery.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#comentArea").load("pull.php"); $("#addComent").click(function(){ var user_sql = $("#user_sql").val(); var coms_sql = $("#coms_sql").val(); $.post( 'data.php', { user_sql:user_sql coms_sql:coms_sql }, function(response) { $("#comentArea").load("pull.php"); $("#user_sql").val(""); $("#coms_sql").val(""); } ) return false; }); }); </script> <head> <body> <!-- form--> <form id="comentForm" method="post"> <input type="text" name="user_sql" id="user_sql" value="" /> <textarea name="coms_sql" id="coms_sql" rows="10" cols="40"></textarea> <input type="submit" id="addComent" value="enviar" /> </form> <hr> <!-- coment area--> <div id="comentArea"> </div> <body> <html>
pull.php
Código PHP:
<?
//connect
$link=mysql_connect("localhost","root","")or die(mysql_error);
mysql_select_db("coms",$link)or die(mysql_error);
//result
$query=mysql_query(" SELECT * FROM comentarios ")or die(mysql_error);
while($row=mysql_fetch_assoc($query)){
?>
<div id="singleComent"><b><? echo $row['user_sql']; ?></b><br><? echo $row['coms_sql']; ?></div>
<?
}
?>
Código PHP:
<?
//connect
$link=mysql_connect("localhost","root","")or die(mysql_error);
mysql_select_db("coms",$link)or die(mysql_error);
mysql_query(" INSERT INTO comentarios SET user_sql='".$_POST['user_sql']."',coms_sql='".$_POST['coms_sql']."' ")or die(mysql_error);
?>
Código PHP:
<?
/**
* This class helps creating simple AJAX application
*
* @author Rochak Chauhan
*/
class Ajax{
/**
* Constructor function
*/
function Ajax($htmlObjectId) {
print '<script src="ajax.js" type="text/javascript"></script>';
print "<script type=\"text/javascript\" language=\"JavaScript\"> createRequestObject('$htmlObjectId');</script>";
}
/**
* This fucntion calls the remote file with certain parameters
*
* @param string $remoteFileName (filename with extension)
* @param [array $parameterNames] (names of all the parameters to be sent)
* @param [array $parameterValues] (corresponding values of the parameters)
*
* @return void
*/
function sendRequest($remoteFileName, $parameterNames = array(), $parameterValues = array()) {
if (count($parameterNames) == count($parameterValues)) {
$parameterNames = implode(',', $parameterNames);
$parameterValues = implode(',', $parameterValues);
print "<script type=\"text/javascript\" language=\"JavaScript\"> sendReq('$remoteFileName', '$parameterNames', '$parameterValues');</script>";
}
else{
die("'Parameter Names' and 'Parameters Values' do NOT match.");
}
}
/**
* This function returns the response as simple string/xml
* @package [$asXml] boolean
*
* @return string
*/
function getResponse($asXml = false) {
if ($asXml) {
return "<script type=\"text/javascript\" language=\"JavaScript\"> document.write(responseXml);</script>";
}
else {
return "<script type=\"text/javascript\" language=\"JavaScript\"> alert(responseText);document.write(responseText);</script>";
}
}
}
?>