First Name:"/> Midd-6ren">
gpt4 book ai didi

php - PHP 验证后将表单数据发布到另一个页面

转载 作者:行者123 更新时间:2023-12-05 05:26:30 25 4
gpt4 key购买 nike

我在验证和将数据发布到另一个页面时遇到了这个问题。

这是我的表格:

注册.php

<form id="regForm" action="<?php echo htmlspecialchars($_SERVER["submit.php"]);?>" method="post" name="regForm">
<label for="fname">First Name:</label><input name="fname" type="text" size="25" maxlength="35" value="<?php if(isset($_POST['fname'])){echo $_POST['fname'];}?>"/><br/>
<label for="mdname">Middle initial:</label><input name="mdname" type="text" size="10" maxlength="35" value="<?php if(isset($_POST['mdname'])){echo $_POST['mdname'];}?>"/><br/>
<label for="lname">Last Name:</label><input name="lname" type="text" size="25" maxlength="35" value="<?php if(isset($_POST['lname'])){echo $_POST['lname'];}?>"/><br/>
<br/>
<label>&nbsp;</label><input type="submit" name="Signup" class="formButton" value="Signup" /></form>

这是我的 submit.php,它将验证 signup.html 输入

提交.php

function msg($status,$txt)
{
return '{"status":'.$status.',"txt":"'.$txt.'"}';
}

// we check if everything is filled in and perform checks
//check if fname is empty
if(!$_POST['fname'])
{
die(msg(0,"<p>Please enter your first name.</p>"));
}
//check if lname is empty
if(!$_POST['lname'])
{
die(msg(0,"<p>Please enter your last name.</p>"));
}

现在,我的问题是,在我的“submit.php”文件中,我想知道在表单字段验证之后放置哪些代码,这将使我能够将输入数据发布到另一个页面,因为我计划将其制作成两页注册表单。假设我的下一页是 signup-2.html

验证后如何将数据发布到下一页?我知道如何检索下一页上发布的数据,例如使用 Session 或 Echo $_POST 数据,但是,我的主要问题是......如何在 提交的验证消息后让表单发布数据.php 文件?

最佳答案

使用标题:

header("Location: your page url?fname=$_POST['fname']&lname=$_POST['lname']");

但在此之前不要回显或打印任何内容,否则它不会重定向到该页面。

您可以像这样使用目标页面上的数据:

$_GET['fname']

例子:

submit.php

function msg($status,$txt)
{
return '{"status":'.$status.',"txt":"'.$txt.'"}';
}

// we check if everything is filled in and perform checks
//check if fname is empty
if(!$_POST['fname'])
{
die(msg(0,"<p>Please enter your first name.</p>"));
}
//check if lname is empty
if(!$_POST['lname'])
{
die(msg(0,"<p>Please enter your last name.</p>"));
}

header('Location:download.php?fname='.$_POST['fname']."&lname=".$_POST['lname']);

view.php

<html>
<body>
<form action="submit.php" method="post">
<input type='text' id="fname" name="fname"/>
<input type='text' id="lname" name="lname"/>
<input type="submit" id="button" value="submit"/>
</form>

</body>


</html>

download.php

<?php
echo "First Name........".$_GET['fname'];

把这三个文件放到同一个目录下,运行view.php。你会没事的。

关于php - PHP 验证后将表单数据发布到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25445927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com