【发布时间】: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 完成 :)
-
任何帮助将不胜感激!