通过Ajaxrequest.open从Javascript到PHP文件获取变量
我有以下javascript函数通过Ajaxrequest.open从Javascript到PHP文件获取变量,javascript,php,ajax,ajax-request,Javascript,Php,Ajax,Ajax Request,我有以下javascript函数 function ajax_runs3(value){ var ajaxRequest; // The variable that makes Ajax possible! try{ ajaxRequest = new XMLHttpRequest(); } catch (e){ // Internet Explorer Browsers try{ ajaxRequ
function ajax_runs3(value){
var ajaxRequest; // The variable that makes Ajax possible!
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.myForm.time.value = ajaxRequest.responseText;
}
}
var runs3= value;
ajaxRequest.open("POST","runs3.php"+ runs3, true);
ajaxRequest.send(null);
}
还有PHP文件
<?php
$servername = "localhost";
$username = "USER";
$password = "PASS";
$dbname = "labi8575_inventory";
$conn = mysql_connect($servername, $username, $password);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('labi8575_inventory');
$runs3 = $_POST["runs3"];
$sql = mysql_query("UPDATE demo SET runs3 = '$runs3'");
$retval = mysqli_query( $sql, $conn );
?>
POST请求不使用参数的URL!它是在url参数中使用的GET方法
解决方案:
var runs3= value;
ajaxRequest.open("POST","runs3.php", true); //We open the url
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); //IMPORTANT!! We add this header to tell to PHP that it is a "form" which sent requesdt
ajaxRequest.send("value=" + encodeURIComponent(runs3)); //Then we send DATA HERE (encodeURIComponent encodes data to prevents URL-specific characters (for example '&'))
然后在PHP中得到$\u POST[“value”]
这是“常规”方式
但如果您想要更灵活的请求格式,还可以将数据作为JSON发送:
var runs3 = {"val" : value};
ajaxRequest.open("POST","runs3.php", true); //We open the url
ajaxRequest.setRequestHeader("Content-type", "application/json");
ajaxRequest.send(JSON.stringify(runs3));
PHP方面:(解释如下:):
不是“常规”方式,但您在发送数据时具有更好的灵活性
(因为您不发送字符串,而是发送原始数据:对象/数组…试试这个。您的函数(ajax\u runs3)
请开始使用mysqli*
或PDO
,因为mysql.*
在PHP7中已被弃用和删除。您正在混合使用mysqli和mysql
$request = file_get_contents('php://input'); //raw request data
$object = json_decode($request, true); //we convert it to associative array, by JSON
print_r($object); //Should return Array[1] { "val" => YOUR_VALUE};
function ajax_runs3(value){
var ajaxRequest; // The variable that makes Ajax possible!
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.myForm.time.value = ajaxRequest.responseText;
}
}
var runs3= value;
//ajaxRequest.open("POST","runs3.php"+ runs3, true);
// ajaxRequest.send(null);
var url = "runs3.php";
var params = "runs3="+value;
ajaxRequest.open("POST", url, true);
//Send the proper header information along with the request
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.setRequestHeader("Content-length", params.length);
ajaxRequest.setRequestHeader("Connection", "close");
ajaxRequest.onreadystatechange = function() {//Call a function when the state changes.
if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
alert(ajaxRequest.responseText);
}
}
ajaxRequest.send(params);
}