ngreenwood6 Posted September 4, 2008 Share Posted September 4, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/122717-if-and-else-help/ Share on other sites More sharing options...
JonnoTheDev Posted September 4, 2008 Share Posted September 4, 2008 Are you aware that your $parent & $child values will change after the form is submitted Quote Link to comment https://forums.phpfreaks.com/topic/122717-if-and-else-help/#findComment-633748 Share on other sites More sharing options...
ngreenwood6 Posted September 4, 2008 Author Share Posted September 4, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/122717-if-and-else-help/#findComment-633749 Share on other sites More sharing options...
ngreenwood6 Posted September 4, 2008 Author Share Posted September 4, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/122717-if-and-else-help/#findComment-633769 Share on other sites More sharing options...
ngreenwood6 Posted September 4, 2008 Author Share Posted September 4, 2008 Someone has to know what is going on here. Maybe someone can show me a better method? Quote Link to comment https://forums.phpfreaks.com/topic/122717-if-and-else-help/#findComment-633861 Share on other sites More sharing options...
elmas156 Posted September 4, 2008 Share Posted September 4, 2008 one problem that I see at a quick glance (I think) is that you should do this: if ((this == that || this == this) && (that == this || that == that)) instead of this: if (this == that || this == this && that == this || that == that) Quote Link to comment https://forums.phpfreaks.com/topic/122717-if-and-else-help/#findComment-633880 Share on other sites More sharing options...
JonnoTheDev Posted September 4, 2008 Share Posted September 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/122717-if-and-else-help/#findComment-633909 Share on other sites More sharing options...
sasa Posted September 4, 2008 Share Posted September 4, 2008 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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/122717-if-and-else-help/#findComment-633937 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.