【问题标题】:Form Error Handleing in include file PHP包含文件 PHP 中的表单错误处理
【发布时间】:2014-04-03 22:58:27
【问题描述】:

我是 php 新手,我正在编写一个包含在 php 网页中的简单表单文件。 这是主页

makepedido.php

<!--Informacion Personal-->
<?php include 'personalinformation.php'; ?>

个人信息.php

<form action="scripts.php" method="post" name="personalinfo" class="form"       id="personalinfo">
<table width="100%" class="tableform">
<tr>
<td>Nombre</td>
<td><input name="Nombre" type="text"></td>
</tr>
<tr>
<td>Apellido</td>
<td><input name="Apellido" type="text" ></td>
</tr>
<tr>
<td>Direccion 1</td>
<td><input name="Direccion1" type="text" ></td>
</tr>
<tr>
<td>Direccion 2</td>
<td><input name="Direccion2" type="text" ></td>
</tr>
<tr>
<td>Ciudad</td>
<td><input name="Ciudad" type="text" ></td>
</tr>
<tr>
<td>Pais</td>
<td><input name="Pais" type="text" ></td>
</tr>
<tr>
<td>Numero de Telefono</td>
<td><input name="NumerodeTelefono" type="text" required></td>
</tr>
<tr>
<td>Correo Electronico</td>
<td><input name="CorreoElectronico" type="text" required></td>
</tr>
</table>
<br />
<br />
<input value="Crear Pedido" type="submit" class="btn btn-default"> <br />

</form>

scripts.php

<?php

$email = $_POST["CorreoElectronico"];

if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
  echo "Correo invalido intente de nuevo";
  die;
}

?>

现在我只想知道如何将scripts.php返回的错误放入personalinformation.php,当我点击提交按钮时,浏览器会转到script.php并显示错误消息。

我希望它留在 makepedido.php 中并显示来自 scripts.php 返回的个人信息.php 表单的错误消息

【问题讨论】:

  • 你要么需要将整个代码放在同一个文件中并使用action="",要么使用 Ajax/JS 来实现你想要做的事情。

标签: php html forms echo


【解决方案1】:

您应该使用 ajax 提交表单并将错误发送到同一页面,如下所示:

$("#formID").submit(function() {

    var url = "path/to/your/scripts.php"; // the script where you handle the form input.

    $.ajax({
           type: "POST",
           url: url,
           data: $("#idForm").serialize(), // serializes the form's elements.
           success: function(data)
           {
               alert(data); // show response from the php script.
           }
         });

    return false; // avoid to execute the actual submit of the form.
});

【讨论】:

  • 我到处都试过这个javascript代码,它没有处理scripts.php回显的错误。它不断将我发送到 scripts.php 以回应错误。
猜你喜欢
  • 1970-01-01
  • 2010-12-04
  • 2012-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-24
  • 1970-01-01
相关资源
最近更新 更多