Jump to content

Recommended Posts

I have this page:

 

<?php

$parents = array("Nick","Kristin","Mark","Missy");

$children = array("Peyton","Greddy","Abby","Will");

$random_parents = array_rand($parents);

$random_children = array_rand($children);

$parent = $parents[$random_parents];

$child = $children[$random_children];

echo "Is $parent the parent of $child?";
?>

<form method="post" action="">

<input type="checkbox" name="yes" value="yes">Yes
<br>
<input type="checkbox" name="no" value="no">No
<br>
<input type="submit" name="submit" value="submit">

</form>

<?

$yes = $_POST['yes'];
$no = $_POST['no'];

if($yes == yes)
{

if($child == Abby || $child == Will && $parent == Mark || $parent == Missy)
{
echo "That is correct";
}

else if($child == Peyton || $child == Greddy && $parent == Nick || $parent == Kristin)
{
echo "That is correct";
}

else
{
echo "You are wrong";
}

}

else if($no)
{

if($child == Abby || $child == Will && $parent == Nick || $parent == Kristin)
{
echo "That is correct";
}

else if($child == Peyton || $child == Greddy && $parent == Mark || $parent == Missy)
{
echo "That is correct";
}

else
{
echo "You are wrong";
}

}

else
{
echo "Please submit a guess";
}

?>

 

In this page Mark and Missy are the Parents of Abby and Will and Nick and Kristin are the Parents of Peyton and Greddy. For some reason no matter what I submit yes or no it always says "That is correct". Does anyone see anything wrong with my logic. Also, if I don't submit anything (ex. coming to the page for the first time) it always says "please submit a guess". I want it to say that if no one submits anything and only then. Does anyone see anything wrong with this or have any suggestions?

Link to comment
https://forums.phpfreaks.com/topic/122717-if-and-else-help/
Share on other sites

Yeah, but it should validate them through the ones that are currently there correct? I know that it will change which is fine because they can keep guessing as they change. I would think that it would try to authenticate the form through the current values not the updated ones correct?

Link to comment
https://forums.phpfreaks.com/topic/122717-if-and-else-help/#findComment-633749
Share on other sites

Is my thinking correct that you can make multiple statements like that. Example this statement:

 

if(this == that || this == this && that == this || that == that)

 

means this

 

if this is equal to that or this is equal to this and that is equal to this or that is equal to that then complete the statement. Which means that is this == that or this and that == that or this then do the statement. Is that correct if you can understand my stupidness?

Link to comment
https://forums.phpfreaks.com/topic/122717-if-and-else-help/#findComment-633769
Share on other sites

Yeah, but it should validate them through the ones that are currently there correct?

Incorrect, looking at your file the values will instantly change the moment that the form is submitted. What you need to do is store the current values of $parent & $child into hidden fields within the form so you can then test the $POST['parent'] & $POST['child'].

 

Once you check the values you should then reload the page so new values are generated so something like:

 

// form posted
if($_POST['submit'] == 'submit') {
if(($_POST['child'] == "Abby") || ($_POST['child'] == "Will" && $_POST['parent'] == Mark) || ($_POST['parent'] == Missy)) {
  echo "That is correct";
  // display a link to repload the page here
}

}
else {
$parents = array("Nick","Kristin","Mark","Missy");
$children = array("Peyton","Greddy","Abby","Will");
$random_parents = array_rand($parents);
$random_children = array_rand($children);
$parent = $parents[$random_parents];
$child = $children[$random_children];

// display the form here
?>
<form method="post" action="">
<input type="hidden" name="parent" value="<?php echo $parent; ?>" />
<input type="hidden" name="child" value="<?php echo $child; ?>" />
<?php
}

 

You should be able to complete the rest.

Link to comment
https://forums.phpfreaks.com/topic/122717-if-and-else-help/#findComment-633909
Share on other sites

try

<?php
$parents = array("Nick"=>2,"Kristin"=>2,"Mark"=>1,"Missy"=>1);
$children = array("Peyton"=>2,"Greddy"=>2,"Abby"=>1,"Will"=>1);
if (isset($_POST['submit'])){
$parent = $_POST['parent'];
$child = $_POST['child'];
echo "Question : Is $parent the parent of $child?<br />\n";
echo "Your ansver : $_POST[ansver]<br />\n";
$ansver = $parents[$_POST['parent']]==$children[$_POST['child']] ? 'yes' : 'no';
if ($_POST['ansver'] == $ansver) echo 'That is correct'; else echo 'You are wrong';
echo '<br /><a href="">Try again</a>';
} else {
$parent = array_rand($parents);
$child = array_rand($children);
echo "Is $parent the parent of $child?";
echo '<form method="post" action="">
<input type="hidden" name="parent" value="', $parent, '"
<input type="hidden" name="child" value="', $child, '"
<input type="radio" name="ansver" value="yes">Yes
<br>
<input type="radio" name="ansver" value="no">No
<br>
<input type="submit" name="submit" value="submit">
</form>';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/122717-if-and-else-help/#findComment-633937
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.