我在'index.php'中有一个表单,我将使用'submit.php'验证一些文本字段,如果一切正常,它应该使用'submit.php'将字段插入到我的数据库中。现在,如果'submit.php'检测到错误它应该回显我在'index.php'表单下存储的数组中的错误:
index . php:
<form action="submit.php" method="POST">
<input type="text" name="email" />
<input type="text" name="url" />
<input type="submit" name="Submit" value="Continue"/>
</form>
<div class="errorsgohere"><?php //WHAT TO ADD HERE? ?></div>
SUBMIT.php:
if(isset($_POST['Submit'])) {
//I validate everything... etc..
if(!empty($errors)) {
echo "<div class='errors'>";
foreach($errors as $error) {
echo $error;
}
echo "</div>";
} else { SubmitDatabase(); //If there are no errors it will proceed }
}
我怎么能回什么我有在$error BUT在INDEX.php在我的,如果可能的话,不刷新整个页面?提前感谢!
您可以使用AJAX发送任何值,然后根据结果更新元素:
…在HTML
<form id="formid" action="submit.php" method="POST">
…在javascript
...
$("#formid").submit(function() {
var r = $.ajax({url:"submit.php",type:"POST",async:false,data:$("#formid").serialize()});
var result = JSON.parse( r.responseText );
// result["error"] should have the 1 you set in PHP
// now update your elements
});
…
...
// process your $_POST values you sent via AJAX and decide the result
...
echo json_encode( array("error"=>1, ...) );