Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-10-2012, 05:00 PM   PM User | #1
listerdl
Regular Coder

 
Join Date: Mar 2011
Posts: 157
Thanks: 7
Thanked 0 Times in 0 Posts
listerdl is an unknown quantity at this point
Change upon submit page shoots to the top

What I mean is that when you click the submit button on my embedded php online quiz - the page refreshes and shoots up to the top.

What I would like is for the page to stay at exactly the same position.

Can this be done with just php? I was thinking that an anchor link would solve this but am not sure where to place that in the php code.

Another alternative is AJAX which I am useless at....

Thanks for all help with this..

Here is the code: (its an online quiz):

Code:
<?
$page_title = "The Simple Quiz Script";
// If the form is submitted run the script
if(isset($_POST['submit'])){
$quest1 = $_POST['quest1']; 
$quest2 = $_POST['quest2'];  
$quest3 = $_POST['quest3'];  
$quest4 = $_POST['quest4'];  
$quest5 = $_POST['quest5'];
// Lets make sure that everything has been submitted
if($quest1 == NULL OR $quest2 == NULL OR $quest3 == NULL OR $quest4 == NULL OR $quest5 == NULL){
$test_complete .='Please complete the quiz! <a href="javascript:history.go(-1)">Go Back</a>';
}else{
// change the quest1 to the right answer
if($quest1 == "3") { 
$test_complete .="Question one is <span class='green'>correct</span>, well done!<br/>";  
}else{ 
$test_complete .="Question one is <span class='red'>incorrect</span>!<br/>"; 
} 
// change the quest2 to the right answer
if($quest2 == "2") { 
$test_complete .="Question two is <span class='green'>correct</span>, well done!<br/>"; 
}else{ 
$test_complete .="Question two is <span class='red'>incorrect</span>!<br/>"; 
} 
// change the quest3 to the right answer
if($quest3 == "1") { 
$test_complete .="Question three is <span class='green'>correct</span>, well done!<br/>"; 
}else{ 
$test_complete .="Question three is <span class='red'>incorrect</span>!<br/>"; 
}
// change the quest4 to the right answer
if($quest4 == "3") { 
$test_complete .="Question four is <span class='green'>correct</span>, well done!<br/>"; 
}else{ 
$test_complete .="Question four is <span class='red'>incorrect</span>!<br/>"; 
}
// change the quest5 to the right answer
if($quest5 == "2") { 
$test_complete .="Question five is <span class='green'>correct</span>, well done!<br/>"; 
}else{ 
$test_complete .="Question five is <span class='red'>incorrect</span>!<br/>"; 
}
// Now lets see if all the questions are correct, this must match the above quest settings
if($quest1 == "3" & $quest2 == "2" & $quest3 == "1" & $quest4 == "3" & $quest5 == "2"){
$test_complete .="<p>Congratulations, you got all the questions correct!</p>"; 
}else{
// If any of the questions are not correct lets tell them
$test_complete .='<p>Your not there just yet! <a href="javascript:history.go(-1)">Try again</a></p>'; 
}}}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>The Simple PHP Quiz Script - www.funkyvision.co.uk</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
<!--
body,td,th {
	color: #000000;
}
.green {color:#009933;}
.red {color:#CC0000;}
-->
</style></head>
<body>
	<? if(!isset($_POST['submit'])){ ?>
	<h2>Quiz Script</h2>
	<p>Lets have some fun and see if you can get all the questions correct .. Good luck!</p>
    <form method="post">
  <p>1. Is this a good script?<br>
    <input type="radio" name="quest1" value="3">
    Yes<br>
    <input type="radio" name="quest1" value="2">
    Maybe<br>
    <input type="radio" name="quest1" value="1">
    No</p>
  <p>2. Do you like free scripts?<br>
    <input type="radio" name="quest2" value="1">
    Some times<br>
    <input type="radio" name="quest2" value="3">
    Never<br>
    <input type="radio" name="quest2" value="2">
    Always</p>
  <p>3. Are Funky Vision's scripts easy to use?<br>
    <input type="radio" name="quest3" value="2">
    No<br>
    <input type="radio" name="quest3" value="1">
    Yes<br>
    <input type="radio" name="quest3" value="3">
    Not sure</p>
  <p>4. Can you turn this script into a fun game? <br>
    <input type="radio" name="quest4" value="3">
    Yes
    <br>
    <input type="radio" name="quest4" value="2">
    Never
    <br>
    <input type="radio" name="quest4" value="1">
    Maybe
  </p>
	  <p>5. Can these quiz scripts add fun to your website? <br>
    <input type="radio" name="quest5" value="3">
    Nope
    <br>
    <input type="radio" name="quest5" value="2">
    Always<br>
    <input type="radio" name="quest5" value="1">
    Never
	  </p>
      <p>
    <input type="submit" name="submit" value="Submit Quiz">
  </p>
</form>
<? }else{ 
echo "<h2>Quiz Results</h2>
<p>".$test_complete."</p>";
}?>
</body>
</html>
listerdl is offline   Reply With Quote
Old 10-11-2012, 07:17 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,880
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by listerdl View Post
Can this be done with just php? I was thinking that an anchor link would solve this but am not sure where to place that in the php code.
you don’t place it in the PHP code, you place it in the HTML code. and the HTML code responsible for the page to load after submit is in the form’s action attribute (which is btw. a required attribute).
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 10-11-2012, 03:27 PM   PM User | #3
listerdl
Regular Coder

 
Join Date: Mar 2011
Posts: 157
Thanks: 7
Thanked 0 Times in 0 Posts
listerdl is an unknown quantity at this point
Quote:
Originally Posted by Dormilich View Post
you don’t place it in the PHP code, you place it in the HTML code. and the HTML code responsible for the page to load after submit is in the form’s action attribute (which is btw. a required attribute).
So to get the results of the php script at the center of the page I could add an anchor link to the form action like below?

<form action="mypage.php#anchor"> // page must be in the same directory

Would I append the form action to this string?

<form method="post"> so it becomes:

<form method="post" action="mypage.php#anchor">

Thanks

Last edited by listerdl; 10-11-2012 at 03:31 PM..
listerdl is offline   Reply With Quote
Old 10-12-2012, 06:02 AM   PM User | #4
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,880
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
yupp.

bear in mind that omitting the action attribute is invalid (though browsers tend to fix that for you).
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 10-12-2012, 06:07 PM   PM User | #5
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
You probably don't want to learn AJAX but some time later you may need it. Actually using 10 minutes you can master the JQuery Ajax method. It's a piece of cake. Check out to start with .get() or to do .post() . In JQuery AJAX, they do the same stuff. Or if you want to have more control of even the basic stuff, you can then use .ajax http://www.jensbits.com/2009/10/04/j...ples-with-php/

Of course you can only use it if you have an understanding of fundamental JQuery stuff.
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk
Redcoder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:48 PM.


Advertisement
Log in to turn off these ads.