Jump to content

using if statements inside a string variable????


tinwakr

Recommended Posts

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

Link to comment
Share on other sites

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" />';
}
?>

Link to comment
Share on other sites

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.)

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>';

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.