GoodVibe Posted March 1, 2012 Share Posted March 1, 2012 Hi guys, first time posting here. Anyway, heres my problem. I have this section of code at the beggining of my page: if (isset($_POST['userChoice'])) { $try = check_word($_POST['userChoice']); echo ("We are into the loop"); } else { echo ("<br/>about to call change word"); changeword(); echo ("<br/> we called the changeword"); } And here is my form: <html> <head> </head> <body> <form name="wordguess" action="Index.php" method="POST"> <table> <tbody> <tr> Try to guess what the following word is: <?=$scrambled ?> <br/> </tr> <tr> Your Guess: <input type="text" id="userChoice" size="20" /> <br/> </tr> <tr> <td> <input type="submit" name="submit" id="submit" value="Submit"> </td> </tr> <tr> <?=$correct?> </tr> </tbody> </table> </form> </body> </html>' Now, how i assumed it worked was that the first time the page is loaded it would display the form and that's it. However, even when they hit the submit button on the form, its not really doing the POST, so when the page reloads, gives me the same stuff so it always goes on the else part of the if. Any help would be appreciated. Thanks, GoodVibe Quote Link to comment https://forums.phpfreaks.com/topic/258069-isset-problem/ Share on other sites More sharing options...
kicken Posted March 1, 2012 Share Posted March 1, 2012 Your input field is missing it's name attribute: name="userChoice" Quote Link to comment https://forums.phpfreaks.com/topic/258069-isset-problem/#findComment-1322859 Share on other sites More sharing options...
GoodVibe Posted March 1, 2012 Author Share Posted March 1, 2012 Thank you. i thought the ID part was taking care of that. I am fairly new to PHP, and Im trying to set this game up as a way to learn the concepts. I'm having an issue that once again I can't really figure out how to fix. I have this code: if (isset($_POST['userChoice'])) { $_SESSION['guess'] = $_POST['userChoice']; $try = check_word($_SESSION['guess']); echo ("<br/>We are into the loop"); } else { echo ("<br/>about to call change word"); changeword(); echo ("<br/> we called the changeword"); } I set the variables here: function changeword(){ echo "<br/>starting to change word"; $result = mysql_query("Select words from CurrentWords"); $totalwords = array(); while ($row = mysql_fetch_array($result)){ $totalwords[] = $row["words"]; } $_SESSION['$word'] = $totalwords[rand(0, count($totalwords))]; echo '<br/>'.$_SESSION['$word']; $_SESSION['scrambled'] = str_shuffle($_SESSION['$word']); echo '<br/>'.$_SESSION['scrambled']; echo "<br/>finished changing words"; } based on that, I have this: function check_word ($guessword){ return strtolower($guessword) == strtolower($_SESSION['word']); } The form is the same. Now, when i click submit and the page reloads, even though I've set the variables as a session. They still dissapear when I go to check the word, so I'm always getting a false. How can I fix this? Thanks, GoodVibe Quote Link to comment https://forums.phpfreaks.com/topic/258069-isset-problem/#findComment-1322874 Share on other sites More sharing options...
DavidAM Posted March 1, 2012 Share Posted March 1, 2012 1) Do you have session_start at the beginning of your scripts? Before any output to the browser 2) FYI: This $_SESSION['$word'] = $totalwords[rand(0, count($totalwords))]; needs to be this $_SESSION['$word'] = $totalwords[rand(0, count($totalwords)-1)]; Since the first word in the list is zero, the last word is one less than the count Quote Link to comment https://forums.phpfreaks.com/topic/258069-isset-problem/#findComment-1322891 Share on other sites More sharing options...
GoodVibe Posted March 1, 2012 Author Share Posted March 1, 2012 Yes, I have a session start at the beginning. Thank you for the point about my array count. And here, i will post my program that way it will be easier to check <?php error_reporting(E_ALL ^ E_NOTICE); session_start(); echo "starting program <br/>"; require_once ("dbconn.php"); $correct = "Your Answer was wrong "; $try = false; $me = $_SERVER['PHP_SELF']; $guess = $_POST['userChoice']; echo '<br/>' . $guess; if (isset($_POST['userChoice'])) { $_SESSION['guess'] = $_POST['userChoice']; $try = check_word($_SESSION['guess']); echo ("<br/>We are into the loop"); } else { echo ("<br/>about to call change word"); changeword(); echo ("<br/> we called the changeword"); } function changeword(){ echo "<br/>starting to change word"; $result = mysql_query("Select words from CurrentWords"); $totalwords = array(); while ($row = mysql_fetch_array($result)){ $totalwords[] = $row["words"]; } $_SESSION['$word'] = $totalwords[rand(0, count($totalwords)-1)]; echo '<br/>'.$_SESSION['$word']; $_SESSION['scrambled'] = str_shuffle($_SESSION['$word']); echo '<br/>'.$_SESSION['scrambled']; echo "<br/>finished changing words"; } function check_word ($guessword){ return strtolower($guessword) == strtolower($_SESSION['word']); } if ($try){ $previous = $word; changeword(); $correct = "Your Previous guess of ". $_POST['userChoice']. " Was Correct For the word ". $previous; } ?> <html> <head> </head> <body> <form name="wordguess" action="Index.php" method="POST"> <table> <tbody> <tr> Try to guess what the following word is: <?=$_SESSION['scrambled']?> <br/> </tr> <tr> Your Guess: <input type="text" name="userChoice" id="userChoice" size="20" /> <br/> </tr> <tr> <td> <input type="submit" name="submit" id="submit" value="Submit"> </td> </tr> <tr> <?=$correct?> </tr> </tbody> <tbody> <form> <input type="button" name="resetpage" onclick="<?php session_destroy(); ?>" /> </form> </tbody> </table> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/258069-isset-problem/#findComment-1322898 Share on other sites More sharing options...
DavidAM Posted March 1, 2012 Share Posted March 1, 2012 In changeword() you are storing the new word in the session array with a key of $word $_SESSION['$word'] = $totalwords[rand(0, count($totalwords)-1)]; echo '<br/>'.$_SESSION['$word']; $_SESSION['scrambled'] = str_shuffle($_SESSION['$word']); But in check_word, you are checking against the value in the session array with a key of word function check_word ($guessword){ return strtolower($guessword) == strtolower($_SESSION['word']); These are not the same array element. Quote Link to comment https://forums.phpfreaks.com/topic/258069-isset-problem/#findComment-1322910 Share on other sites More sharing options...
GoodVibe Posted March 1, 2012 Author Share Posted March 1, 2012 Unfortunately, though that is a mistake that would've popped up in the future. Right now I still have the issue where even though im storing the variables as a session, when I call them (specially the scrambled) in order to be displayed later in the form, it still just appears blank. Quote Link to comment https://forums.phpfreaks.com/topic/258069-isset-problem/#findComment-1322917 Share on other sites More sharing options...
batwimp Posted March 2, 2012 Share Posted March 2, 2012 This may be beside your point, but I think the session_destroy() in your button onclick value is destroying the session regardless. The nature of PHP is to evaluate the expression, then put the output (or return value) in its place. You can't have a button behave this way with PHP. Experts, tell me if I'm wrong. Quote Link to comment https://forums.phpfreaks.com/topic/258069-isset-problem/#findComment-1322934 Share on other sites More sharing options...
DavidAM Posted March 2, 2012 Share Posted March 2, 2012 batwimp, good catch. session_destroy destroys all of the data associated with the current session The session_destroy() here is being executed before the page is ever displayed to the user. The onclick attribute then has nothing assigned to it, so the button will have no effect. Quote Link to comment https://forums.phpfreaks.com/topic/258069-isset-problem/#findComment-1322977 Share on other sites More sharing options...
batwimp Posted March 2, 2012 Share Posted March 2, 2012 Thanks, David. Quote Link to comment https://forums.phpfreaks.com/topic/258069-isset-problem/#findComment-1323003 Share on other sites More sharing options...
GoodVibe Posted March 2, 2012 Author Share Posted March 2, 2012 I thought that by adding it as the action it didnt occur until it was clicked? Quote Link to comment https://forums.phpfreaks.com/topic/258069-isset-problem/#findComment-1323121 Share on other sites More sharing options...
batwimp Posted March 2, 2012 Share Posted March 2, 2012 That's true for javascript, but not PHP. Quote Link to comment https://forums.phpfreaks.com/topic/258069-isset-problem/#findComment-1323123 Share on other sites More sharing options...
GoodVibe Posted March 2, 2012 Author Share Posted March 2, 2012 Well thats good to know. Removed that, but my problem is still there. the variables aren't passing on. I've put echo statements throughout and even though the procedures are being called correctly, since the variables are empty, it never does a good comparison Quote Link to comment https://forums.phpfreaks.com/topic/258069-isset-problem/#findComment-1323137 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.