【问题标题】:Add information to textarea and save it in an array将信息添加到 textarea 并将其保存在数组中
【发布时间】:2015-04-09 11:20:36
【问题描述】:

是否可以在 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

等等。

所以我想要在 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
        }
    });
  });
});

【问题讨论】:

  • 那么,当 textarea 改变时,你希望 PHP 变量 $scriptlauncher 被更新?
  • 感谢您的回复。是的,你明白了!
  • PHP 在 服务器 上运行。 HTML 在 client(浏览器)上运行。您必须将新内容从客户端移动到服务器。这通常使用 AJAX 和 JSON 来完成。
  • @SverriM.Olsen 谢谢,我正在尝试使用 AJAX 并且它正在工作,但无法将变量从 AJAX 发送到 PHP 数组。我认为这部分可以用 Json 完成 :)
  • 任何帮助将不胜感激!

标签: php html css textarea


【解决方案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);

} 

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

【讨论】:

  • 好吧,我需要在不重新加载页面的情况下这样做。所以这变得更加复杂。 : \
  • 这就是 AJAX 的用途,您可以 POST 数据而无需重新加载。 jQuery 让我们更容易做到这一点。我会编辑我的答案来帮助你。编辑:对不起,我不能给你一个 AJAX 例子,整个 StackOverlow 都有很多。 (我现在正在工作,哎呀!)
猜你喜欢
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-25
  • 1970-01-01
  • 2016-07-16
  • 1970-01-01
相关资源
最近更新 更多