【发布时间】:2018-06-24 23:26:24
【问题描述】:
我对 PHP 还很陌生。我有一个表单,用户正在填写各种详细信息(开始日期、结束日期等),名为 purchaseLicence.php。提交时,表单操作会自行重新加载以使用 PHP 验证数据。
如果验证通过,我希望它使用 post 方法导航到 purchaseLicence2.php,就好像表单最初直接发布到 purchaseLicence2.php。
我不介意使用 Javascript 来执行此操作,而且我猜想它需要参与,因为它最终会看到与原本期望的形式不同的形式。
这是我当前的purchaseLicence.php,我得到的问题是purchaseLicence2.php和purchaseLicence.php都是在表单发布后呈现的,并且浏览器仍然指向purchaseLicence.php,而不是purchaseLicence2.php .
<?php
include_once('php/strings.php');
include_once('php/sprocs.php');
include_once('php/dates.php');
$encounteredValidationError = false;
$navigateAway=false ;
if (isset($_POST['process']))
{
if ($_POST['process'] == 1)
{
// if here, form has been posted
$ProductCode = $_POST['ProductCode'];
$StartDate = $_POST['StartDate'];
$EndDate = $_POST['EndDateHidden'];
// standardise the date formats to ISO8601
$StartDate = date("Y-m-d", strtotime($StartDate));
$EndDate = date("Y-m-d", strtotime($EndDate));
echo "<descriptive>" . PHP_EOL;
echo "ProductCode:" . $ProductCode . "<br/>" . PHP_EOL;
echo "StartDate:" . $StartDate . "<br/>" . PHP_EOL;
echo "EndDate:" . $EndDate . "<br/>" . PHP_EOL;
echo "</descriptive>" . PHP_EOL;
// validation to happen here
if (!$encounteredValidationError)
{
// so we're happy with the values. The form has just reloaded, so we need to put these back from $_POST into the input fields, so
// that we can call NavigateToPurchaseLicence2(), which will get them out of the input fields and post them to purchaseLicence2.php
// What a faff!
$data = array('ProductCode'=>$ProductCode, 'StartDate'=>$StartDate, 'EndDate'=>$EndDate);
$options = array(
'http'=>array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents('purchaseLicence2.php', false, $context);
if ($result === FALSE) { /* Handle error */ }
var_dump($result);
}
else
{
// hilite errors in the form here, how? form is not yet loaded
}
}
}
?>
</head>
<body>
<form method="post" action="purchaseLicence.php" id="form1">
<input type="hidden" name="process" value="1">
<table border=0 width=800px align=left style="margin: 0px auto;">
<tr> <!-- Product > -->
<td style="vertical-align:top" width="500px" bgcolor="lightgray"><descriptive>Product</descriptive></td>
<td width="500px" bgcolor="lightgray">
<?php
// creates a dropdown of products
OutputSelectFromSQL("SELECT * FROM Product ORDER BY Description", "ProductCode", "ProductCode", "Description", "");
?>
</td>
</tr>
<tr> <!-- Licence Period -->
<td style="vertical-align:top" width="500px" bgcolor="lightgray"><descriptive>Licence Period</descriptive></td>
<td width="500px" bgcolor="lightgray"><descriptive>1 year</descriptive></td>
</tr>
<tr> <!-- Start Date -->
<td style="vertical-align:top" width="500px" bgcolor="lightgray"><descriptive>Start/End Dates</descriptive></td>
<td width="500px" bgcolor="lightgray">
<input type="date" style="font-family:verdana;font-size:12px;" name="StartDate" id="StartDate" onchange="updateEndDate(this.value);"></input>
<descriptive> to <a id="EndDate"></a></descriptive>
<input type="hidden" name="EndDateHidden" id="EndDateHidden"></input> <!-- this is used so we can post the end date to $_POST -->
</td>
</tr>
<tr> <!-- Next > -->
<td style="vertical-align:top" width="500px" bgcolor="lightgray"><descriptive></descriptive></td>
<td width="500px" bgcolor="lightgray" align="right"><input type="submit" value="Next"></input></td>
</tr>
</table>
</form>
</body>
遵循标准模式的简单示例将非常有用。
【问题讨论】:
-
此问题缺少任何代码来向我们表明您已尝试自己解决此问题。如果您尝试过某些东西,请向我们展示您尝试过的内容、预期输出的示例以及您实际得到的结果。如果您没有尝试过任何事情,则需要在发布之前这样做。我们可以为您的现有代码提供帮助,但我们不会为您编写。请阅读:How to create a Minimal, Complete, and Verifiable example 和 How do I ask a good question?
-
我还建议您在保存数据之前进行验证。如果您在验证后将其发回并再次在浏览器中输出(以新形式,作为 js 数据或其他形式),任何人都可以在那里更改它,甚至直接将无效数据发布到
dataentry2.php并绕过验证在一起。 -
@MagnusEriksson - 嗨 Magnus,我已经编辑以显示我当前代码的缩减版本。理想情况下,我只想看到一些 PHP 代码使用 POST 方法将用户导航到新表单,但我怀疑这样的事情不存在。
-
@Lawrence Cherone - 非常感谢。我已将此标记为答案。因此,从概念上讲,使用 $_SESSION 变量在表单之间转移数据,而 post 方法只是让每个表单验证它自己的数据。此外,我缺少的代码是 exit(header('Location: dataentry2.php')),它只是导航到一个新表单并退出。
标签: javascript php forms post data-entry