PHP:回声验证消息

时间:2013-04-28 23:28:30

标签: php validation echo

我在'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 }
}

如何在我内部的INDEX.php中回显我在$ error中的内容,如果可能的话,不刷新整个页面? 提前谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用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
});

...关于PHP(最后)

...
// process your $_POST values you sent via AJAX and decide the result
...
echo json_encode( array("error"=>1, ...) );