0

Please excuse my terminology as its a little shocking, I know. Currently I'm reading data from a MySQL database which is loaded into text and 4 buttons which are displayed on the webpage. This is used for a simple RPG system loading the data from a MySQL database and then loading the corresponding scenario based on the choices made by clicking on the buttons. Then using the data from the PHP loads the corresponding webpage using data in the parameters so for example:

I want it to load example.com/index.php?id=1&story=2 however for some unknown reason I cannot get the code to do that and every time it loads example.com/index.php (without the extra parameters which I need). Here is the code:

<?php
//Get the parameters
$id = $_GET["id"];
$story = $_GET["story"];

// Connect to the MySQL
$con=mysqli_connect("HOST_HERE","USER_HERE","PASSWORD_HERE","DATABASE_HERE");

// Check connection
if (mysqli_connect_errno())
{
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM Story");

while($row = mysqli_fetch_array($result))
{
       if ($id == $row['GameID'])
       {
              if ($story == $row['StoryID'])
              {
                     echo "<center>" . $row['Scenario'] . "</center>";
                     echo "<br><form method='link1' action='index.php?id=" . $id . "&story=" . $row['ChoiceAID'] . "'>";
                     echo "<input type='submit' value='" . $row['ChoiceA'] . "'></form>";
                     echo "<br><form method='link2' action='index.php?id=" . $id . "&story=" . $row['ChoiceBID'] . "'>";
                     echo "<input type='submit' value='" . $row['ChoiceB'] . "'></form>";
                     echo "<br><form method='link3' action='index.php?id=" . $id . "&story=" . $row['ChoiceCID'] . "'>";
                     echo "<input type='submit' value='" . $row['ChoiceC'] . "'></form>";
                     echo "<br><form method='link4' action='index.php?id=" . $id . "&story=" . $row['ChoiceDID'] . "'>";
                     echo "<input type='submit' value='" . $row['ChoiceD'] . "'></form>";
              }
       }
}

mysqli_close($con);
?>

The live site to help explain whats wrong is here: http://www.textbasedrpg.site90.com/?id=1&story=1

2
  • I do not see why your form method is link, it looks like you should be using GET since your using the getter $_GET to receive the data Commented Aug 8, 2014 at 23:32
  • after while($row = mysqli_fetch_array($result)) add print_r($row); to check exact field names Commented Aug 8, 2014 at 23:34

3 Answers 3

1

You need to use hidden elements to convey those values.

<form method="post" action="index.php">
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<input type="hidden" name="story" value="<?php echo $row['ChoiceAID']; ?>" />
<input type="submit" value="<?php echo $row['ChoiceA']; ?>" />
</form>

These values will be in $_POST instead of $_GET. As a side note, you need to use Single quotes on the outside and double quotes for the HTML elements.

Sign up to request clarification or add additional context in comments.

6 Comments

I tried using your method however it still only loads index.php not index.php?id=1&story=2
The URL won't change but the data is still being passed along. You could do a var_dump($_POST) at the top of the page to see the data.
Could you give me an example of how to do this?
Instead of $id = $_GET["id"]; do $id = $_POST["id"];. Do the same for $story. Then, copy and paste my code into your if statement where you have the echos currently. My code example covers ONE of the options. You'll need to add the others.
With the $_POST the new page remains as index.php?id=1&story=1 not index.php?id=1&story=2
|
0

You should add the "id" and "story" values an hidden inputs and make the "method" attribute of the form as "get"

                     echo "<center>" . $row['Scenario'] . "</center>";
                     echo "<br><form method='get'>";
                     echo "<input type='submit' value='" . $row['ChoiceA'] . "'>";
                     echo "<input type='hidden' name='id' value='" . $id . "'>";
                     echo "<input type='hidden' name='story' value='" . $row['ChoiceAID'] . "'></form>";
                     echo "<br><form method='get'>";
                     echo "<input type='submit' value='" . $row['ChoiceB'] . "'>";
                     echo "<input type='hidden' name='id' value='" . $id . "'>";
                     echo "<input type='hidden' name='story' value='" . $row['ChoiceBID'] . "'></form>";
                     echo "<br><form method='get'>";
                     echo "<input type='submit' value='" . $row['ChoiceC'] . "'>";
                     echo "<input type='hidden' name='id' value='" . $id . "'>";
                     echo "<input type='hidden' name='story' value='" . $row['ChoiceCID'] . "'></form>";
                     echo "<br><form method='get'>";
                     echo "<input type='submit' value='" . $row['ChoiceD'] . "'>";
                     echo "<input type='hidden' name='id' value='" . $id . "'>";
                     echo "<input type='hidden' name='story' value='" . $row['ChoiceDID'] . "'></form>";

1 Comment

Ahh.. forgot to add the names to hidden inputs. Try it now.
0

You could use an array button for example

   <form method=post '?'>

   <input type="submit" name="story[1]" value='Turn left">
   <input type="submit" name="story[2]" value='Turn right">
   etc.
   </form>

Then use

   $story = key($_POST["story"]);

If "Turn Right" is pressed, $story will be 2, and so on, simpler and easier solution. I see your ID is everywhere the same, so you could store it hidden,

  <input type="hidden" name="id" value="<?php echo $id; ?>">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.