简体   繁体   English

如何从jQuery提交表单并导航到页面?

[英]how to submit form from jQuery and navigate to the page?

I have two php pages. 我有两个PHP页面。 Page 1 contains one form which i submitted with jQuery. 第1页包含一种我使用jQuery提交的表单。 Page 2 is form query page. 页面2是表单查询页面。 I want to read all the data from Page 2 after submitting from page 1 through navigation. 我想通过导航从第1页提交后,从第2页读取所有数据。 My following code does not work as I expected. 我的以下代码无法正常工作。

Page 1: 第1页:

$.ajax({
type: "POST",
url: "requestProcessor.php",
data: "fname="+ fname +"& lname="+ lname,
success: function(){

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

}
}); 

Page 2: requestProcessor.php 第2页:requestProcessor.php

<?php
require("db/phpsql_info.php");

echo htmlspecialchars(trim($_POST['fname']));
echo htmlspecialchars(trim($_POST['lname']));
?>

Thanks in advance.. 提前致谢..

First, change your data parameter to: 首先,将数据参数更改为:

data: { fname: fname, lname: lname }

It's more readable. 更具可读性。

2nd, it does not make sense what you are doing. 第二,你在做什么没有意义。 You are posting data to requestProcessor.php, then after that you send the user to requestProcessor.php (which is then a GET). 您将数据发布到requestProcessor.php,然后将用户发送到requestProcessor.php(然后是GET)。 This loses all the POST data from before of course as it's the equivalent of just typing requestProcessor.php into your URL bar. 当然,这会丢失之前的所有POST数据,因为这等同于在URL栏中键入requestProcessor.php。

If you want to go from page 1 to page 2 and see the data POSTed then just submit your form on page 1 to requestProcessor.php directly. 如果要从第1页转到第2页并查看POST数据,则只需将第1页上的表单直接提交给requestProcessor.php。

Else, if you want to show the result of page 2 after the POST, then send the response to some div on the current page. 否则,如果要在POST之后显示第2页的结果,则将响应发送到当前页面上的某个div。

success: function(response) {
    $("#some-div").html(response);
}

Not sure what you are trying to achieve though if I am honest. 坦白说,我不确定您要达到的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM