www.webdeveloper.com
Results 1 to 8 of 8

Thread: isset doesnt work.

  1. #1
    Join Date
    Feb 2014
    Posts
    4

    isset doesnt work.

    Im trying to make form and I want if the field is empty it doesnt show it at all, but it shows anyway.

    <form action="form.php" method="get">
    <fieldset style="width: 250px;">
    <legend>Register Form</legend>

    <!-- name, surname, email, password -->
    <label>First name: <input type="text" name="firstname"></label><br>
    <label>Last name: <input type="text" name="lastname"></label><br>
    <label>E-mail: <input type="text" name="email" size="24"></label><br>
    <label>Password: <input type="password" name="password"></label><br>

    <!-- gender -->
    <label for="gender">Gender:
    <input type="radio" name="gender" value="M" /> Male
    <input type="radio" name="gender" value="F" /> Female</p>
    </label>

    <!-- drop down menu -->
    <label>Age:
    <select name="age">
    <option value="0-29">Under 30</option>
    <option value="30-60">Between 30 and 60</option>
    <option value="60+">Over 60 </option>
    </select>
    </label><br>

    <!-- comment -->
    <label>Comments: <textarea name="comments" rows="3" cols="40"></textarea>

    </fieldset>
    <!-- submit button -->
    <input type="submit" value="Submit" name="submit">

    </form>


    and

    <?php

    $firstname = $_REQUEST['firstname'];
    $lastname = $_REQUEST['lastname'];
    $email = $_REQUEST['email'];
    $password = $_REQUEST['password'];
    $gender = $_REQUEST['gender'];
    $age = $_REQUEST['age'];

    if (isset($_REQUEST['firstname']))
    echo 'Firstname is: ' . $firstname;
    if (isset($_REQUEST['lastname']))
    echo '<br>Lastname is: ' . $lastname;
    echo '<br>Email is: ' . $email;
    echo '<br>Password is: ' . $password;
    echo '<br>Gender is: ' . $gender;
    echo '<br>Age is: ' . $age;

    Why it shows lines under if? And when I use chrome it shows me Notice: Undefined index: gender in C:\xampp\htdocs\form.php on line 7. but there is nothing wrong. :/

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    699
    Don't understand your question. Can you be more exact about "under if"?

    Also - you should not use GET to pass all this data. Use POST and never check the REQUEST array for your input values.

    <form method="POST">
    and

    $firstname = $_POST['firstname'];

    would be the right way.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,304
    Here's a little trick you can do with variable variables:
    PHP Code:
    $fields = array(
        
    'firstname',
        
    'lastname',
        
    'email',
        
    'password',
        
    'gender',
        
    'age'
    );
    foreach(
    $fields as $field) {
        ${
    $field} = '';
        if(isset(
    $_POST[$field])) {
            ${
    $field} = trim($_POST[$field]);
        }
    }
    // each of those fields should now exist as a variable, e.g.:
    echo "Email: $email"
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Feb 2014
    Posts
    4
    Quote Originally Posted by ginerjm View Post
    Don't understand your question. Can you be more exact about "under if"?

    Also - you should not use GET to pass all this data. Use POST and never check the REQUEST array for your input values.

    <form method="POST">
    and

    $firstname = $_POST['firstname'];

    would be the right way.
    I changed to $_POST, but still it doesnt work.
    if (isset($firstname))
    echo 'First name is: ' . $firstname;
    It shows anyway if its empty or if it has a value.

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,304
    Remember that isset() just means the variable is defined, not whether or not it is an empty string. Use empty() for that test (but remember that a value of 0 is empty).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Feb 2014
    Posts
    4
    Thanks you guys for helping. Got what I wanted.
    Final Result

    <?php

    $fields = array(
    'firstname',
    'lastname',
    'email',
    'password',
    'gender',
    'age'
    );
    foreach($fields as $field) {
    ${$field} = '';
    if(isset($_POST[$field])) {
    ${$field} = trim($_POST[$field]);
    }
    }
    // each of those fields should now exist as a variable, e.g.:
    if (!empty($firstname))
    echo "Firstname: $firstname<br>";
    if (!empty($lastname))
    echo "Lastname: $lastname<br>";
    if (!empty($email))
    echo "Email: $email<br>";
    if (!empty($password))
    echo "Password: $password<br>";
    if (!empty($gender))
    echo "Gender: $gender<br>";
    if (!empty($age))
    echo "Age: $age<br>";

    ?>

  7. #7
    Join Date
    Feb 2014
    Posts
    4
    0 is a value can be fixed: if (!empty($firstname) || $firstname == 0)

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,304
    Another test I've used to make sure they entered something:
    PHP Code:
    if(trim($_POST['foo']) === '') { // note use of "is identical to" operator
        // it's blank or just white-space

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles