我试图在不传递表单操作的情况下获取textbox的值(因为我既不能重定向到另一个页面,也不能刷新当前的页面)。
index.php
入头
<script type="text/javascript">
function send() {
$.ajax({
type:"POST",
url: "script.php",
data: {
fname: document.getElementById("fname").value,
lname: document.getElementById("lname").value
},
success: function callScriptAndReturnAlert() {
var sdata = new XMLHttpRequest();
sdata.onload = function() {
alert(this.responseText);
};
sdata.open("get", "script.php", true);
sdata.send();
}
});
}
</script>
入体
<button class="myclass" onclick="send();">MyButton</button>
<input type="text" id="fname" placeholder="First name here" />
<input type="text" id="lname" placeholder="Last name here" />
按钮在输入之前,只供用户界面使用。
script.php
$prev = $_SESSION['prev'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
[....do something....]
echo 'You used '.$fname.' and '.$lname;
好吧..。script.php没有接收输入fname和lname的值。
发布于 2015-03-04 16:49:19
当您只需要一个ajax调用时,您将尝试进行两个ajax调用。
<script type="text/javascript">
function send() {
$.ajax({
type:"POST",
url: "script.php",
data: {
fname: $("#fname").val(),
lname: $("#lname").val()
},
beforeSend: function(){
alert('Sending');
},
success: function(data) {
//Received data from PHP script
alert(data);
},
error: function(){
alert('Error !');
},
complete: function(){
alert('Done');
}
});
}
</script>
发布于 2015-03-04 17:02:16
$.post(
'script.php', // Your PHP file
{ // The data to pass
'fname' : $('#fname').val(),
'lname' : $('#lname').val()
}
).done(function(data) { // Here your AJAX already finished correctly.
alert(data); // Show what the PHP script returned
});
$.ajax()
、$.get()
和$.post()
jQuery方法用于进行AJAX调用,因此,已经管理了自己的XMLHttpRequest对象。
https://stackoverflow.com/questions/28858427
复制相似问题