我对以下情况感到困惑......
的情况
将数据从 a.php 发布到 b.php 并重定向到 b.php ...但是失败
CODE 的 - a.php
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function() {
$('#submit').click(function() {
$.ajax({
url: 'b.php',
dataType: 'html',
type: 'POST',
data: {
value: $('#value').val()
},
error: function(xhr) {
console.log('ajax went wrong!');
},
success: function(response) {
console.log(response);
window.location.href = "b.php";
}
});
});
});
</script>
</head>
<body>
value: <input type="text" id="value">
</body>
</html>
CODE 的 - b.php
<?php
echo $_REQUEST['value'];
?>
a.php 可以从 b.php 获得正确的响应而无需重定向功能。但是,一旦我包含语句window.location.href = "b.php";
, a.php 将重定向到 b.php 但不打印任何内容。
为什么会出现这种情况?
有没有解决方法来解决这个问题?
谢谢!
答案 0 :(得分:1)
您无法以这种方式在页面之间传递数据。将数据发布到b.php并重定向到它 - 是两个不同的请求。
如果你想通过重定向传递数据 - 使用GET参数:
window.location.href = "b.php?value="+$('#value').val();
直接向b.php表单提交可以帮到你。
<form action="b.php" method="post">
<input name="value" />
<input type="submit" />
</form>
答案 1 :(得分:1)
您只需要一个<form>
,这样每当您点击submit
按钮时,您的所有表单数据都会转移到b.php
(您将自动将其重定向到b.php
页面)你可以访问$_POST['value']
。这很简单。无需$.ajax
来电。
<form id="myFrm" name="myFrm" method="post" action="b.php">
value: <input type="text" id="value" name="value">
<input type="submit" id="submit" name="submit" value="submit">
</form>
答案 2 :(得分:1)
您的ajax代码正常运行。它调用url b.php并将值打印到输出。但是,结果并不是您所期望的。
当你进行ajax调用并在表单中发送数据时,当ajax请求成功时,b.php的评估将会响应。尝试进行以下更改:
来自
window.location.href = "b.php";
到
alert(response);
您将在消息框中看到您在输入中发送的内容。遵循您的代码改编。注意我添加了一个按钮来拨打电话:
的index.php
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function() {
$('#submit').click(function() {
$.ajax({
url: 'ajax.php',
dataType: 'html',
type: 'POST',
data: {
value: $('#value').val()
},
error: function(xhr) {
console.log('ajax went wrong!');
},
success: function(response) {
alert(response);
console.log(response);
//window.location.href = "b.php";
}
});
});
});
</script>
</head>
<body>
value: <input type="text" id="value">
<input type=button id="submit" value=go>
</body>
</html>
ajax.php
<?php
echo $_REQUEST['value'];
?>
这是ajax方法。但是如果你只需要将表单中的值传递给b.php,则不需要ajax。只需创建一个表单并使用b.php,就像这样:
的index.php
<html>
<head>
</head>
<body>
<form method="POST" action="b.php">
value: <input type="text" id="value" name="value">
<input type=submit value=go>
</form>
</body>
</html>
b.php
<?php
echo $_REQUEST['value'];
?>
请注意我在您的HTML中所做的更改。
答案 3 :(得分:0)
Try this one, i think this will help you. Here i create two pages. one is a.php and another page is b.php
a.php只会
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function() {
var abc='';
$('#submit').click(function() {
var abc=$('#abc').val();
$.ajax({
url: 'b.php',
dataType: 'html',
type: 'POST',
data: {
value: abc
},
error: function(xhr) {
console.log('ajax went wrong!');
},
success: function(response) {
console.log(response);
var stateObj = { foo: "b" };
history.pushState(stateObj, "page 2", "b.php?value="+response);
$('#hid').hide();
$('#res').html(response);
}
});
});
});
</script>
<html>
<body>
<div id="hid">
Value: <input type="text" id="abc" /> <input type="button" id="submit" value="Get Value" /><br>
</div>
<div id="res"></div>
</body>
</html>
b.php
<?php
echo $_REQUEST['value'];
?>
答案 4 :(得分:-1)
使用
$ _ post ['value']而非$ _request ['value']