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: larry@skbk.com

 

This is not filled out-

Name: Larry Boy

 

Email: larry@skbk.com

 

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
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: yes@test.com

 

No Phone

Name: Joe Test

Email: yes@test.com

 

No Email

Name: Joe Test

Phone: 123-456-7890

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.