通过Ajax使用PHP在MySQL中插入和检索数据

通过Ajax使用PHP在MySQL中插入和检索数据,php,html,mysql,ajax,forms,Php,Html,Mysql,Ajax,Forms,我有一个非常简单的表单,包含一个文本框和一个提交按钮。当用户在表单中输入一些内容,然后单击submit时,我想使用PHP和Ajax(带jQuery)将表单的结果插入MySQL数据库。该结果应以表格的形式显示在同一页上,并在每次插入后更新 有人能帮忙吗 我使用的代码不起作用: ajax.html: <html> <body> <script language="javascript" type="text/javascript"> <!-- //Brow

我有一个非常简单的表单,包含一个文本框和一个提交按钮。当用户在表单中输入一些内容,然后单击submit时,我想使用PHP和Ajax(带jQuery)将表单的结果插入MySQL数据库。该结果应以表格的形式显示在同一页上,并在每次插入后更新

有人能帮忙吗

我使用的代码不起作用:

ajax.html

<html>
<body>
<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
 var ajaxRequest;  // The variable that makes Ajax possible!

 try{
   // Opera 8.0+, Firefox, Safari
   ajaxRequest = new XMLHttpRequest();
 }catch (e){
   // Internet Explorer Browsers
   try{
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
   }catch (e) {
      try{
         ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }catch (e){
         // Something went wrong
         alert("Your browser broke!");
         return false;
      }
   }
 }
 // Create a function that will receive data 
 // sent from the server and will update
 // div section in the same page.
 ajaxRequest.onreadystatechange = function(){
   if(ajaxRequest.readyState == 4){
      var ajaxDisplay = document.getElementById('ajaxDiv');
      ajaxDisplay.value = ajaxRequest.responseText;
   }
 }
 // Now get the value from user and pass it to
 // server script.
 var name = document.getElementById('name').value;
var age = document.getElementById('age').value;
 var wpm = document.getElementById('wpm').value;
 var sex = document.getElementById('sex').value;
 var queryString = "&name=" +name+ "&age=" + age ;
 queryString +=  "&wpm=" + wpm + "&sex=" + sex;
 ajaxRequest.open("GET", "ajax-example.php" + 
                              queryString, true);
 ajaxRequest.send(null); 
}
//-->
</script>
<form name='myForm'>
Name: <input type='text' id='name' /><br/>
Max Age: <input type='text' id='age' /> <br />
Max WPM: <input type='text' id='wpm' />
<br />
Sex: <select id='sex'>
<option value="m">m</option>
<option value="f">f</option>
</select>
<input type='button' onclick='ajaxFunction()' 
                              value='Query MySQL'/>
</form>
<div id='ajaxDiv'>Your result will display here</div>
</body>
</html>

internet上有许多关于ajax与PHP和Jquery的教程。您可以通过其中的任何一个来获得所需的结果

请参见此处的示例

当您收到php脚本中的数据时,对数据库进行查询并获得结果


希望这会有所帮助

我已经使用php从数据库中接受和检索数据。数据将显示在下一页上。我希望数据显示在同一个页面上,它从数据库检索结果。如果要插入数据库然后检索,需要进行哪些更改?将“选择”查询替换为“插入”查询。看这个例子,它不起作用。从提交后,数据库未更新。实际上,按submit
code
ajaxRequest.onreadystatechange=function(){if(ajaxRequest.readyState==4){var ajaxDisplay=document.getElementById('ajaxDiv');ajaxDisplay.innerHTML=ajaxRequest.responseText;}时不会发生任何事情div没有value属性。您需要使用innerHTML属性。您的查询字符串是错误的。
code
var queryString=“?name=“+name+”&age=“+age;queryString+=”&wpm=“+wpm+”&sex=“+sex;ajaxRequest.open”(“GET”,“ajax example.php”+queryString,true);您需要将第一个“&”替换为“?”。
<?php
$dbhost = "localhost";
$dbuser = "demo";
$dbpass = "demo";
$dbname = "test_db";
    //Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
    //Select Database
mysql_select_db($test_db) or die(mysql_error());
    // Retrieve data from Query String
$age = $_GET['age'];
$sex = $_GET['sex'];
$wpm = $_GET['wpm'];
    // Escape User Input to help prevent SQL Injection
$age = mysql_real_escape_string($age);
$sex = mysql_real_escape_string($sex);
$wpm = mysql_real_escape_string($wpm);
    //build query
$query = "INSERT INTO form2 (name,age,sex,wpm) VALUES ('$name','$age','$sex','$wpm')";;


mysql_select_db('test_db');

$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}

    //Build Result String
$display_string = "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Name</th>";
$display_string .= "<th>Age</th>";
$display_string .= "<th>Sex</th>";
$display_string .= "<th>WPM</th>";
$display_string .= "</tr>";

// Insert a new row in the table for each person returned
$result1=mysql_query("SELECT * FROM form2 WHERE name='$name'"); 
while($row = mysql_fetch_array($result1))
{
    $display_string .= "<tr>";
    $display_string .= "<td>$row[name]</td>";
    $display_string .= "<td>$row[age]</td>";
    $display_string .= "<td>$row[sex]</td>";
    $display_string .= "<td>$row[wpm]</td>";
    $display_string .= "</tr>";

}

$display_string .= "</table>";
echo $display_string;
?>
$("button_id").click(function () {
    $.ajax({
        url:"where you should post the data",
        type: "POST",  
        data: the string you should post,  
        success: function (result) {
            //display your result in some DOM element
        }
    });
});