2016-08-13 191 views
0

我试图改变使用AJAX我的PHP网页如下 内容index.php页面已输入申请的是调用函数按钮上点击执行,但我的问题是,页面重新加载是它使用ajax更新php页面使用post请求重新加载页面?

所以我想知道我在做什么错?

请注意,我现在用的是POST请求,以保持我的数据安全,因为w3schools.com推荐

inexd.php下面

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Site Title</title> 

</head> 

<body align="left"> 

<div> 
    <h4 align="left">Balance Enquiry</h4> 
</div> 

<form> 
    <div> 
     <label>Account Number </label> 
     <input id="AccNum" type="text" name="AccNumInput"> 
     <button type="button" onclick="SendForm()">Search</button> 
     </div> 
</form> 

<script> 
function SendForm() 
{ 
    alert("Hello! SendForm start"); 
     var xmlhttp = new XMLHttpRequest(); 
     xmlhttp.onreadystatechange = function() 
    { 
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
      { 

       document.getElementById("AccNum").innerHTML = xmlhttp.responseText; 
      } 
    }; 
     alert("Hello! going to send ajax"); 
     var x = xmlhttp.open("POST","AccData.php", true); 
     xmlhttp.send(document.getElementById("AccNum").value); // you want to pass the Value so u need the .value at the end!!! 

     alert(document.getElementById("AccNum").value); 
     alert("Hello! SendForm end"); 
} 
</script> 

</body> 

</html> 

data.php文件代码文件代码

<?php 

alert("Hello! php start processing"); 

$AccountNumber = $_POST['AccNum']; 

$conn = oci_connect('admin', 'admin', 'localhost/JDT', 'AL32UTF8'); 
if (!$conn) { 
    $e = oci_error(); 
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR); 
} 

alert("Hello! connected to oracle"); 

$sqlstr = 'SELECT CUSTOMER_ID,CUST_NAME,PHONE1 FROM customers where CUSTOMER_ID=:AccNum'; 

$stid = oci_parse($conn, $sqlstr); // creates the statement 

oci_bind_by_name($stid, ':AccNum', $AccountNumber); // binds the parameter 

oci_execute($stid); // executes the query 

echo $AccountNumber; 
/** 
* THIS WHILE LOOP CREATES ALL OF YOUR HTML (its no good solution to echo data out like this) 
*/ 
while ($row = oci_fetch_array($stid, OCI_ASSOC + OCI_RETURN_NULLS)) { 
    echo "<tr>"; 
    foreach ($row as $item) { 
     echo "<td align=center>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "&nbsp;") . "</td>"; 
    } 
    echo "</tr>\n"; 
} 
echo "</table>\n"; 

oci_free_statement($stid); // releases the statement 
oci_close($conn); // closes the conneciton 

?> 

回答

1

随着<input type="submit" value="Search">您的形式发送“旧”与Ajax无关的服务器方式!

<form> 
    <div> 
     <label>Account Number </label> 
     <input id="AccNum" type="text" name="AccNuminput"> 
     <button type="button" onclick="sendForm()">Search</button> 
     </div> 
</form> 
<script> 
function sendForm(){ 
    var xmlhttp = new XMLHttpRequest(); 
    xmlhttp.onreadystatechange = function() { 
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
       //Execudted when finished and everything its Okay 
       document.getElementById("AccNum").innerHTML = xmlhttp.responseText; 
      } 
     }; 
     xmlhttp.open("POST", "acc_data.php", true); 
     xmlhttp.send("accNum="+document.getElementById("AccNum").value); // you want to pass the Value so u need the .value at the end!!! 
    } 


</script> 

然后在你的data.php你不需要任何HTML你只需要处理您通过ajax的POST请求(也没有必要为会议)接收到的数据。在xmlhttp.responseText中,您在请求完成时从服务器接收到您的答案。

<?php 

$accountNumber = $_POST['accNum'];// set a good variable name 
$conn = oci_connect('admin', 'admin', 'localhost/JDT', 'AL32UTF8'); //setup connection 
if (!$conn) { 
    $e = oci_error(); 
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR); // throws an error on connection error 
} 
$sqlstr = 'SELECT CUSTOMER_ID,CUST_NAME,PHONE1 FROM customers where CUSTOMER_ID=:ACCNUM'; // sql stirng 
$stid = oci_parse($conn, $sqlstr); // creates the statement 
oci_bind_by_name($stid, ':ACCNUM', $accountNumber); // binds the parameter 
oci_execute($stid); // executes the query 


/** 
* THIS WHILE LOOP CREATES ALL OF YOUR HTML (its no good solution to echo data out like this) 
*/ 
while ($row = oci_fetch_array($stid, OCI_ASSOC + OCI_RETURN_NULLS)) { 
    echo "<tr>"; 
    foreach ($row as $item) { 
     echo "<td align=center>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "&nbsp;") . "</td>"; 
    } 
    echo "</tr>\n"; 
} 
echo "</table>\n"; 

oci_free_statement($stid); // releases the statement 
oci_close($conn); // closes the conneciton 

?> 
+0

非常感谢您的善意帮助..这解释了很多人认为我不清楚...请你介意教我什么更好地使用,而不是循环创建HTML? – samer

+0

欢迎 我认为最好的解决方案是创建一个JSON对象并将json对象返回到html页面,然后用javascript做其余的事情...但是一步一步地......不要试图学习所有的技术同时这真的很难。 –

+0

好吧然后生病就像你说的一步一步走..最后认为数据没有从服务器回来的url更改为http:// localhost/test /?AccNumInput = 1000,但结果不会加载和在xmlhttp.send (“accNum =”+ document.getElementById(“AccNum”)value) 应该是xmlhttp.send(document.getElementById(“AccNum”).value),因为该值将传递给字符串,该字符串不应该在sql语句? 'xmlhttp.open(“POST”,“acc_data.php”,true)中的 – samer

相关问题