Html 如何将 textarea 数据发送到另一个页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4915766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I send textarea data to another page?
提问by Vercas
I am new to this kind of things... I need to submit the data in 3 text areas to another page...
How do I do that?
P.S. I would have used inputs, if I could.
我对这种事情很陌生...我需要将 3 个文本区域中的数据提交到另一个页面...
我该怎么做?
PS如果可以的话,我会使用输入。
回答by Filip Krstic
You need to create your page with form... For example call it form.html use this code for example:
您需要使用表单创建您的页面...例如调用它 form.html 使用此代码例如:
<form id="form1" name="form1" method="post" action="acceptpage.php">
<input type="text" name="input" id="textfield" />
<textarea name="text" id="textarea" cols="45" rows="5"></textarea>
<input type="submit" name="button" id="button" value="Send" />
</form>
After that create page called acceptpage.php Put this in between body tags
之后创建名为 acceptpage.php 的页面 把它放在 body 标签之间
<?php
$frominput = $_POST[input]; // Variable who accept your data from input
$fromtextarea = $_POST[text]; // Variable who accept your data from textarea
// You don't need to use variables, but if you starter, easier to understand.
// Do something with your arrived data...
// Stupid, but for short explanation... for example echo it...
echo $fromtextarea;
echo $frominput;
?>
Of course, you can send data with html, but you can't accept data, for accepting data you need a programming language like php. I use php above to show you how it works...
当然可以用html发送数据,但是不能接受数据,接受数据需要php这样的编程语言。我使用上面的 php 向您展示它是如何工作的...
Edit...(I just now saw that you want to you put the data into the database)... Here is code:
编辑...(我刚刚看到您要您将数据放入数据库中)...这里是代码:
<?php
$frominput = mysql_real_escape_string($_POST[input]);
$fromtextarea = mysql_real_escape_string($_POST[text]);
// Do something with your arrived data...
$query = mysql_query("INSERT INTO table (rowforinput, rowfortextarea) VALUES
('$frominput','$fromtextarea')") or die (mysql_error());
?>
As you can see, there are little modifications at variables which accept data, I called mysql_real_escape_string function, to make data more secure. If you want to learn more about mysql_real_escape_string function, mysql_real_escape_string
如您所见,对接受数据的变量几乎没有修改,我称之为 mysql_real_escape_string 函数,以使数据更安全。如果你想了解更多关于 mysql_real_escape_string 函数,mysql_real_escape_string
Don't forget that first you make a connection to the database. For it I usually make my own function:
不要忘记首先要连接到数据库。为此,我通常制作自己的功能:
function connect_db() {
mysql_connect("localhost", "username", "password");
mysql_select_db("database");
}
And call function with <?php connect_db(); ?>
并调用函数 <?php connect_db(); ?>
UPDATE 03.15.2015
2015 年 3 月 15 日更新
A lot of changes since 2011 in PHP. Function mysql_connect
will be deprecated in future. Use mysqli or PDO instead.
自 2011 年以来 PHP 发生了很多变化。mysql_connect
将来会弃用该功能。请改用 mysqli 或 PDO。
Reference:
参考:
MySQL Improved Extension (Mysqli)