sheldon_cooper Posted February 18, 2015 Share Posted February 18, 2015 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 Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 18, 2015 Share Posted February 18, 2015 What does the code look like for your forms? I have a feeling that "colourname" isn't being passed through the second form. Quote Link to comment Share on other sites More sharing options...
sheldon_cooper Posted February 18, 2015 Author Share Posted February 18, 2015 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 Quote Link to comment Share on other sites More sharing options...
Solution cyberRobot Posted February 18, 2015 Solution Share Posted February 18, 2015 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: http://seancoates.com/blogs/xss-woes Quote Link to comment Share on other sites More sharing options...
sheldon_cooper Posted February 18, 2015 Author Share Posted February 18, 2015 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! Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 18, 2015 Share Posted February 18, 2015 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"; } ?> Quote Link to comment Share on other sites More sharing options...
sheldon_cooper Posted February 18, 2015 Author Share Posted February 18, 2015 Thanks cyberRobot, you are a genius!!! Cannot thank you enough! Think I might need to go through another few tutorials before I attempt a few pages on my own. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.