dpqaaczn141761 2016-10-15 13:07
浏览 12
已采纳

如果没有错误,PHP表单验证会做警报

I have the following form validation.

<?php 

    $error_occured = 0;
    $error_name = "";
    $error_email = "";
    $error_contact = "";
    $error_comments = "";

    if(isset($_POST["tx_name"])) {

            if(($tx_name == "") || (!preg_match("/^[a-zA-Z ]*$/",$tx_name))) {
                    $error_occured = 1;
                    $error_name = "Please enter a valid name";
                }

            if(($tx_email == "") || (!filter_var($tx_email, FILTER_VALIDATE_EMAIL))) {
                    $error_occured = 1;
                    $error_email = "Please enter a valid email";
                }

            if(($tx_contact == "") || !(is_numeric($tx_contact))) {
                    $error_occured = 1;
                    $error_contact = "Please enter a valid contact number";
                }

            if($tx_comments == "") {
                    $error_occured = 1;
                    $error_comments = "Please enter your message";
                }


        }

    if(isset($_POST["tx_name"]) && $error_occured = 0) {
            echo "<script>alert('Hi!');</script>";
        }

?>

The validation works fine and if there is no error, its supposed to do an alert. However, when I submit the form with no error, I do not see the alert message. How do I fix this?

  • 写回答

1条回答 默认 最新

  • dongyin2885 2016-10-15 13:10
    关注

    Try like this

    <?php 
    
    $error_occured = 0;
    $error_name = "";
    $error_email = "";
    $error_contact = "";
    $error_comments = "";
    
    if(isset($_POST["tx_name"])) {
    
            if(($tx_name == "") || (!preg_match("/^[a-zA-Z ]*$/",$tx_name))) {
                    $error_occured = 1;
                    $error_name = "Please enter a valid name";
                }
    
            if(($tx_email == "") || (!filter_var($tx_email, FILTER_VALIDATE_EMAIL))) {
                    $error_occured = 1;
                    $error_email = "Please enter a valid email";
                }
    
            if(($tx_contact == "") || !(is_numeric($tx_contact))) {
                    $error_occured = 1;
                    $error_contact = "Please enter a valid contact number";
                }
    
            if($tx_comments == "") {
                    $error_occured = 1;
                    $error_comments = "Please enter your message";
                }
    
         if($error_occured != 1) {
            echo "<script>alert('Hi!');</script>";
          }
     }
    
    
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置