dphw5101 2016-08-13 10:55
浏览 50

使用ajax更新php页面使用post请求重新加载页面?

I am trying to change the content of my php web page using ajax as below the index.php page has input filed that call a function to executed on the button click but my problem is that the page is reload it

so i want to know what I am doing wrong??

Note that i am using the post requests to keep my data secure as w3schools.com recommended

inexd.php file code below

<!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>

The data.php file code below

<?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>
";
}
echo "</table>
";

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

?>
  • 写回答

1条回答 默认 最新

  • douyin9987 2016-08-13 13:31
    关注

    With the <input type="submit" value="Search"> your sending the form the "old" way to the server not with 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>
    

    Then in your data.php you do not need any html you just need to process the the data that you received by the ajax post request(Session is also not needed for that) . In the xmlhttp.responseText you are receiving your answer from the server when the request is finished.

    <?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>
    ";
    }
    echo "</table>
    ";
    
    oci_free_statement($stid); // releases the statement
    oci_close($conn); // closes the conneciton
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题