Jump to content

help with an if statement


oraya

Recommended Posts

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 = "" ;
}

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
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.