javascript 使用 PHP 将表单数据从一个网页传递到另一个网页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6913970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:18:55  来源:igfitidea点击:

Passing form data from one web page to another with PHP

phpjavascriptjqueryforms

提问by Ad Reactor

i found few similar questions here but from answers i didn't get the whole picture of how should work.

我在这里发现了几个类似的问题,但从答案中我没有得到应该如何工作的全貌。

I have a subscription form in a page:

我在页面中有一个订阅表格:

<form method="post" action="index.php/register"> 
<fieldset> 
<input type="text" id="first_name" name="first_name" /> 
<input type="text" id="last_name" name="last_name" />
<input type="text" id="email" name="email" />
<input type="text" id="address" name="address" /> 
<input id="submit" type="submit" value="&gt;&gt;" /> 
</fieldset> 
</form>

when a user a user click the submit button is lead to a page with the full registering form, where i need to have few fields populated with the data sent from previous page form. this is a preview of few fields from the form of the second page:

当用户单击提交按钮时,将进入一个带有完整注册表单的页面,我需要在其中填充几个字段,其中包含从上一页表单发送的数据。这是第二页表单中几个字段的预览:

<form id="register" name="form1" method="post" action="send_contact.php">
<fieldset>  
<li><label>*First Name</label>
<input type="text" id="first_name" name="first_name" />
</li>

<li>
<label>*Last Name</label>
<input type="text" id="last_name" name="last_name" />
</li>

<li>
<label>*Email</label>
<input type="text" id="email" name="email" />
</li>

<li>
<label>*Confirm Email</label>
<input type="text" id="confirm-email" name="confirm_email" />
</li>

<li>
<label>Street Address</label>
<input type="text" id="address" name="address" />

<li class="full-width">
<input id="submit" type="submit" value="Register" />                    
</li>

</fieldset>
</form> 

the php is not my strong point, so if you can be more detailed in answer is great for me.

php 不是我的强项,所以如果你能更详细地回答对我来说很棒。

thanks!

谢谢!

采纳答案by Tumharyyaaden

I would say for security reasons, do not use Get method "$_GET[]" as people described, keep POST as you have it.

我会说出于安全原因,不要$_GET[]像人们描述的那样使用 Get 方法“ ”,保持 POST 就可以了。

All you need to do on register/ page is get all the values passed on using the POST method and populate them into your HTML. So the second form should look like:

您需要在注册/页面上做的就是获取使用 POST 方法传递的所有值并将它们填充到您的 HTML 中。所以第二种形式应该是这样的:

    <form id="register" name="form1" method="post" action="send_contact.php">
    <fieldset>  
    <li><label>First Name</label>
    <input type="text" id="first_name" name="first_name" value="<?=$_POST[first_name]?>" />
    </li>

    <li>
    <label>*Last Name</label>
    <input type="text" id="last_name" name="last_name" value="<?=$_POST[last_name]?>" />
    </li>

    <li>
    <label>*Email</label>
    <input type="text" id="email" name="email" value="<?=$_POST[email]?>" />
    </li>

    <li>
    <label>*Confirm Email</label>
    <input type="text" id="confirm-email" name="confirm_email" />
    </li>

    <li>
    <label>Street Address</label>
    <input type="text" id="address" name="address" value="<?=$_POST[address]?>" />

    <li class="full-width">
    <input id="submit" type="submit" value="Register" />                    
    </li>

    </fieldset>
    </form> 

Above, I am using shorthand version of "echo" and php tags, if you do not have that enabled under php.ini, please change "" to "; ?>. Also, script will not populate "confirm" email as I assume you would like the user to retype that.

以上,我使用的是“echo”和php标签的简写版本,如果你没有在php.ini下启用,请将“”更改为"; ?>。此外,脚本不会填充“确认”电子邮件,因为我假设您希望用户重新输入。

That should do it.

那应该这样做。

回答by Jonah Katz

There are basically two methods

基本上有两种方法

  1. Store the values of the first form in a cookie, and the process code can retrieve the values from the cookie

  2. make the form action 'Get' so that the data is passed on to the next page.

  1. 将第一个表单的值存储在cookie中,流程代码可以从cookie中取值

  2. 使表单操作“获取”,以便将数据传递到下一页。

回答by Sherif elKhatib

<form method="post" action="register.php"> 
<fieldset> 
<input type="text" id="first_name" name="first_name" /> 
<input type="text" id="last_name" name="last_name" />
<input type="text" id="email" name="email" />
<input type="text" id="address" name="address" /> 
<input id="submit" type="submit" value="&gt;&gt;" /> 
</fieldset> 
</form>

register.php

register.php

<form id="register" name="form1" method="post" action="send_contact.php">
<fieldset>  
<li><label>*First Name</label>
<input type="text" value="<?php echo $_POST['first_name'];?>" id="first_name" name="first_name" />
</li>

<li>
<label>*Last Name</label>
<input type="text" value="<?php echo $_POST['last_name'];?>" id="last_name" name="last_name" />
</li>

<li>
<label>*Email</label>
<input type="text" value="<?php echo $_POST['email'];?>" id="email" name="email" />
</li>

<li>
<label>*Confirm Email</label>
<input type="text" id="confirm-email" name="confirm_email" />
</li>

<li>
<label>Street Address</label>
<input type="text" value="<?php echo $_POST['address'];?>" id="address" name="address" />

<li class="full-width">
<input id="submit" type="submit" value="Register" />                    
</li>

</fieldset>
</form>

Finally service the post in send_contact.phpas you wish

最后send_contact.php按照您的意愿为帖子提供服务

回答by PtPazuzu

You can use the $_POST values from the first form in the page handling the submit of the first form and print them in the new form as:

您可以使用页面中第一个表单中的 $_POST 值来处理第一个表单的提交,并将它们打印在新表单中:

<?php
  echo '<input type="text" id="email" name="email" value="' . htmlentities($_POST['email']) . '"/>
?>