Required fields in PHP form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ReMoN SaMiR
    New Member
    • Oct 2006
    • 22
    • 3.6.x

    Required fields in PHP form

    Hello,

    Few days ago I was searching online for a way to create a PHP forum though which the visitors can leave their messages and to automatically appear on the page. I found the a code and I used it to create the PHP form, but unfortunately I discovered the that all the fields are not required means that the visitor can submit an empty message and it will be added empty

    The form:

    PHP Code:
                <form id="form1" name="form1" method="post" action="addwish.php">
                  <
    td><table width="760" border="0" cellpadding="3" cellspacing="1">
                      <
    tr>
                        <
    td width="112" class="style13">Name:</td>
                        <
    td width="633" class="style13"><input name="name" type="text" id="name" size="50" /></td>
                      </
    tr>
                      <
    tr>
                        <
    td valign="top" class="style13">Email:</td>
                        <
    td class="style13"><input name="email" type="text" id="email" size="50" /></td>
                      </
    tr>
                      
                      <
    tr>
                        <
    td valign="top" class="style14 style15">Wishes</td>
                        <
    td class="style13"><textarea name="comment" cols="60" rows="6" id="comment"></textarea></td>
                      </
    tr>
                      <
    tr>
                        <
    td class="style14">&nbsp;</td>
                        <
    td class="style14"><input type="submit" name="Submit" value="Send" />
                          <
    input type="reset" name="Submit2" value="Clear" /></td>
                      </
    tr>
                  </
    table></td>
                </
    form
    The Addwish.php

    PHP Code:
      <?php
    $host
    ="localhost"// Host name
    $username="name"// Mysql username
    $password="password"// Mysql password
    $db_name="dbname"// Database name
    $tbl_name="tablename"// Table name

    // Connect to server and select database.
    mysql_connect("$host""$username""$password")or die("cannot connect server ");
    mysql_select_db("$db_name")or die("cannot select DB");

    $datetime=date("y-m-d h:i:s"); //date time

    $name=$_POST['name']; 
    $email=$_POST['email']; 
    $comment=$_POST['comment']; 
    $sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
    $result=mysql_query($sql);

    //check if query successful
    if($result){
    echo 
    "Your Birthday wish has been added successfully. Thank You!";
    echo 
    "<BR>";
    echo 
    "You'll be automatically redirected to the wishes page in 1 second.";
    }

    else {
    echo 
    "ERROR";
    }

    mysql_close();
    ?>

    Can anyone tell me how to edit the above code to make all the fields required before submitting the post into the database.

    Your help is appreciated. :rose:
  • sven4o
    Member
    • Feb 2010
    • 56
    • 3.8.x

    #2
    As a solution you can swap the following code lines:

    PHP Code:
    $sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
    $result=mysql_query($sql); 
    with

    PHP Code:
    if ($name == '')
    {
    print 
    'Fill in your name.';
    }
    elseif (
    $email == '')
    {
    print 
    'Fill in the e-mail.';
    }
    elseif (
    $comment == '')
    {
    print 
    'Write a comment.';
    }
    else 
    {
    $sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
    $result=mysql_query($sql);

    Then you can check whether the $result value is 0 and redirect the user back to the form.
    SiteGround Technical Support Team Member.
    Check out our special vBulletin hosting package.

    Comment

    • ReMoN SaMiR
      New Member
      • Oct 2006
      • 22
      • 3.6.x

      #3
      Originally posted by sven4o
      As a solution you can swap the following code lines:

      PHP Code:
      $sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
      $result=mysql_query($sql); 
      with

      PHP Code:
      if ($name == '')
      {
      print 
      'Fill in your name.';
      }
      elseif (
      $email == '')
      {
      print 
      'Fill in the e-mail.';
      }
      elseif (
      $comment == '')
      {
      print 
      'Write a comment.';
      }
      else 
      {
      $sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
      $result=mysql_query($sql);

      Then you can check whether the $result value is 0 and redirect the user back to the form.
      That was more than perfect, it works!

      Big thanks for your prompt reply. I appreciate it so much.

      May I ask you another question? I want to split the comments into php pages, can you help me with that?

      Comment

      • Aaron873
        Member
        • Jun 2010
        • 31
        • 4.0.0

        #4
        What do you mean split the comments into PHP Pages?
        If you want the error to appear on a blank page apart from the form you can simply do..

        PHP Code:
        if(isset($_POST['form'])){
        //Errors and Success here
        }else{
        //The actual forum can go here
        echo "<form method=post></form>"// Just don't add an action='', because it's not needed.

        Other than that, I'm not sure what else you're talking about.
        GBOmega Owner

        Comment

        • EddieHot
          New Member
          • Dec 2010
          • 8
          • 3.6.x

          #5
          agreed, better use if(isset($_POST['form'])) to elimite boring warnings.


          Originally posted by Aaron873
          What do you mean split the comments into PHP Pages?
          If you want the error to appear on a blank page apart from the form you can simply do..

          PHP Code:
          if(isset($_POST['form'])){
          //Errors and Success here
          }else{
          //The actual forum can go here
          echo "<form method=post></form>"// Just don't add an action='', because it's not needed.

          Other than that, I'm not sure what else you're talking about.
          http://www.budgetgadgets.com/

          Comment

          widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
          Working...