Paul
Paul

Reputation: 57

If statement in variable

I've seen many posts with problems like this but i couldn't find a right answer that worked out. The problem is that i have a code that says if ... is empty, make variable ... (see code below). This variable is a form and in this form you have things like name, message etc. What i want is the placeholder of the name, if user is logged in to be his name, which is saved in my database. This is the code:

    $action = isset($_POST["action"]) ? $_POST["action"] : "";
if (empty($action)) { 
    // Send back the contact form HTML
    $output = "<div style='display:none'>
    <div class='contact-top'></div>
    <div class='contact-content'>
        <h1 class='contact-title'>Stuur een bericht voor hulp:</h1>
        <div class='contact-loading' style='display:none'></div>
        <div class='contact-message' style='display:none'></div>
        <br><br><form action='#' style='display:none'>
            <input type='text' id='contact-name' class='contact-input' name='name' tabindex='1001' placeholder='Naam*' /><br><br>
            <input type='text' id='contact-email' class='contact-input' name='email' tabindex='1002' placeholder='Email*' /><br><br>";

And this is what i want but what doesn't work:

<input type='text' id='contact-name' class='contact-input' name='name' tabindex='1001' placeholder=', " if (logged_in() === true) {echo $user_data["name"] } else { echo 'Naam'; } " ,' /><br><br>

Hope someone can help, thanks in advance,

Paul

(naam = name in my language)

Upvotes: 0

Views: 69

Answers (3)

baldrs
baldrs

Reputation: 2161

You forgot to surround your if in <?php tags

 <input type='text' id='contact-name' class='contact-input' name='name' tabindex='1001' placeholder="<?php  if (logged_in() === true) { echo $user_data["name"]; } else { echo 'Naam'; } ?>" /><br><br>

PS Conditions are not working inside strings, so if you need it outside of the html template, you need do it before the string:

$name = 'Naam';
if (logged_in() === true) { $name = $user_data["name"]; }    

$html = "<input type='text' id='contact-name' class='contact-input' name='name' tabindex='1001' placeholder="{$name}" /><br><br>";

Upvotes: 1

Ballantine
Ballantine

Reputation: 338

You have to concatenate the variable you want to put in your string. For example, you can do :

if (User is logged)
    naam = 'USERNAME';
else
    naam = '';

and in your output :

<input type='text' id='contact-name' class='contact-input' name='name' tabindex='1001' placeholder='".$naam."' />

Upvotes: 0

Kylie
Kylie

Reputation: 11749

Just do this at the top of your file

 <?php if(logged_in()==true){ $name = $user_data["name"];} else {$name="Naam*";};?>

Then echo $name in the form...

<input type='text' id='contact-name' class='contact-input' name='name' tabindex='1001' placeholder='<?php echo $name;?>' /><br><br>

Its a little more seperated and cleaner like that

Basically like this...

  $action = isset($_POST["action"]) ? $_POST["action"] : "";
  if (empty($action)) { 
  if(logged_in()==true){ $name = $user_data["name"];} else {$name="Naam*";};
  // Send back the contact form HTML
   $output = "<div style='display:none'>
   <div class='contact-top'></div>
   <div class='contact-content'>
    <h1 class='contact-title'>Stuur een bericht voor hulp:</h1>
    <div class='contact-loading' style='display:none'></div>
    <div class='contact-message' style='display:none'></div>
    <br><br><form action='#' style='display:none'>
        <input type='text' id='contact-name' class='contact-input' name='".$name."' tabindex='1001' placeholder='Naam*' /><br><br>
        <input type='text' id='contact-email' class='contact-input' name='email' tabindex='1002' placeholder='Email*' /><br><br>";

Upvotes: 0

Related Questions