fierdor Posted December 10, 2008 Share Posted December 10, 2008 I have just started with PHP a week ago...I want to develop sumthing of a webquiz where u hv to answer d prev question complusorily before going to the next one.. so i hv a database(mysql) storing my answers and i m using xampp for testing at home....i hv sum questions which may sound stupid: I hv a basic if else loop in d code to check whether d answer is right or wrong..if it is right it goes to d next page using d "header("Location:"")" or else it goes to an error page with d question displayed again(coz i cant call d same page again as it is coz d browser says it is a indeterminate loop) I hv d form where d user types in his answer in d same php page... so wat happens is whenever i type d url of d page in d browser i m directly taken to d page dat shud b displayed fr d wrong answer...instead of d page itself.i m posting my sample code...plss help!! <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("myst", $con); $result = mysql_query("SELECT * FROM answers WHERE level='1'"); while($row = mysql_fetch_array($result)) { $a=$row['answer']; $b=$_POST["name"]; if($a==$b) { header("Location:http://localhost/myst/welcome1.php"); exit; } else { header("Location:http://localhost/myst/welcomeerror.php"); echo("You are wrong"); exit; } } ?> <html> <body> <form method="post" action="<?php echo $PHP_SELF;?>"> Answer: <input type="text" name="name" /> <input type="submit" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/136394-help-in-if-else-loop/ Share on other sites More sharing options...
Stryves Posted December 10, 2008 Share Posted December 10, 2008 If you really want someone to spend the time to answer your question, spend the time to ask it properly. Quote Link to comment https://forums.phpfreaks.com/topic/136394-help-in-if-else-loop/#findComment-711686 Share on other sites More sharing options...
genericnumber1 Posted December 10, 2008 Share Posted December 10, 2008 <?php if(isset($_POST['name'])) { // Your code here } ?> Wrap your main body of php code in that. The problem you're having is it's checking even if the user hasn't submitted something yet. Assuming of course, that a proper answer isn't "null". As he said this isn't a text message, so I'm sure you have a full keyboard. I'm pretty sure you can use the calorie it takes to type "the" instead of "d".. unless you're starving in a third world country... in which case you should probably buy food instead of paying the internet bill. Quote Link to comment https://forums.phpfreaks.com/topic/136394-help-in-if-else-loop/#findComment-711688 Share on other sites More sharing options...
fierdor Posted December 10, 2008 Author Share Posted December 10, 2008 Ok i am sorry..i realise i should have put forth my problem more clearly...wont happen again...considering dat i will be pestering you all more frequently from now on i will try to be more clear and not be miserly with words... But it has been solved with d isset thing....Thanks a lot!! When i see the source code of the above posted PHP page in my browser i see <html> <body> <form method="post" action=""> Name: <input type="text" name="name" /> <input type="submit" /> </form> </body> </html> so does that mean that the url's of the further levels(questions) will be hidden from the users?? and also what if sumone comes to know of the next url and types it in the browser,will he gain access to that? Edit:I checked this and i gain access to the next level without answering the previous level. What should i do to prevent that kind of a scenario? Thanks in advance!! Quote Link to comment https://forums.phpfreaks.com/topic/136394-help-in-if-else-loop/#findComment-711712 Share on other sites More sharing options...
DeanWhitehouse Posted December 10, 2008 Share Posted December 10, 2008 Use sessions, here is a tutorial to help http://djw-webdesign.awardspace.com/code.php?snippet=9 Quote Link to comment https://forums.phpfreaks.com/topic/136394-help-in-if-else-loop/#findComment-711717 Share on other sites More sharing options...
genericnumber1 Posted December 10, 2008 Share Posted December 10, 2008 instead of $PHP_SELF use $_SERVER['PHP_SELF'], it won't fix your problem, but I just hate to see that mistake Quote Link to comment https://forums.phpfreaks.com/topic/136394-help-in-if-else-loop/#findComment-711723 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.