#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Posts
    26
    Rep Power
    0
    Ok, I know I keep running into careless errors but I have a mental block with this code and can't seem to find the problem.

    I'm trying to process a form by sending information to the database. None of the fields are required, all are declared as NOT NULL. If I leave a field blank, no problems. But I leave a checkbox field blank, my query won't run through. And the only way my data is inserted into the database is if I select ALL the options in the checkboxes. I haven't put them into an array yet -- will do that later -- right now just concerned with why I can't leave a checkbox blank.

    Here's the code:

    <?php

    if (isset($ID) && isset($First_Name) && isset($Exam1) && isset($Exam2) && isset($Exam3) && isset($Exam4) && isset($Exam5) && isset($Exam6) && isset($Exam7))
    {
    echo "IS SET";

    mysql_connect("i-20experts.com", "username", "password");
    $query = "INSERT INTO Applicants VALUES ('$ID', '$First_Name', '$Exam1', '$Exam2', '$Exam3', '$Exam4', '$Exam5', '$Exam6', '$Exam7')";
    $result = mysql_db_query("host", $query);
    }
    ?>

    <form>
    <input type="Hidden" name="ID">

    First Name:
    <input type="Text" name="First_Name" size="20" maxlength="60">

    Mark any of the following exams you have taken:
    <input type="checkbox" name="Exam1" value="TOEFL"> TOEFL
    <input type="checkbox" name="Exam2" value="SAT I"> SAT I
    <input type="checkbox" name="Exam3" value="SAT II"> SAT II
    <input type="checkbox" name="Exam4" value="ACT"> ACT
    <input type="checkbox" name="Exam5" value="GRE"> GRE
    <input type="checkbox" name="Exam6" value="GMAT"> GMAT
    <input type="checkbox" name="Exam7" value="Other"> Other
    </form>


    I've named them all as separate fields temporarily -- they will be in an array later on. But I can't seem top find why I can't leave one blank when they are already declared Not Null in the schema. If I select all of them, I get my desired output of "IS SET" and the data is inserted into the database. If I leave even one blank, nothing goes through.

    Someone point me to the right direction again please...
  2. #2
  3. No Profile Picture
    Gödelian monster
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 1999
    Location
    Central Florida, USA
    Posts
    2,307
    Rep Power
    61
    I believe with a checkbox it has to be clicked for the variable to even be set. In other words, it's not even a NULL without being checked and unchecked.
  4. #3
  5. No Profile Picture
    Gödelian monster
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 1999
    Location
    Central Florida, USA
    Posts
    2,307
    Rep Power
    61
    Actually I was wrong. I don't think it has a value even when it's checked and unchecked, but there is a solution, posted in the Devshed HTML/DHTML forum under:

    "Possible to put a value for non-checked checkboxes? "

    Essentially the answer is to use Javascript to hold "proxy" values in hidden form fields, which change as the checkbox is checked or unchecked.

    Another answer is not to even check in PHP whether those particular variables are set at all. (maybe the simplest thing, but think about it some)

  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Posts
    26
    Rep Power
    0
    So I don't have to list them in the ISSET section at all to check if the variable is set? The last time I left out a variable from the ISSET section, my query didn't run through at all. How would I go about doing that?
  8. #5
  9. No Profile Picture
    Gödelian monster
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 1999
    Location
    Central Florida, USA
    Posts
    2,307
    Rep Power
    61
    I suppose you could do something like:

    if !isset($Exam1){
    $Exam1 = 0
    }

    etc...

    (! being the NOT operator...)

    Then run your query as you coded before.

  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Posts
    26
    Rep Power
    0
    Thanks, that did the trick!

Similar Threads

  1. auto populating form
    By usrbin in forum PHP Development
    Replies: 1
    Last Post: February 11th, 2004, 01:10 PM
  2. auto populating form
    By usrbin in forum PHP Development
    Replies: 4
    Last Post: February 11th, 2004, 11:52 AM
  3. Avoid form resubmission when REFRESH clicked
    By rahulgup in forum ASP Programming
    Replies: 0
    Last Post: February 5th, 2004, 01:21 PM
  4. HTTP Error 400: Bad Request, with basic form
    By crackwombat in forum PHP Development
    Replies: 1
    Last Post: January 23rd, 2004, 06:40 PM
  5. form creation using php
    By emzyme in forum PHP Development
    Replies: 4
    Last Post: January 15th, 2004, 06:23 AM

IMN logo majestic logo threadwatch logo seochat tools logo