我有以下javascript函数
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 );
?>
问题是我无法将var runs3从javascript功能传递到php文件。我还尝试了根据以下主题(Using an ajaxRequest.open to send a variable to php)解决方案,如ajaxRequest.open(“POST”,“runs3.php?variable =”+ runs3)或AjaxRequest.open(“POST”,“runs3.php? myvar = runs3“,true);但就我而言,它不起作用。你知道我的案子有什么问题吗?谢谢你的兴趣。
答案 0 :(得分:2)
POST请求不使用参数URL !这是使用in-url params的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中获得$_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方面:(在此解释:Reading JSON POST using PHP):
$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};
不是“常规”方式,但您可以更灵活地使用您发送的数据 (因为你不发送字符串,而是原始数据:对象/数组......)
答案 1 :(得分:0)
试试这个。你的功能(ajax_runs3)
#include <algorithm>
#include <iterator>
std::partition_copy(array, array + size,
std::back_inserter(real),
std::back_inserter(imag),
[&toggle](int t){ return t != toggle; });