-3

So, I have a problem with getting data from forms and using it in my codes. this is choose options page:

        <form action="options.php" method="post"> 
        <label>Select number of options:</label>
        <select name="options">
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
        </select><br><br>
        <input type="submit" name="next" value="Next"><br><br>
    </form> 

and this is options.php page

<?php
    // put your code here
    $options = $_POST['options'];
    if (isset($_POST['submit'])) {
        echo $options;
    }
    ?>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
        Fill in the following fields:<br><br>
        <?php
        //loop to prompt the user to enter options' details
        for ($i = 1; $i <= $options; $i++) {
            $optionName = "option$i";
            ?>
            <?php echo 'Option ' . $i; ?><input type="text" name="<?php echo $optionName; ?>"/><br><br>
            <?php
        }
        ?>
        <input type="submit" name="submit" value="Next"/>
    </form>

Somehow I can't get it working. It keeps giving me an error which is "undefined index". I tried to fix it with isset(). I keep doing something wrong here but I don't know what is it. Will someone please help me or suggest some solutions and ways to get it working. I am new at this and started learning last week.

John Conde
  • 217,595
  • 99
  • 455
  • 496
eSS92
  • 241
  • 1
  • 3
  • 10

4 Answers4

1

Put these at the beginning of your code to see what's actually posted (it will also show the get values). These format the Get and Post values nicely. I use them all the time. Once you see what's actually posted you'll be able to see what isn't what you're expecting.

echo("<br><br>Get contents:"); echo("<pre>" . print_r($_GET, 1) . "</pre>");
echo("<br>Post contents:"); echo("<pre>" . print_r($_POST, 1) . "</pre>");
exit;
rrtx2000
  • 626
  • 4
  • 13
0

Your $options = $_POST['options']; should be $options = (isset($_POST['options']) ? $_POST['options'] : "");

0

You have

<input type="submit" name="next" value="Next"><br><br>

Later you check

if (isset($_POST['submit'])) {

The name you give the input, is the key you want to use when looking up the value.

if (isset($_POST['next'])) {
msfoster
  • 2,474
  • 1
  • 17
  • 19
  • I'm aware of that, sir. You probably did not understand what I was saying up there. I have two pages. First page is chooseoptions.php page which has "next" button and the second page has the "submit" button. – eSS92 Apr 30 '13 at 23:44
0

To start you $_POST['submit'] will never be set because there's nothing posting to that field.

If you want to debug this, let's start by getting the output of EVERYTHING posted. We can do that by doing a print_r($_POST); like the following.

    <?php
        echo '<pre>';
        print_r($_POST);
        echo '</pre>'
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
        Fill in the following fields:<br><br>
        <?php
        //loop to prompt the user to enter options' details
        for ($i = 1; $i <= $options; $i++) {
            $optionName = "option$i";
            ?>
            <?php echo 'Option ' . $i; ?><input type="text" name="<?php echo $optionName; ?>"/><br><br>
            <?php
        }
        ?>
        <input type="submit" name="submit" value="Next"/>
    </form>

Then you can see what POST fields are available to you easily and continue coding your project from there.

Sajan Parikh
  • 4,668
  • 3
  • 25
  • 28
  • Thank you for sharing this. I think now I can use this array to get the number of options the user has chosen and use it in my codes. – eSS92 Apr 30 '13 at 23:53