简体   繁体   English

包含文件PHP中的表单错误处理

[英]Form Error Handleing in include file PHP

i am new in php and i am programming a simple form file that is included in a php webpage. 我是php的新手,我正在编程一个包含在php网页中的简单表单文件。 this is the main page 这是主页

makepedido.php makepedido.php

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

personalinformation.php personalinformation.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 scripts.php

<?php

$email = $_POST["CorreoElectronico"];

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

?>

now i just want to know how to put in personalinformation.php the error returned by scripts.php, when i click the submit button, the browser goes to script.php and show the error msg. 现在我只想知道如何将personalsinformation.php放入scripts.php返回的错误,当我单击“提交”按钮时,浏览器转到script.php并显示错误msg。

i want it to stay in makepedido.php and show the error msg from the personalinformation.php form returned by scripts.php 我希望它保留在makepedido.php中,并显示scripts.php返回的personalinformation.php表单中的错误消息。

You should use ajax to submit the form and get the error to the same page, like this: 您应该使用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.
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM