将信息添加到textarea并将其保存在数组中

时间:2015-04-09 11:20:37

标签: php html css textarea

是否可以在PHP + CSS中执行此操作?得到一个textarea,里面有一个数组的结果:

<textarea class="ta_scripts"><?php
  foreach ($script as $script_launcher){
       echo $script_launcher."\r\n";
  }?></textarea><br><br>

下面的代码显示了一个类似这样的textarea:

This is a beautiful line 1 from the array
This is a beautiful line 2 from the array
This is a more beautiful line 3 from the array

等等。

所以我想要的是在textarea中添加一些文本,然后当点击一个提交按钮时,这个文本将被添加到数组$ scriptlauncher []

如果我写了“这是我的新文本”,那么textarea的结果必须是:

This is a beautiful line 1 from the array
This is a beautiful line 2 from the array
This is a more beautiful line 3 from the array
this is my new text

我的第一个方法: HTML和PHP并调用Javascript

<form name="frmscript" onsubmit="return false;">

        <?php if (isset($_POST["script"])) {

            $script = $_POST["script"];

          }?>

        <textarea class="ta_scripts" name="ta_scripts"><?php

                          foreach ($script as $script_launcher){
                              echo $script_launcher."\r\n";
                          }?>
        </textarea><br><br>

<input type="button" name="execscript" value="Execute scripts" id="submit" onClick="addtext();" />
</form>

使用Javascript:

function addtext() {
    var newtext = document.frmscript.ta_scripts.value;
    document.frmscript.ta_scripts.value = "";
    document.frmscript.ta_scripts.value += newtext;
    window.location.href = "index.php?script=" + newtext; 
}

第二种方法(无法保存新阵列) HTML和PHP并调用Javascript:

<form name="frmscript" onsubmit="return false;">
                    <?php /*if (isset($_POST["script"])) {*/
                    if($_POST){

                    $script = json_decode($_POST["script"]);

                    }

                    ?>

                    <textarea class="ta_scripts" name="ta_scripts"><?php

                          foreach ($script as $script_launcher){
                              echo $script_launcher."\r\n";
                          }?></textarea><br><br>

                  <input type="button" name="execscript" value="Execute scripts" id="submit" onClick="addtext();" />
                    </form>

使用Javascript:

function addtext() {
  var script = document.frmscript.ta_scripts.value;
  document.frmscript.ta_scripts.value = "";
  document.frmscript.ta_scripts.value += script;
  script = JSON.encode(script);



  var miAjax = new Request({
      url: "index4.php",
      data: "script=" + script,
      onSuccess: function(textResponse){
      $('result').set("html", textResponse);
  },
  onFailure: function(){
    $('result').set("html", "Error!!");
}
})
  miAjax.send();

第三种方法(非常感谢任何帮助!):

HTML和PHP:

<form name="frmscript" onsubmit="return false;">
                    <?php /*if (isset($_POST["script"])) {*/
                    if(isset($_GET['script'])){

                    $script = $_GET['script'];

                    }

                    ?>

                    <textarea class="ta_scripts" name="ta_scripts"><?php

                          foreach ($script as $script_launcher){
                              echo $script_launcher."\r\n";
                          }?></textarea><br><br>

                  <input type="button" name="execscript" value="Exec script" id="submit" onClick="addtext();" />
                    </form>

使用Javascript:

 $(document).ready(function () {
$('#execscript').click(function () {
/*        var name_val = $('input[name="script"]').val();*/
var script = document.frmscript.ta_scripts.value;

    $.ajax({
        type: "GET",
        url: "index4.php", //get response from this file
        data: {
            name: script
        },
        success: function (response) {

            $("textarea#ta_scripts").val(response); //send response to textarea
        }
    });
  });
});

1 个答案:

答案 0 :(得分:1)

我不确定我是否完全理解你,但为了将行读入数组:

$text = trim($_POST['testtextarea']);
$textAr = explode("\n", $text);
$textAr = array_filter($textAr, 'trim'); // remove any extra \r characters left behind

foreach ($textAr as $line) {
   //Push to scriptlauncher array.
    array_push($scriptlauncher, $line);

} 

我没有对此进行测试,但它应该让你走上正轨。