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

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

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

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

inexd.php下面

Site Title
Balance Enquiry

Account Number

Search

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");

}

下data.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 "

";

foreach ($row as $item) {

echo "

" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . "";

}

echo "

\n";

}

echo "\n";

oci_free_statement($stid); // releases the statement

oci_close($conn); // closes the conneciton

?>

2016-08-13

samer

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个使用原生 AJAX 发送 POST 请求的完整案例: HTML 代码: ``` <!DOCTYPE html> <html> <head> <title>使用原生 AJAX 发送 POST 请求</title> <meta charset="UTF-8"> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script> </head> <body> <h1>使用原生 AJAX 发送 POST 请求</h1> <form> <label for="name">姓名:</label> <input type="text" id="name" name="name"><br><br> <label for="email">邮箱:</label> <input type="email" id="email" name="email"><br><br> <button type="button" id="submit">提交</button> </form> <div id="result"></div> <script src="main.js"></script> </body> </html> ``` JavaScript 代码: ``` document.getElementById("submit").addEventListener("click", function() { var name = document.getElementById("name").value; var email = document.getElementById("email").value; var xhr = new XMLHttpRequest(); xhr.open("POST", "submit.php", true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function() { if (this.readyState === XMLHttpRequest.DONE && this.status === 200) { document.getElementById("result").innerHTML = this.responseText; } }; xhr.send("name=" + name + "&email=" + email); }); ``` PHP 代码(submit.php): ``` <?php $name = $_POST["name"]; $email = $_POST["email"]; echo "姓名:" . $name . "<br>邮箱:" . $email; ?> ``` 注意:这个案例使用了 jQuery 库,但是只是为了方便引入 Ajax 库。实际上,这个案例并不需要 jQuery。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值