Возможно ли это сделать в PHP + CSS? Получил текстовое поле с результатом массива внутри него:Добавить информацию в textarea и сохранить ее в массиве
<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
И так далее.
Так что я хочу, чтобы добавить текст в текстовое поле, а затем, когда нажать на кнопку Submit этот текст будет добавлен в массив $ scriptlauncher []
Если бы я написал «это мой новый текст», то результат текстового поля должны быть:
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
}
});
});
});
Итак, когда изменяется текстовое поле, вы хотите обновить переменную PHP '$ scriptlauncher'? –
Спасибо за ваш ответ. Да, вы поняли! – user3321425
PHP работает на * сервере *. HTML работает на * клиенте * (в браузере). Вы должны перенести новый контент с клиента на сервер. Обычно это делается с использованием AJAX и JSON. –