如何将两个html页面中的值传递给php?

时间:2021-12-25 21:10:46

I have 2 html pages :

我有2个html页面:

page1.html

<html>
 <body>
 <form action="page2.html" method="post">
 Enter First name: <input type="text" id="text1">
 <input type="submit" value="Next">
 </form>
 </body>
 </html>

page2.html

 <html>
 <body>
 <form action="test.php" method="post">
 Enter Last name: <input type="text" id="text2">
 <input type="submit" value="Submit">
 </form>
 </body>
</html>

Now, i would like to retrieve the value of text1 from page1.html and text2 from page2.html. How can i go about it?

现在,我想从page1.html中检索text1的值,从page2.html检索text2的值。我该怎么办呢?

1 个解决方案

#1


1  

Looks like you are looking for forms. See this tutorial http://www.w3schools.com/php/php_forms.asp. Post your form data from page1.php to page2.php. On page2.html you can access them via $_POST.

看起来你正在寻找表格。请参阅本教程http://www.w3schools.com/php/php_forms.asp。将您的表单数据从page1.php发布到page2.php。在page2.html上,您可以通过$ _POST访问它们。

Please be aware: This is not a secure example, just for showing purposes! When you want to show user generated data in your frontend, please use sanitizing and validation http://www.php.net/manual/en/filter.examples.sanitization.php.

请注意:这不是一个安全的例子,仅用于显示目的!如果要在前端显示用户生成的数据,请使用清理和验证http://www.php.net/manual/en/filter.examples.sanitization.php。

page1.php:

<form action="page2.php" method="post">
    Enter first name: <input type="text" name="firstName"><br>
    <input type="submit">
</form>

page2.php

<h1>Hey <?php echo $_POST['firstName']; ?></h1>
    <form action="lastpage.php" method="post">
    Enter last name: <input type="text" name="lastName"><br>
    <input type="hidden" name="firstName" value="<?php echo $_POST['firstName']; ?>">
    <input type="submit">
</form>

lastpage.php

<h1>Yo, my mate <?php echo $_POST['firstName']; ?> <?php echo $_POST['lastName']; ?>!</h1>

#1


1  

Looks like you are looking for forms. See this tutorial http://www.w3schools.com/php/php_forms.asp. Post your form data from page1.php to page2.php. On page2.html you can access them via $_POST.

看起来你正在寻找表格。请参阅本教程http://www.w3schools.com/php/php_forms.asp。将您的表单数据从page1.php发布到page2.php。在page2.html上,您可以通过$ _POST访问它们。

Please be aware: This is not a secure example, just for showing purposes! When you want to show user generated data in your frontend, please use sanitizing and validation http://www.php.net/manual/en/filter.examples.sanitization.php.

请注意:这不是一个安全的例子,仅用于显示目的!如果要在前端显示用户生成的数据,请使用清理和验证http://www.php.net/manual/en/filter.examples.sanitization.php。

page1.php:

<form action="page2.php" method="post">
    Enter first name: <input type="text" name="firstName"><br>
    <input type="submit">
</form>

page2.php

<h1>Hey <?php echo $_POST['firstName']; ?></h1>
    <form action="lastpage.php" method="post">
    Enter last name: <input type="text" name="lastName"><br>
    <input type="hidden" name="firstName" value="<?php echo $_POST['firstName']; ?>">
    <input type="submit">
</form>

lastpage.php

<h1>Yo, my mate <?php echo $_POST['firstName']; ?> <?php echo $_POST['lastName']; ?>!</h1>