oraya Posted July 4, 2012 Share Posted July 4, 2012 Could someone tell me what I have done wrong. I'm new to php so, hence why I'm here lol.. I have three options for the variable $phone [*]Phone number left and a call back requested. [*]Phone number left but no call back requested [*]No phone no or call back request And depending on the out come of the submitted form will depend on what the variable $phone is. At the moment the first and second work as I expected, but the last option no number prints out the variable from the elseif statement. I'm confused lol.. Many many thanks in advance, Oraya //DID THEY LEAVE A PHONE NUMBER OR REQUEST A CALL BACK? if (isset($phone) && ($callme == "Yes")) { // PHONE NO LEFT AND CALL BACK REQUESTED $phone = $title . " " . $last_name . " Requested a call back their number is: " . $phone; } elseif (isset($phone) && ($callme == "")) { // PHONE NO LEFT BUT NO CALL BACK REQUEST $phone = $title . " " . $last_name . " Left their phone number but did not request a call back, their number is: " . $phone; } else { // NO PHONE NO OR CALL BACK REQUESTED $phone = "" ; } Quote Link to comment https://forums.phpfreaks.com/topic/265191-help-with-an-if-statement/ Share on other sites More sharing options...
Barand Posted July 4, 2012 Share Posted July 4, 2012 The trouble with code that doesn't work is that it gives us no idea about what should happen. What do you want it to do? Quote Link to comment https://forums.phpfreaks.com/topic/265191-help-with-an-if-statement/#findComment-1359071 Share on other sites More sharing options...
oraya Posted July 4, 2012 Author Share Posted July 4, 2012 Hi Barand, I'd like it to display nothing in the variable if no phone number is entered in the form or call back request checkbox is left unchecked. The third option else $phone = "" I just can't figure out why it's using the elseif variable when nothing is there. I had to put a variable there otherwise when I sent the email I would get and undefined $phone variable error if they didn't enter a phone number. Hence why it's empty. Thank you for replying by the way! Any idea on what I did wrong? New to this so any feedback for learning would be great. Oraya Quote Link to comment https://forums.phpfreaks.com/topic/265191-help-with-an-if-statement/#findComment-1359072 Share on other sites More sharing options...
Barand Posted July 4, 2012 Share Posted July 4, 2012 Where do the $phone and $callme variables come from originally? Quote Link to comment https://forums.phpfreaks.com/topic/265191-help-with-an-if-statement/#findComment-1359073 Share on other sites More sharing options...
oraya Posted July 4, 2012 Author Share Posted July 4, 2012 I set the variables already from the form with $thevariable = $_POST['thename']; EDIT ----- I know they're working because I tested them by printing them out. Quote Link to comment https://forums.phpfreaks.com/topic/265191-help-with-an-if-statement/#findComment-1359074 Share on other sites More sharing options...
Barand Posted July 4, 2012 Share Posted July 4, 2012 So the code is executed when the form data is posted and you have something like $phone = $_POST['phone']; $callme = $_POST['callme']; If this is the case, $phone may be empty but it will always be set. The only time it won't be set is when no form data has been submitted. So <?php if (isset($_POST['phone'])) { // was form submitted $phone = $_POST['phone']; $callme = $_POST['callme']; if (empty($phone)) { $message = "You were called - no number"; } else { if ($callme == 'Yes') { $message = $title . " " . $last_name . " Left their phone number but did not request a call back, their number is: " . $phone; } else { $message = $title . " " . $last_name . " Left their phone number but did not request a call back, their number is: " . $phone; } } // email $message } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265191-help-with-an-if-statement/#findComment-1359078 Share on other sites More sharing options...
oraya Posted July 4, 2012 Author Share Posted July 4, 2012 Ah I see, so because I set it to a variable, using the isset was pointless because it was already set! That's brilliant, thank you so much for your time and patience! And for explaining to me my error. It makes perfect sense now that I think about it. I spent ages trying to figure it out myself through reading my books and looking online before coming to here. I like to try to figure it out myself if I can, but this time i couldn't lol.. Have a great day! Oraya Quote Link to comment https://forums.phpfreaks.com/topic/265191-help-with-an-if-statement/#findComment-1359085 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.