2013-04-28 146 views
0

我在'index.php'中有一个表单,我将使用'submit.php'验证一些文本字段,如果一切正常,它应该使用'submit.php将字段插入到我的数据库中”。现在,如果“submit.php”检测到错误,应该呼应我存储在数组下,我在形式“的index.php”的错误: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内,如果可能的话,而不刷新整个页面? 在此先感谢!

+0

你可以在前一页显示错误,但是你可以让'submit.php'看起来像'index.php',但是有错误,或者只是在$ _SERVER ['PHP_SELF']'的同一页面上验证。不刷新你需要AJAX。 – elclanrs 2013-04-28 23:35:13

回答

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, ...));