PHP将文本发送到<input type =“text”/>

时间:2011-04-30 06:25:37

标签: php xhtml

如何将表单中的信息发送到PHP代码块然后再返回文本区域?我找不到这个答案。

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

一个非常基本的例子。

假设您有一个名为index.php的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>

查看本教程的基础知识:http://net.tutsplus.com/articles/news/diving-into-php/

答案 2 :(得分:0)

使用ajax!(并为那个ajax使用jquery!)

假设你有这个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);
    });
});

当您在输入字段上按Enter键时,正在调用请求文件your_php_file.php?param1=val1&param2=val2的ajax函数。使用php的结果,调用回调函数来更新textarea