doupu3211 2018-10-17 20:29 采纳率: 100%
浏览 42
已采纳

有没有办法阻止表单在php上抛出ctype_digit错误后提交

im a newbie here in php still learning. what im in need of is to stop the form once the user put something other than an integer in the Age field and not submit to mysql. here is my the basic form and code:

add.html->

    <form action="add.php" method="post" name="form1">
    <table width="25%" border="0">
        <tr> 
            <td>Name</td>
            <td><input type="text" name="name"></td>
        </tr>
        <tr> 
            <td>Age</td>
            <td><input type="text" name="age"></td>
        </tr>
        <tr> 
            <td>Email</td>
            <td><input type="text" name="email"></td>
        </tr>

        <tr> 
            <td></td>
            <td><input type="submit" name="Submit" value="Add"></td>
        </tr>
    </table>
</form>

And here is the process file add.php->

    <?php



     error_reporting(E_ALL);
     ini_set('display_errors', 1);
     //including the database connection file
      include_once("config.php");

      if(isset($_POST['Submit'])) { 
       $name = mysqli_real_escape_string($mysqli, $_POST['name']);
       $age = mysqli_real_escape_string($mysqli, $_POST['age']);
       $email = mysqli_real_escape_string($mysqli, $_POST['email']);



        // checking integer fields
        if (!ctype_digit($age)){
        echo "<font color='red'>ERREUR!  age n'est PAS entier</font><br/>";
         }
         // checking empty fields
         if(empty($name) || empty($age) || empty($email)) {

          if(empty($name)) {
          echo "<font color='red'>Name field is empty.</font><br/>";
           }

            if(empty($age)) {
           echo "<font color='red'>Age field is empty.</font><br/>";
           }


           if(empty($email)) {
            echo "<font color='red'>Email field is empty.</font><br/>";
            }

           //link to the previous page
           echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
            } else { 
           // if all the fields are filled (not empty) 

    //insert data to database   
    $result = mysqli_query($mysqli, "INSERT INTO users(name,age,email)    VALUES('$name','$age','$email')") or die(mysql_error());

    //display success message
    echo "<font color='green'>Data added successfully.";
    echo "<br/><a href='index.php'>View Result</a>";
      }
     }
      ?>

Please ive read many posts but as a beginner it seems too difficult to dive into javascript now. if this is the only solution can you teach me -lol- how to implement the few lines of scripts. thanks

  • 写回答

1条回答 默认 最新

  • donglou1866 2018-10-17 20:40
    关注

    You can use exit function (http://php.net/manual/en/function.exit.php)

    if (!ctype_digit($age)){
        echo "<font color='red'>ERREUR!  age n'est PAS entier</font><br/>";
        exit();
    ...
    

    Then the script exectuion will stop at this point.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?