1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: PHP - if/then logic

Discussion in 'Web Design & Development' started by SociPoet, Sep 13, 2011.

Thread Status:
Not open for further replies.
Advertisement
  1. SociPoet

    SociPoet Thread Starter

    Joined:
    Sep 12, 2011
    Messages:
    6
    Hi all,
    I'm new to the html and php coding club, so I'm learning as I go. I am developing a form on a php webpage, connecting to a MySQL database. Right now, I am working on the form validation (yes, I know everyone's favorite). Let me know what I am missing from the code below. Essentially, I am checking if fields are empty once the submit button is pushed. If any field is empty, the red html notice should appear, warning the user. If no field is empty on the pushed submit button, the form should be submitted successfully into the database. Yet, the code below does ABSOLUTELY NOTHING, regardless if I fill all fields or leave an empty field. Is it my IF logic?


    if (isset($_POST['Submitted']) AND
    ($_POST['date']==""
    || $_POST['state']==""
    || $_POST['county']=="")){
    ?>
    <tr><td></td><td><center><font size="2" color="red" face="arial"> Please enter all fields of your input </font></center></td></tr>
    <?php
    }
    if (isset($_POST['Submitted']) AND
    $_POST['date']!==""
    AND $_POST['state']!==""
    AND $_POST['county']!=="")
    {

    $date=mysql_real_escape_string($_POST['date']);
    $state=mysql_real_escape_string($_POST['state']);
    $county=mysql_real_escape_string($_POST['county']);


    ... on to form submission into database with query and all

    Any suggestions are much appreciated.
    Thanks y'all!

     
  2. colinsp

    colinsp

    Joined:
    Sep 5, 2007
    Messages:
    1,833
    Try assigning your post results to your variables BEFORE you do your if/then statements.
     
  3. SociPoet

    SociPoet Thread Starter

    Joined:
    Sep 12, 2011
    Messages:
    6
    Thanks colinsp! I got it to work. I re-ordered the $_POST assignments as you mentioned and even cleaned up the if/then logic to one if/else statement.

    While I did your suggestion, it turns out I misspelled the 'Submit' button name with 'Submitted'. Nine times out of ten, it's the little things that cause the frustration with coding!

    if (isset($_POST['Submit'])) {
    $date=mysql_real_escape_string($_POST['date']);
    $state=mysql_real_escape_string($_POST['state']);
    $county=mysql_real_escape_string($_POST['county']);

    if (empty($date)
    || empty($state)
    || empty($county){
    ?>
    <tr><td></td><td><center><font size="2" color="red" face="arial"> Please enter all fields of your input </font></center></td></tr>
    <?php
    }
    else { ... database insert and query
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1017392