2013-02-25 67 views
1

我沒有經歷過這個,但我認爲我很好。我需要有經驗的人幫忙。認爲這是一個有趣的問題 假設有3個複選框和3個文本框。如果複選框被同時選中作爲選取框顯示多個輸入文本框

<input type="checkbox" name="check[1]" value="1."> 
<textarea name="text[1]" ></textarea> 

<input type="checkbox" name="check[2]" value="2."> 
<textarea name="text[2]" ></textarea> 

<input type="checkbox" name="check[3]" value="3."> 
<textarea name="text[3]" ></textarea> 

,我需要建立一個循環,如果用 if (isset($_POST['check[%]']))檢查添加的所有檢查text[%]文本框值給部分$value

$value需要像一個線路輸出作爲選取框將檢查的foreach check[%]

我認爲它需要$value需要走低谷html_entity_decode但我不確定。 最終輸出是像

$output .= "<font><marquee scrollamount='3' BEHAVIOR=SCROLL DIRECTION="left"> $value"."</marquee>"."</font>"; 
+1

如果複選框可以選中和未選中時,已經被加載TE頁面,作爲你的HTML顯示我。你爲什麼不使用JavaScript來處理這一切? – AmazingDreams 2013-02-25 15:22:43

+0

我希望它是乾淨的HTML和PHP ...但我也想到JavaScript,但你會怎麼做多個輸入顯示爲一個字幕在JavaScript? – levat 2013-02-25 15:33:06

+0

類似於'document.getElementById(「your-marquee」)。innerHTML = marqueetext;'在​​更改每個複選框的值時運行的函數中,檢查選中哪個複選框並用textareas填充'var marqueetext'。編輯:還應該運行onpageload – AmazingDreams 2013-02-25 15:44:05

回答

1

對於初學者來說,對於PHP POST我不會在輸入值的名稱使用方括號。

的index.php:

<form action="test.php" method="POST"> 
    <input type="checkbox" name="check1" value="1."> 
    <textarea name="text1" ></textarea> 
    <input type="checkbox" name="check2" value="2."> 
    <textarea name="text2" ></textarea> 
    <input type="checkbox" name="check3" value="3."> 
    <textarea name="text3" ></textarea> 
    <button type="submit">Submit</button> 
</form> 

test.php的:

$total_checkbox_num = 3; 
    $final_message = ''; 
    if ($_SERVER["REQUEST_METHOD"] == "POST") { 
     foreach($_POST as $key => $value) { 
      if (!is_array($key)) { 
       $_POST[$key] = htmlspecialchars(stripslashes(trim($value))); 
      } 
     } 
     for($i = 1; $i <= $total_checkbox_num; $i++) { 
      if(isset($_POST['check'.$i])) { 
       $final_message .= $_POST['text'.$i]; 
      } 
     } 
     $output = '<font><marquee scrollamount="3" behavior="scroll" direction="left">' . $final_message . '</marquee></font>'; 
     echo $output; 
    } else echo "There was an error"; 
相關問題