我是php的新手,我正在编写一个包含在php网页中的简单表单文件。 这是主页
makepedido.php
<!--Informacion Personal-->
<?php include '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
<?php
$email = $_POST["CorreoElectronico"];
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
echo "Correo invalido intente de nuevo";
die;
}
?>
现在我只想知道如何将personalinformation.php放入scripts.php返回的错误中,当我点击提交按钮时,浏览器会转到script.php并显示错误信息。
我希望它留在makepedido.php中并显示来自scripts.php返回的personalinformation.php表单的错误信息。
答案 0 :(得分:0)
您应该使用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.
});