Jump to content

Newbie - if else problem . . . . I think


sheldon_cooper
Go to solution Solved by cyberRobot,

Recommended Posts

HI folks,

 

Been reading the forums for a while, but this is my first post - what a wealth of knowledge on these boards!

 

I've been trying to learn PHP and followed a few tutorials, thought I was doing well but have hit some trouble with a page I've been creating. Basically I've created a single page with two forms that take input (one from a list, one from a text box) and those work fine. However when I attempt to read the value of the of the POST variables into the third function to display, it only displays one and I can't figure out why!

 

I think I've made errors in my if/else statement at the bottom but I've rewrote it a few times and think i'm just getting confused! Here is the code:

if (empty($_POST['colourname'])) {
        pickacolour();
}
if (!empty($_POST['colourname'])) {
        selectUser();
        echo "You have selected " . $_POST['colourname'];
}
if (isset($_POST['resetNOW'])) {
        if (isset($_POST['resetNOW'])){
                testingMe();
                echo "<br>You just entered " . $_POST['user'];
                echo "<br>You just selected " . $_POST['colourname'];
        }
        else{
                echo "Nothing to see here";
        }
}

It gets to the end and prints the 'testingMe' function (which basically just echos to say we made it that far) and the user entered but won't print the colourname, that is always blank. But it does print the colourname when I am selecting a user in the 'selectUser' function - I just can't figure out why it doesn't echo it with the 'testingMe' function and its driving me insane!

 

Thanks for any help guys :)

Link to comment
Share on other sites

Hi cyberRobot, thanks for your reply. My forms look like :

<?php

include 'connect.php';

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);


function pickacolour(){
?>

<form name="colourlist" method="post" action="<?php print $_SERVER['PHP_SELF'] ?>">
<table border=0>
<tr>
<td>Select this colour: </td>

<?php

$colourlist = file("colourlist.txt");
echo '<td><select name="colourname">';
foreach ($colourlist as $list) {
        echo '<option value="' . $list .'">' .$list. '</option>';
        }
echo '</select>';

?>


<input type=hidden value=<?php print $_POST['colourForm']; ?> name=colourForm>
<td><input type=submit tabindex=1 value="Next" name="colour"></td></tr>
</table></form>

<?php

} // end of pickacolour()

function selectUser(){
?>

<form name"enteruser" method="post" action"<?php echo $_SERVER['PHP_SELF'] ?>">
<table border=0>
<tr><td>Enter username (Favourite colour being <?php echo "". $_POST['colourname'] ?>) <input type="text" name="user"></td>
<input type=hidden value=<?php echo $_POST['colourForm']; ?> name=colourForm>
<td><input type=submit tabindex=1 value="Submit" name="resetNOW"></td></tr>

<?php

} // end of selectUser


function testingMe(){

        echo "This is a test";
        echo "<br>You just entered " . $_POST['user'];
        echo "<br>You just selected " . $_POST['colourname'];
} //end of testingMe



//////////////////////////////////
//       Main prog              //
//////////////////////////////////

if (empty($_POST['colourname'])) {
        pickacolour();
}
if (!empty($_POST['colourname'])) {
        selectUser();
        echo "You have selected " . $_POST['colourname'];
}
if (isset($_POST['resetNOW'])) {
        if (isset($_POST['resetNOW'])){
                testingMe();
                echo "<br>You just entered " . $_POST['user'];
                echo "<br>You just selected " . $_POST['colourname'];
        }
        else{
                echo "Nothing to see here";
        }
}




?>

Probably not the most efficient or cleanest code, but I'm still learning!

 

Thanks for looking

Link to comment
Share on other sites

  • Solution

It looks like you're not passing the colorname through the second form (under selectUser()). Try adding a hidden field like the following:

<input type="hidden" value="<?php echo $_POST['colourname']; ?>" name="colourname">
<td><input type=submit tabindex=1 value="Submit" name="resetNOW"></td></tr>
 
Note that I added quotes around the attribute values. That way color names like "Dark Blue" will work. If you don't use quotes, browsers will only recognize "Dark" as the value...and they won't know what to do with "Blue".
 
Also, note that using the raw value from PHP_SELF as the form's action opens your page up to XSS attacks. More information can be found here:
Link to comment
Share on other sites

You sir, are a genius!! I was banging my head off a brick wall with that one and didn't even think to look at my forms! Thank you again!

 

Now I just need to figure out a way to fix the if/else isset/empty statements at the bottom so when it displays the 'testingMe' function, it doesn't ask for a username input (second form) at the same time!

Link to comment
Share on other sites

Now I just need to figure out a way to fix the if/else isset/empty statements at the bottom so when it displays the 'testingMe' function, it doesn't ask for a username input (second form) at the same time!

 

You could try changing the order:

<?php
if (isset($_POST['resetNOW'])) {
        if (isset($_POST['resetNOW'])){
                testingMe();
                echo "<br>You just entered " . $_POST['user'];
                echo "<br>You just selected " . $_POST['colourname'];
        }
        else{
                echo "Nothing to see here";
        }
 
} elseif (empty($_POST['colourname'])) {
    pickacolour();
 
} elseif (!empty($_POST['colourname'])) {
    selectUser();
    echo "You have selected " . $_POST['colourname'];
}
?>

Also, you don't need to check if "resetNOW" is set twice. Maybe something like this will work:

<?php
if (isset($_POST['resetNOW'])) {
    testingMe();
    echo "<br>You just entered " . $_POST['user'];
    echo "<br>You just selected " . $_POST['colourname'];
 
} elseif (empty($_POST['colourname'])) {
    pickacolour();
 
} elseif (!empty($_POST['colourname'])) {
    selectUser();
    echo "You have selected " . $_POST['colourname'];
 
} else {
    echo "Nothing to see here";
}
?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.