我有以下代码,我想在同一个文件中向PHP中的变量发送一个值(例如日期2015-07-12)而不重新加载页面。所以我必须从输入中提取价值 - > ajax然后从ajax - >变量php
if($_POST['action'] == 'search'){
$date_start = $_POST['date_start'];
$date_end = $_POST['date_end'];
...
}
function ajax_post(){
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "parse_form.php";
var date_start = document.getElementById("date_start").value;
var date_end = document.getElementById("date_end").value;
var vars = "date_start="+date_start+"&date_end="+date_end;
hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
//alert(return_data);
document.getElementById("status").innerHTML = return_data;
}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send(vars); // Actually execute the request
//document.getElementById("status").innerHTML = "processing...";
}
<form method="POST" action="<?php $_SERVER['PHP_SELF'];?>" >
<input type="text" class="date start" id="date_start" name="date_start" value="" />
<input type="hidden" name="action" value="search" />
<input name="myBtn" type="button" value="Submit Data" onclick="ajax_post();">
</form>
我不知道该怎么说这个日期,例如我2015-07-12
if($_POST['action']){ ... }
答案 0 :(得分:0)
这实际上是不可能的。即使在您的浏览器从服务器获得完整响应之前,Php也会得到评估。之后,如果您希望使用新参数重新评估php,则javascript将运行ajax并将要求页面重新加载。