我可以在文本字段中添加“保存”/“提交”按钮,以便在单击按钮时文本保存在字段中吗?

时间:2016-03-17 04:30:45

标签: html forms button submit textfield

标题几乎说明了一切。

我可以在文本字段中添加“保存”/“提交”按钮,以便在单击按钮时文本会保存在字段中吗?

我有一个文本字段,但我现在需要的是添加一个按钮,这样当我在文本字段中写完时,按钮“保存”我在文本字段中写的文本。

我不确定如何做到这一点,我一直在寻找答案。 任何帮助将不胜感激。

我的代码目前看起来像这样,(显然我添加的提交/保存按钮只会刷新页面而我会丢失文本字段中的所有文本)

<form class="admin-customer-notes">
<textarea rows="4" cols="50" style="margin-bottom:30px; width: 100%; height: 200px;">
</textarea>
<button type="submit">Save</button>
</form>

1 个答案:

答案 0 :(得分:0)

您需要使用类似.PHP

之类的内容向表单指向服务器端脚本添加操作

或者,您可以使用javascript / cookies保存数据。

或者您可以使用AJAX调用服务器端脚本。

我不确定您熟悉哪种脚本语言,但如果您可以访问PHP,那么您可以做什么......

按如下方式更改表单:

<form class="admin-customer-notes" method="POST" action="/script.php">
<textarea rows="4" cols="50" style="margin-bottom:30px; width: 100%; height: 200px;" name="text">
</textarea>
<button type="submit">Save</button>
</form>

然后在script.php中,您将解析数据并以您想要的任何形式存储它。例如:

的script.php

<?php
$text = $_POST['text'];

// at this point, $text contains the text from the textarea.  You would need to write this to a database or a flat file.

?>

如果您能告诉我们您正在使用哪种脚本语言,我们可以提供更好的示例。如果您只熟悉javascript,则可能需要使用Cookie。