Jump to content

Recommended Posts

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: :P

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>

Link to comment
https://forums.phpfreaks.com/topic/136394-help-in-if-else-loop/
Share on other sites

<?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.

Link to comment
https://forums.phpfreaks.com/topic/136394-help-in-if-else-loop/#findComment-711688
Share on other sites

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!!

Link to comment
https://forums.phpfreaks.com/topic/136394-help-in-if-else-loop/#findComment-711712
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.