2018-10-17 20:29
采纳率: 100%
浏览 41


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:


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

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

And here is the process file add.php->


     ini_set('display_errors', 1);
     //including the database connection file

      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 (

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

    Then the script exectuion will stop at this point.

    打赏 评论

相关推荐 更多相似问题