Below is the code i've been trying to use to make a series of options, some are just 'continue' while others you choose between the options and it changes in which direction the story goes. The code as is is live at thecycle.ie but at the moment when you click 'continue' it changes , correctly to start, and you click it again and it goes back to the first entry.
Basically when you click i want it to store a value, and use that value to check along with $i, which goes up as you go through the game, which choice was made and output the correct values.
$i = 0;
$question = simplexml_load_file('xml/question.xml');
$opt1 = $question->entry[$i]->opt1;
$opt2 = $question->entry[$i]->opt2;
$story = simplexml_load_file('xml/story.xml');
$theSpeaker = $story->entry[$i]->speaker;
$theSpeech = $story->entry[$i]->speech;
echo'<form method="post" action="main.php">';
if($_POST['click']=='opt0' && $i==0)
{
$i = 1;
$theSpeaker = $story->entry[$i]->speaker;
$theSpeech = $story->entry[$i]->speech;
echo '<h2>'.$theSpeaker.'</h2>';
echo '<p>'.$theSpeech.'</p>';
echo '</div>
<div id="menu_about" class="menu_start">
<br>
<button type="submit" name="click" id="submit" value="opt1">'.$opt1.'</button>
</div>
</div>
</div>';
}
elseif($_POST['click']=='opt1' && $i==1)
{
$i = 2;
$theSpeaker = $story->entry[$i]->speaker;
$theSpeech = $story->entry[$i]->speech;
echo '<h2>'.$theSpeaker.'</h2>';
echo '<p>'.$theSpeech.'</p>';
echo '</div>
<div id="menu_about" class="menu_start">
<br>
<button type="submit" name="click" id="submit" value="opt2">'.$opt1.'</button>
</div>
</div>
</div>';
}
else
{
$i = 0;
$theSpeaker = $story->entry[$i]->speaker;
$theSpeech = $story->entry[$i]->speech;
echo '<h2>'.$theSpeaker.'</h2>';
echo '<p>'.$theSpeech.'</p>';
echo '</div>
<div id="menu_about" class="menu_start">
<br>
<button type="submit" name="click" id="submit" value="opt0">'.$opt1.'</button>
</div>
</div>
</div>';
}
In your script delete line 27, there are too many curly braces /THIS ONE/:
<script type="text/javascript">
$(document).ready(function() {
$("body").show();
$("body").css("display", "none");
$("body").fadeIn(1000);
//
$("#submit").click(function(event) {
event.preventDefault();
newLocation = this.href;
$("body").fadeOut(1000, newpage);
});
function newpage() {
window.location = newLocation;
} /*THIS ONE*/
});
</script>
Button behaviour varies browser-to-browser in what elements of the tag are submitted and available. Change your [button] to an [input]. That way you can be sure that name, value, etc.. is available.
$i = 0;
Value of $i will always be 0 when the script is encountered. so it goes to the else part. use session to store value of $i, change that session variable and conditions according to session variables. Hope this helps.
Why don't you just add a session or cookie? then every time when you need the 'continue' button to change its value, just pass it the session / cookie value
Session are like constants who are passed in the whole site as long as the tab is not closed. They can be modified and accessed from anywhere in the site.
Cookies are the same but are still stored until you clear them or their lifespan end