2011-04-30 162 views
0

如何將信息從表單發送到一段PHP代碼,然後返回到文本區域?我找不到這個答案。PHP將文本發送到<input type =「text」>

+0

你的問題是非常非常基本的。我的建議是遵循這個教程:http://net.tutsplus.com/articles/news/diving-into-php/它會非常有幫助 – JohnP 2011-04-30 06:29:03

+0

@JohnP我知道,但我想要做的是顯示我的導致一個文本框,但我讀過或去過的所有基本教程都沒有顯示,他們只是說回聲「要記錄的東西」; – daddycardona 2011-04-30 06:34:06

+0

增加了一個例子 – JohnP 2011-04-30 06:44:14

回答

0

一個非常基本的例子。

假設你有一個名爲index.php的

<?php 
    $val = 'Nothing in POST'; 
    if (!empty($_POST)) { //$_POST is where stuff posted from the FORM is saved 
    $val = isset($_POST['text']) ? $_POST['text'] : '';//you're looking for the data with key text which is the name of your textarea element 
    $val = 'Got something from POST : ' . $val; 
    } 
?> 


<form action='index.php' method='post'> 
    <textarea name='text'><?php echo $val ?></textarea> 
</form> 

一個PHP文件,看看本教程爲基礎:http://net.tutsplus.com/articles/news/diving-into-php/

+0

這就是我所說的,我想把兩個值加在一起,並將我的結果發佈到文本區域。 – daddycardona 2011-04-30 06:40:31

+0

@daddycardona我更新了我的答案。這應該這樣做 – JohnP 2011-04-30 06:43:02

+0

是你這些教程是非常糟糕的屁股到目前爲止感謝您的網站。 – daddycardona 2011-04-30 06:43:13

5

要打印出你在文本框輸入的下一個請求文本應該是這樣的假設您呈現相同的頁面(即myform.php):

<?php 
    $fieldValue = htmlentities($_POST['myfield']); 
?> 

<form action="myform.php" method="post"> 
    <label for="myfield">Your textfield:</label> 
    <input type="text" name="myfield" id="myfield" value="<?php echo $fieldValue; ?>" /> 
</form> 
+0

不要忘記用htmlspecialchars轉義$ textfieldvalue – 2011-04-30 07:39:15

+0

所以我不會把名字改爲部分ID嗎? – daddycardona 2011-04-30 11:06:59

+0

@EmilVikström:是的,很好的指出,但我認爲它已超出了給定問題的範圍。 – stefanglase 2011-04-30 14:00:45

0

!用ajax(!和使用jquery對於AJAX)

假設你有這樣的HTML:

<input type="text" id="input"> 
<textarea id="result"></textarea> 

不是腳本應該是:

$('#input').keypress(function(e){ 
    if(e.wich != 13)//not enter 
     return; 
    $.get(your_php_file.php,{param1:val1,param2:val2},function(result){ 
     $('#result').val(result); 
    }); 
}); 

當你點擊輸入字段中輸入,正在調用的ajax函數請求文件your_php_file.php?param1=val1&param2=val2。隨着PHP的結果,回調函數被稱爲更新你的textarea

相關問題