tinwakr Posted January 31, 2012 Share Posted January 31, 2012 Hi Everyone, I have built a website utilizing a PHP abstract Webpage class to create the pages. When creating another class that inherits from Webpage class I rewrite abstract functions, etc. where needed. When instantiating an object of the extended classes I pass the constructor the $page_title, $css_file, $meta_data, $keywords, etc., etc.. I am able to pass html markup in a string variable to a class level "Setter" function to populate a "main area" of the website and all works perfectly. However, I have a contact form which I send the "Submit Message" button click to a PHP function that takes care of making sure the form fields are not empty, then sends email and returns a success message back to the calling page, if they are empty, the function returns what fields are empty(required). If the page includes empty and populated fields I want to put the populated fields into session variables so that I may populate the fields when the user is taken back to the page and display to the user the required fields that were empty(this functionality is already within the function). This brings me to my question(see code below for reference): Seeing that I am populating a variable with a string it seems I cannot use an if statement, echo or the like, my IDE gives me errors. How may I insert an if statement where needed to populate the empty form fields? include('../php_functions/functions.php');//for the chechData() function $error_message = NULL; if(isset($_POST['submit'])) { $error_message = checkData($_POST, 'Renovation & Consulting'); } $main_content = '<form method="post" action=""> <fieldset id="contactform"> <legend>Renovations & Consultations</legend>'. [color=red]//placing a variable here works fine![/color] $error_message .'<div class="row_div"> <div class="label_div"> <label>First Name:</label> </div> <div class="input_div"> <input name="First Name" type="text" maxlength="50" size="50" value="'.[color=red]if(session variable not empty) echo it here;.'"[/color] /> </div> </div> <div class="row_div"> <input name="submit" type="submit" value="Send Message" /> </div> </fieldset> </form>'; Thanks for any and all responses. Chuck Quote Link to comment https://forums.phpfreaks.com/topic/256140-using-if-statements-inside-a-string-variable/ Share on other sites More sharing options...
digibucc Posted January 31, 2012 Share Posted January 31, 2012 you want to echo the whole input if: <?php if(!empty($value)) { echo '<input name="First Name" type="text" maxlength="50" size="50" value="'. $value. '" />'; } else { echo '<input name="First Name" type="text" maxlength="50" size="50" />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/256140-using-if-statements-inside-a-string-variable/#findComment-1313075 Share on other sites More sharing options...
scootstah Posted January 31, 2012 Share Posted January 31, 2012 You can use the ternary operator. $var = '<input name="First Name" type="text" maxlength="50" size="50" value="' . (!empty($var) ? $var : '') . '" />'; Quote Link to comment https://forums.phpfreaks.com/topic/256140-using-if-statements-inside-a-string-variable/#findComment-1313078 Share on other sites More sharing options...
PFMaBiSmAd Posted January 31, 2012 Share Posted January 31, 2012 The only conditional logic that you can place in-line, concatenated as part of a string, is to use the ternary conditional operator - <input name="First Name" type="text" maxlength="50" size="50" value="'.(isset($_SESSION['some_index'])? $_SESSION['some_index']:'').'" /> The ( ) are needed around the ternary expression so that the logic will work (otherwise the ternary will use the string up to that point to determine if the logic is true/false.) Quote Link to comment https://forums.phpfreaks.com/topic/256140-using-if-statements-inside-a-string-variable/#findComment-1313079 Share on other sites More sharing options...
digibucc Posted January 31, 2012 Share Posted January 31, 2012 i was just reading about that today... understand it but guess i haven't learned it yet huh Quote Link to comment https://forums.phpfreaks.com/topic/256140-using-if-statements-inside-a-string-variable/#findComment-1313081 Share on other sites More sharing options...
tinwakr Posted January 31, 2012 Author Share Posted January 31, 2012 Thanks to everyone who replied, I got it working like this: $middle_column = '<form method="post" action=""> <fieldset id="contactform"> <legend>Renovations & Consultations</legend><span id=errors>'. $error_message .'</span><div class="row_div"> <div class="label_div"> <label>First Name:</label> </div> <div class="input_div">'; if(!empty($_SESSION['First_Name'])) { $middle_column .= '<input name="First Name" type="text" maxlength="50" size="50" value="'.$_SESSION['First_Name'].'" />'; } else { $middle_column .= '<input name="First Name" type="text" maxlength="50" size="50" />'; } $middle_column .= '</div> </div> </fieldset> </form>'; I had to do it this way and not use an echo statement because I was passing this string variable to the object(class) so I had to concatenate it to the variable. Thanks again everyone! Chuck Quote Link to comment https://forums.phpfreaks.com/topic/256140-using-if-statements-inside-a-string-variable/#findComment-1313127 Share on other sites More sharing options...
scootstah Posted January 31, 2012 Share Posted January 31, 2012 Did you even try my code? You can make that much cleaner like this: $middle_column = '<form method="post" action=""> <fieldset id="contactform"> <legend>Renovations & Consultations</legend><span id=errors>'. $error_message .'</span><div class="row_div"> <div class="label_div"> <label>First Name:</label> </div> <div class="input_div"><input name="First Name" type="text" maxlength="50" size="50" value="' . (!empty($_SESSION['First_Name']) ? $_SESSION['First_Name'] : '') . '" /></div> </div> </fieldset> </form>'; Quote Link to comment https://forums.phpfreaks.com/topic/256140-using-if-statements-inside-a-string-variable/#findComment-1313131 Share on other sites More sharing options...
tinwakr Posted January 31, 2012 Author Share Posted January 31, 2012 Much cleaner scootstah but I had to try and do it myself in a way that I understand, if I just use someone's code and I don't understand it I will never learn. Thanks buddy for all your help I really appreciate it. Chuck Quote Link to comment https://forums.phpfreaks.com/topic/256140-using-if-statements-inside-a-string-variable/#findComment-1313135 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.