Travist6983 Posted July 26, 2010 Share Posted July 26, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/208924-if-variable-blank-display-nothing/ Share on other sites More sharing options...
aleX_hill Posted July 26, 2010 Share Posted July 26, 2010 What is the problem? The blank line if the results are not filled out? I would do something like this: if($Tel != "") { $Body .= "Tel:".$Tel; } and leave it at that, then add the email after the if statement. Quote Link to comment https://forums.phpfreaks.com/topic/208924-if-variable-blank-display-nothing/#findComment-1091305 Share on other sites More sharing options...
Travist6983 Posted July 26, 2010 Author Share Posted July 26, 2010 I defiantly appreciate the advice and the efficiency upgrade to the code butit doesnt get the results that i am looking for... i am trying to eliminate the extra space... and again i know this is stupid but it is what my boss is looking for Quote Link to comment https://forums.phpfreaks.com/topic/208924-if-variable-blank-display-nothing/#findComment-1091354 Share on other sites More sharing options...
hcdarkmage Posted July 26, 2010 Share Posted July 26, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/208924-if-variable-blank-display-nothing/#findComment-1091384 Share on other sites More sharing options...
Travist6983 Posted July 26, 2010 Author Share Posted July 26, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/208924-if-variable-blank-display-nothing/#findComment-1091408 Share on other sites More sharing options...
hcdarkmage Posted July 26, 2010 Share Posted July 26, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/208924-if-variable-blank-display-nothing/#findComment-1091411 Share on other sites More sharing options...
Travist6983 Posted July 26, 2010 Author Share Posted July 26, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/208924-if-variable-blank-display-nothing/#findComment-1091413 Share on other sites More sharing options...
hcdarkmage Posted July 26, 2010 Share Posted July 26, 2010 I usually say, "If it works, don't complain." That way you don't over complicate things . There may be a better way to write the code, but if it works then go with it. Quote Link to comment https://forums.phpfreaks.com/topic/208924-if-variable-blank-display-nothing/#findComment-1091417 Share on other sites More sharing options...
Travist6983 Posted July 27, 2010 Author Share Posted July 27, 2010 Yeah i guess i cant disagree with you... though i know that my coding techniques might not always be the most efficient... but thank you for your more indepth help... Quote Link to comment https://forums.phpfreaks.com/topic/208924-if-variable-blank-display-nothing/#findComment-1091665 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.