在 php 上使用 ajax 返回值

Use ajax return value on php

本文关键字:ajax 返回值 php      更新时间:2023-09-26

我正在尝试将ajax响应值传递给php函数。

阿贾克斯.js

 xmlhttp.onreadystatechange=function(){
  if (xmlhttp.readyState==4 && xmlhttp.status==200){
    var a = xmlhttp.responseText;
    }
  }

我需要a值用于PHP函数。

测试.php

function testFun($a); 

这可能吗???感谢您的帮助!!

Try This:
/** JAVA SCRIPT **/
// create a request query
var queryString = "?send_param=" + a;
// send request query to php file
xmlhttp.open("GET", "test.php" + queryString, true);
xmlhttp.send(null);
/** JAVA SCRIPT END **/
/** PHP SCRIPT **/
[ test.php ]
<?php
// if $_REQUEST array is empty show error and die;
if (empty($_REQUEST)) {
die("Error: No request found");
} else {
// split the $_REQUEST array and make array key as php variable
extract($_REQUEST);
}
/**
* Function testFun
* @param string $a
*/
function testFun($a)
{
// return val
}
// Call the function
testFun($send_param);
?>
/** PHP SCRIPT END **/
I preferred to use $.ajax function instead to Java Script Ajax, Coz it's handy :) and to good.

[1]: http://php.net/manual/en/function.extract.php

编写另一个 AJAX 请求并将a传递到包含该函数的 PHP 文件中,并将输入读取为 GET 或 POST,然后根据需要使用输出。使用像jQuery这样的JS框架将有助于缩短AJAX调用。