0

I was totally confused by the following situation...

situation

post data from a.php to b.php and redirect to b.php...but fail

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 can get the right response from b.php without redirect function. However, once I include the statement window.location.href = "b.php";, a.php will redirect to b.php but without printing anything. Why is this situation happening? Is there any solution to fix this?

Thanks!

4

5 回答 5

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>
于 2013-05-10T09:42:44.567 回答
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>
于 2013-05-10T09:36:44.603 回答
1

您的 ajax 代码正在运行。它正在调用 url b.php 并打印要输出的值。然而,结果并不是你所期望的。

当您进行ajax调用并以表单发送数据时,当ajax请求成功时,b.php的评估将响应。尝试进行以下更改:

window.location.href = "b.php"; 

alert(response);

您将在消息框中看到您在输入中发送的内容。按照您的代码调整。注意我添加了一个按钮来拨打电话:

索引.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 作为它的操作,如下所示:

索引.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 中所做的更改。

于 2013-05-10T09:44:13.973 回答
0
试试这个,我想这会对你有所帮助。
在这里,我创建了两个页面。
一个是a.php,另一个页面是b.php

一个.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'];
?>
于 2013-05-10T11:37:59.787 回答
-1

利用

$_post['value'] 而不是 $_request['value']

于 2013-05-10T09:36:42.227 回答