2012-01-10 2 views
2

텍스트 상자와 제출 버튼이 포함 된 매우 간단한 폼이 있습니다. 사용자가 폼에 무엇인가를 입력 한 다음 제출을 클릭하면 PHP와 Ajax (jQuery와 함께)를 사용하여 폼의 결과를 MySQL 데이터베이스에 삽입하고 싶습니다. 이 결과는 삽입 될 때마다 업데이트되는 테이블 형태로 동일한 페이지에 표시되어야합니다.Ajax를 통해 PHP를 사용하여 MySQL에 데이터 삽입 및 검색

아무도 도와 줄 수 있습니까?

I가 작동하지 않는 것을 사용하고 코드 :

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> 

ajax-example.php :

<?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; 
?> 
+0

[당신이 시도 ?] (http://mattgemmell.com/2008/12/08/what-have-you-tried/) – dezso

+0

나는 php를 사용하여 db로부터 데이터를 받고 받아 들였다. 데이터는 다음 페이지에 표시됩니다. 나는 같은 페이지에 데이터를 표시하고자했다. – prit2192

답변

0

PHP와 jQuery로 Ajax 용 인터넷에서 사용할 수 많은 자습서가 있습니다 . 이 중 하나를 통해 원하는 결과를 얻을 수 있습니다.

데이터베이스에 PHP 스크립트의 메이크업 쿼리에서 데이터를 수신 할 때 여기에 예를 http://www.tutorialspoint.com/ajax/ajax_database.htm

+0

그것은 db로부터 결과를 가져온다. db에 삽입 한 다음 검색하려면 어떤 변경을해야합니까? – prit2192

+0

'선택'쿼리를 '삽입'쿼리로 바꿉니다. 이 예제를 참조하십시오. http://woork.blogspot.com/2007/10/insert-record-into-database-using-ajax.html –

+0

이 작동하지 않습니다. 에서 데이터베이스를 업데이트 한 후 데이터베이스가 업데이트되지 않습니다. 아무 일도 일어나지 않을 때 submit – prit2192

1
$("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 
     } 
    }); 
}); 

를 참조하십시오이 도움이 될

희망 당신의 결과를 얻을

관련 문제