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

标签 php html css textarea

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

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

下面的代码显示了一个类似这样的文本区域:

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

等等。

所以我想要的是在文本区域内添加一些文本,然后当单击提交按钮时,该文本将被添加到数组 $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
        }
    });
  });
});

最佳答案

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

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

} 

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

关于php - 将信息添加到textarea并保存在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29537119/

相关文章:

php - 在 symfony 中丢失经过身份验证的 token

php - 我怎样才能从 web 表单正确设置 PHP 邮寄文本的样式?

jquery - 全屏动画背景

javascript - 从右侧而不是左侧的侧边栏导航

javascript - 如何将 div 标签放在 Canvas 上

php - 仅使用 PHP 的虚拟页面?

javascript - 显示另一个选择标签,其中包含所选群组名称中的联系号码

html - div 元素的正确 z-index

css - 为什么我的 <div> 消失在另一个后面?

javascript - GSAP Javascript if else 语句问题