Jump to content

isset problem


GoodVibe

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.