2014-06-27 29 views
0

中發表聲明,我已經看過很多有這樣問題的帖子,但是我找不到正確的答案。 問題是我有一個代碼,說如果...爲空,使變量...(見下面的代碼)。 這個變量是一種形式,在這種形式中,你有像名稱,消息等東西。我想要的是名稱的佔位符,如果用戶登錄是他的名字,它保存在我的數據庫中。這是代碼:如果在變量

$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>"; 

這也是我想要什麼,但什麼行不通:

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

希望有人能幫助,在此先感謝,

保羅

(naam =用我的語言寫的名字)

+0

您shoule加上''在您輸入 – lighter

+0

@LLL我覺得他相當努力去做包含字符串的HTML裏面,而不是模板。 – baldrs

回答

1

你忘了圍繞你的if <?php標籤

<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的條件沒有字符串內工作,所以如果你需要它的HTML模板之外,你需要的字符串之前做到這一點:

$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>"; 
0

只是這樣做在頂部文件

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

然後在表格回聲$名稱...

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

它多了幾分分離式和清潔一樣,

基本上是這樣的......

$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>"; 
0

你必須來連接你想要把你的字符串變量。例如,你可以這樣做:

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

,並在您的輸出:

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