没有从服务器端文件获取ajax响应

时间:2016-08-13 17:01:55

标签: php ajax

你可以在下面看到我试图改变我的页面的一部分,而不是使用ajax重新加载所有页面但是我没有得到任何回复

我正在使用本地主机xampp,两个文件都在同一目录中

我也厌倦了在主机上放置文件而没有任何反应 当我将它们放在服务器上而没有数据库的情况下连接到 accdata.php 文件中的数据库时,我没有遇到任何错误

我愚弄了一个迷失方式来改变pmlting xmlhttp.open的url部分的方式

like file:/// C:/xml/dineshkani.xml

的index.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");
        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>

accdata.php

<?php

alert("Hello! php start processing");
echo "start";

$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 个答案:

答案 0 :(得分:0)

ajax函数只发送一个值而不是一个带有关联值的post变量。尝试沿着这些方向 - 稍微整理一下,但重要的一点是通过ajax发送的参数中的name=value,设置Content-Type标题通常有助于解决顽固的xhr请求。

javascript不需要在正文中 - 因此我将其移至文档的头部。

<!DOCTYPE html>
<html>
    <head>
    <meta charset="UTF-8">
        <title>Site Title</title>
        <script>
            function SendForm(){
                var xmlhttp = new XMLHttpRequest();
                xmlhttp.onreadystatechange = function() {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
                        document.getElementById("AccNum").innerHTML = xmlhttp.responseText;
                    }
                };
                xmlhttp.open( "POST", "AccData.php", true );
                xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                xmlhttp.send( 'AccNum='+document.getElementById("AccNum").value );
            }
        </script>
    </head>
    <body>
        <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>
    </body>
</html>

一个基本的ajax函数,只需在调用时更改参数即可重复使用。

function ajax(method,url,parameters,callback){
    var xhr=new XMLHttpRequest();
    xhr.onreadystatechange=function(){
        if( xhr.readyState==4 && xhr.status==200 )callback.call( this, xhr.response );
    };

    var params=[];
    for( var n in parameters )params.push( n+'='+parameters[n] );

    switch( method.toLowerCase() ){
        case 'post':
            var p=params.join('&');
        break;
        case 'get':
            url+='?'+params.join('&');
            var p=null;
        break;  
    }
    xhr.open( method.toUpperCase(), url, true );
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr.send( p );
}

function cbaccdata(r){ document.getElementById('AccNum').innerHTML=r; }


function sendForm(){
    ajax.call( this, 'post','accdata.php',{ 'AccNum':document.getElementById("AccNum").value },cbaccdata );
}
相关问题