dpkt17803 2013-04-28 23:28
浏览 16

PHP:回声验证消息

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'. Now, if 'submit.php' detects an error it should echo the errors I stored in an array under my form at '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 }
}

How can I echo what I have in $error BUT in INDEX.php inside my and if possible without refreshing the whole page? Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dpcyx08288 2013-04-28 23:55
    关注

    you can use AJAX to send whatever values and then update your elements by the result:

    ...on HTML

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

    ...on 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)

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

报告相同问题?

悬赏问题

  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录