Jump to content

If variable blank display nothing


Travist6983

Recommended Posts

OK guys thanks for reading my post!

 

i am having a slight issue with a contact form and that is if someone doesnt fill in the phone number box then it just ads a blank space to the email that gets sent such as below:

 

This is filled out -

Name: Larry Boy

Phone: 703-922-4010

Email: [email protected]

 

This is not filled out-

Name: Larry Boy

 

Email: [email protected]

 

I am using this to figure out if it is actually blank or not:

 

if ($Tel == "")
$TelResults = "";
else
$TelResults = "Phone: ".$Tel;

 

 

 

and this to display it on the email

$Body .= $TelResults;

 

 

 

I understand this is VERY small thing but my boss hates getting contacts forms that "dont look right" i have scowered the net trying to figure this out so now i am turning to this forum for some help... Can someone please point me in the right directions...

Link to comment
https://forums.phpfreaks.com/topic/208924-if-variable-blank-display-nothing/
Share on other sites

Actually, i do believe that Alex's code is sound logic. Did you even try it? If you look at the code it states that if there is something in the $Tel variable, then add it to the body. An expanded look could be:

if ($name != "") {
   $Body = "Name: ".$name;
}
if($Tel != "") {
    $Body .= "<br />Phone: ".$Tel;
}
if($email != "") {
    $Body .= "<br />Email: ".$email;
}

echo $Body;

If done properly, then if any field is blank it wouldn't even bother with showing the line:

 

No Name

Phone: 123-456-7890

Email: [email protected]

 

No Phone

Name: Joe Test

Email: [email protected]

 

No Email

Name: Joe Test

Phone: 123-456-7890

Thank You Both Alex and hcdarkmage... i didn't quite understanding what Alex was exactly saying in that case... thanks for elaborating hcdarkmage

 

you were exactly correct if applied correctly it DOES indeed Work...

 

I am very much a PHP noob

 

maybe you can help me write another similar statement  with applying that same code... ok so i am trying to do the same thing only display things that are filled in from the contact form...

 

so currently i am using this to fill in there time frame of when they want to buy something

$Body .= "Time Frame of buying: ";
$Body .= $immediate;

$Body .= $Ldays;

$Body .= $Mdays;

 

Where on the contact form each of those variables such as $immediate is value =  "Right Now!" then i have added a comma and   just so that it looks correct on the transmitted form

 

any suggestions to make this all work together?

 

and again thank you both you have been VERY helpful  ;D

Just a quick question: The time frame"thing" you are trying to add. Are the variables from a dropdown box, checklist or radio list? It might help if I/we had a visual of the form that you are working with. More code is better then less.

Thanks hcdarkmage it is coming from checklist buttons but i have made it work though i dont know it is the best way to do it

 

is this the best way to do it?

if($buying != "" or $selling != "" or $first != "" ) {
$Body .= "\r\n Interested in: ".$buying.$selling.$first;
}

 

i also was using the \r\n instead of the <br /> due to it not putting a return in the actual email it was just displaying <br />

 

Thanks for your continued support

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.