简体   繁体   English

PHP:回声验证消息

[英]PHP: Echo Validation Message

I have a form in 'index.php' in wich I will validate some text fields using 'submit.php', if everything is ok it should insert the fields into my database using 'submit.php'. 我在“ index.php”中有一个表单,我将使用“ submit.php”验证一些文本字段,如果一切正常,则应使用“ submit.php”将字段插入我的数据库。 Now, if 'submit.php' detects an error it should echo the errors I stored in an array under my form at 'index.php': 现在,如果“ submit.php”检测到错误,它应该回显我在“ index.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 : 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 }
}

How can I echo what I have in $error BUT in INDEX.php inside my and if possible without refreshing the whole page? 我如何在我的INDEX.php中的$ error BUT中回显我的内容,如果可能的话,而不刷新整个页面? Thanks in advance! 提前致谢!

you can use AJAX to send whatever values and then update your elements by the result: 您可以使用AJAX发送任何值,然后通过结果更新元素:

...on HTML ...在HTML上

<form id="formid" action="submit.php" method="POST">

...on javascript ...在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
});

...on PHP (at the end) ...在PHP上(最后)

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

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

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