#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    60
    Rep Power
    2

    Unhappy Could anyone tell what's wrong with this script, please?


    I'm trying to create a form system where 3 pages will ask you your name, age, address, and all the missing input will be listed on the confirmation page after the 3rd page.

    Please someone help me... been trying for a couple of hours and I'm new to PHP.

    ----- This is the script for the confirmation page----------------


    <?php
    // unset the formStarted session variable
    unset($_SESSION['formStarted']);

    // go through the elements in the $_SESSION
    foreach ($_SESSION as $key => $value) {
    // skip the missing element
    if ($key == "missing"){continue;}
    // skip the submit buttons
    // use identity operator with strpos to prevent false negatives
    if (strpos($key, 'Submit') === 0) {
    continue;
    }
    echo "<li>$key: $value</li>";
    }
    // clear the $_SESSION array and destroy session
    $_SESSION = array();
    session_destroy();
    ?>
    </ul>


    <?php


    if(isset($_SESSION['missing'])){
    $missingList = implode(", ", "$_SESSION['missing']");
    echo "<p>The following required field(s) are missing: $missingList. <br />Please fill them out below.</p>";
    }

    ?>



    ---------And the script below is from previous page. (there are 2 other pages to ask name and age before this page. )-----------


    session_start();
    if (!isset($_SESSION['formStarted'])) {
    header('Location: multiple01.php');
    exit;
    }
    // each page needs a different name for the submit button
    if (array_key_exists('Submit3', $_POST)) {
    include('shared.php');
    nukeMagicQuotes();
    // set required fields
    // must be an array, even if only one item is required
    // if no fields are required, an empty array is needed
    // otherwise, in_array() later in the script will generate an error
    $required = array('address');
    // create empty array for any missing fields
    //$missing = array();

    // process the $_POST variables and save them in the $_SESSION array
    foreach ($_POST as $key => $value) {
    // assign to temporary variable and strip whitespace if not an array
    $temp = is_array($value) ? $value : trim($value);
    // if empty and required, add to $missing array
    if (empty($temp) && in_array($key, $required)) {
    array_push($_SESSION['missing'], $key);
    }
    // otherwise, assign to a variable of the same name as $key
    else {
    $_SESSION[$key] = $temp;
    }
    }
    // if no required fields are missing, redirect to next page
    //if (!$missing) {
    header('Location: multiple04.php');
    exit;
    //}
    }
    ?>
    <!DOCTYPE html>

    <head>

    <title>Multiple form 3</title>
    </head>

    <body>

    <form id="form1" name="form1" method="post" action="">
    <p>
    <label for="address">Address:</label>
    <input type="text" name="address" id="address" /> (required)
    </p>
    <p>
    <input type="submit" name="Submit3" value="Send details" />
    </p>
    </form>
    </body>
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,850
    Rep Power
    6351
    Please read the new user guide directly below this message.

    If you're developing without error_reporting turned on, turn it on. Either make the change to error_reporting in php.ini or put this at the top of every page:

    error_reporting(E_ALL);

    That will tell you what's wrong.

    Your function nukeMagicQuotes() isn't defined. Even if it were, it's entirely unnecessary if you're developing on a modern system.

    The rest of your code is badly formatted and uncolored, so it's difficult to tell what's going on. Please wrap your code in [ PHP ] and [ /PHP ] tags (remove the spaces) which will allow us to better read your code.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,992
    Rep Power
    9397
    Originally Posted by ManiacDan
    [ PHP ] and [ /PHP ] tags (remove the spaces)
    Oh, like this?

    &amp;#91;PHP] and &amp;#91;/PHP]

    Comments on this post

    • ManiacDan agrees : Too lazy
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    60
    Rep Power
    2

    Thanks


    Did you mean like this??



    PHP Code:
    <?php
    // unset the formStarted session variable
    unset($_SESSION['formStarted']);

    // go through the elements in the $_SESSION
    foreach ($_SESSION as $key => $value) {
    // skip the missing element
    if ($key == "missing"){continue;}
    // skip the submit buttons
    // use identity operator with strpos to prevent false negatives
    if (strpos($key'Submit') === 0) {
    continue;
    }
    echo 
    "<li>$key$value</li>";
    }
    // clear the $_SESSION array and destroy session
    $_SESSION = array();
    session_destroy();
    ?>
    </ul>

    PHP Code:
    <?php


    if(isset($_SESSION['missing'])){
    $missingList implode(", ""$_SESSION['missing']");
    echo 
    "<p>The following required field(s) are missing: $missingList. <br />Please fill them out below.</p>";
    }

    ?>

    ---------And the script below is from previous page. (there are 2 other pages to ask name and age before this page. )-----------

    PHP Code:
    <?php
    session_start
    ();
    if (!isset(
    $_SESSION['formStarted'])) {
    header('Location: multiple01.php');
    exit;
    }
    // each page needs a different name for the submit button
    if (array_key_exists('Submit3'$_POST)) {
    include(
    'shared.php');
    nukeMagicQuotes();
    // set required fields
    // must be an array, even if only one item is required
    // if no fields are required, an empty array is needed
    // otherwise, in_array() later in the script will generate an error
    $required = array('address');
    // create empty array for any missing fields
    //$missing = array();

    // process the $_POST variables and save them in the $_SESSION array
    foreach ($_POST as $key => $value) {
    // assign to temporary variable and strip whitespace if not an array
    $temp is_array($value) ? $value trim($value);
    // if empty and required, add to $missing array
    if (empty($temp) && in_array($key$required)) {
    array_push($_SESSION['missing'], $key);
    }
    // otherwise, assign to a variable of the same name as $key
    else {
    $_SESSION[$key] = $temp;
    }
    }
    // if no required fields are missing, redirect to next page
    //if (!$missing) {
    header('Location: multiple04.php');
    exit;
    //}
    }
    ?>
    <!DOCTYPE html>

    <head>

    <title>Multiple form 3</title>
    </head>

    <body>

    <form id="form1" name="form1" method="post" action="">
    <p>
    <label for="address">Address:</label>
    <input type="text" name="address" id="address" /> (required)
    </p>
    <p>
    <input type="submit" name="Submit3" value="Send details" />
    </p>
    </form>
    </body>
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,850
    Rep Power
    6351
    Well...yes, like that, though I was hoping your code would be indented and formatted so it would actually be readable.

    Did you do the error_reporting? If this is your real code, I see at least one fatal error in the magic quotes function.

    Also...can YOU tell us what's wrong with this script? I just now realized you have no question
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    60
    Rep Power
    2

    Sorry for the late reply


    Thanks ManiacDan.

    yeah I just realized that.. My problem was the page named 'multiple04.php' doesn't show the script at all.

    So I wanted to ask someone about why the page isn't showing anything.
    Since I assumed there are some problems on the fourth and the third page, I just posted 2 scripts on the fourth page and 1 big chunk of script on the third page


    And am I supposed to use 'error_reporting(E_ALL);' right after the '<?php'?


    Actually, my Dreamweaver is telling me that there is error on the 2nd line of the 2nd chunk of the script, where it says '$missingList = implode(", ", "$_SESSION['missing']"); '.

    Can you tell what's wrong with this line or things above??

    sorry for the hard-to-read code..

    Thank you.
  12. #7
  13. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,101
    Rep Power
    1990
    If that's the line that the error is occuring on, it's quit easy. You don't want to have quotes around $_SESSION[]. If you have it wrapped in quotes it is seen as a string, and you can't pass a string to explode(). If you're going to be sure, you should check that the value is an array with is_array($_SESSION['missing']) before you try ot use explode() on it.
    PHP Code:
    $missingList implode(", "$_SESSION['missing']); 

IMN logo majestic logo threadwatch logo seochat tools logo