一般是用post獲取提交的數據,如下實例:
創新互聯主營涪城網站建設的網絡公司,主營網站建設方案,app開發定制,涪城h5成都小程序開發搭建,涪城網站營銷推廣歡迎涪城等地區企業咨詢
form?name="form1"?method="post"
p用戶名:input?type="text"?name="uname"?//p
p密碼:input?type="password"?name="upwd"?//p
pinput?type="submit"?name="btn"?value="提交"?//p
?php
if?($_POST["btn"]){
echo?'用戶名:'.$_POST["uname"].'br';//三體教程
echo?'密碼:'.$_POST["upwd"];
}
?
/form
PHP 可以通過POST、GET方法獲取到表單提交的數據
獲取到的POST、GET是數組形式的值,需要通過鍵值來詳細獲取相應的值
比如: index.php 頁面
下面是POST方法
form name="form1" method="post" action="index.php"
input type="text" name="contents" value=""
input type="submit" value="提交"
/form
?php
//獲取表單提交的數據
$contents = $_POST['contents'];
echo $contents;
?
也可以是下面是GET方法
form name="form1" method="get" action="index.php"
input type="text" name="contents" value=""
input type="submit" value="提交"
/form
?php
//獲取表單提交的數據
$contents = $_GET['contents'];
echo $contents;
?
POST相對于GET方法,更好一些,可以提交大量數據,以及更安全些。
這個技術稍微綜合了PHP的基礎知識,
給你一個思路,
(1)
先將textarea
文本中的信息
傳入
php的
$_POST['content'],
content
是textarea的屬性名稱,
(2)
傳過來的值是通過數組的形式進行保存的
,其中PHP有一個函數是可以將數組轉換成字符串形式,
引用那個函數后,通過var_dump()打印出你的轉換數據,看是否是字符串
在這里需要提醒你一下,因為你是每一行作為一句話
通過逗號分隔出來的
,那么
在轉換成數組的時候,
將每一行數據
用
|
隔開,例如:
數據1
,
數據11,
數據111
|
數據2,
數據22,
數據222|
數據3
,
數據33,
數據333
|
數據4,
數據44,
數據444
|
這就是一個轉換成字符串的格式了,
(3)
通過轉換成字符串后,php中還有一個函數就是將字符串轉換成
數組的函數,轉換結果應該出來的數據格式是:
array=
array(0)=array{
'數據1,數據11,數據111'
},
array(1)=array{
'數據2,數據22,數據222'
}....
(4)以上的數據都是索引數組的二維數組,將二維數組用foreach()去循環打印出來,那么久可以得到每一個
所以數組下的
數據了,這些數據
就是你要保存到數據的數據,在按照(1)和(2)的方式進行操作,最后就可以把textarea的數據保存到數據庫中咯。
思路就是這樣的
,希望你能自己動手,把這個程序解決,這個程序在實際開發中運用的很廣泛,最好自己把它掌握了.....
1:首先要使用PHP的超全局變量 $_GET 和 $_POST 用于收集表單數據(form-data) 2:然后使用INSERT INTO 語句用于向數據庫表中插入新記錄。 具體示例: (1)首先創建了一個名為 "Persons" 的表,有三個列:"Firstname", "Lastname" 以及 "Age"。
在獲取表單數據中,最常用的自動全局變量是$_GET和$_POST,它們分別獲取通過GET方法提交的數據和通過POST方法提交的數據。
比如一個名稱為"user"的文本框表單控件,如果用GET方法提交,可以用 $_GET["user"]或者$_GET['user']
獲取它提交的值。
一、用file_get_contents以get方式獲取內容,需要輸入內容為:
1、?php
2、$url='';
3、$html = file_get_contents($url);
4、echo $html;
5、?
二、用file_get_contents函數,以post方式獲取url,需要輸入內容為
1、?php
2、$url = '';
3、$data = array ('foo' = 'bar');
4、$data = http_build_query($data);
5、$opts = array (
6、'http' = array (
7、 ? 'method' = 'POST',
8、? 'header'= "Content-type: application/x-www-form-urlencoded\r\n" .
9、 ? ? ? ? ? ? ? ? ? ? "Content-Length: " . strlen($data) . "\r\n",
10、 ? 'content' = $data
11、)
12、);
13、$ctx = stream_context_create($opts);
14、$html = @file_get_contents($url,'',$ctx);
15、?
三、用fopen打開url,以get方式獲取內容,需要輸入內容為
1、?php
2、$fp = fopen($url, 'r');
3、$header = stream_get_meta_data($fp);//獲取信息
4、while(!feof($fp)) {
5、$result .= fgets($fp, 1024);
6、}
7、echo "url header: {$header} br":
8、echo "url body: $result";
9、fclose($fp);
10、?
四、用fopen打開url,以post方式獲取內容,需要輸入內容為
1、?php
2、$data = array ('foo2' = 'bar2','foo3'='bar3');
3、$data = http_build_query($data);
4、$opts = array (
5、'http' = array (
6、'method' = 'POST',
7、'header'= "Content-type: application/x-www-form-urlencoded\r\nCookie:cook1=c3;cook2=c4\r\n" .
8、"Content-Length: " . strlen($data) . "\r\n",
9、'content' = $data
10、)
11、);
12、$context = stream_context_create($opts);
13、$html = fopen(';id2=i4','rb' ,false, $context);
14、$w=fread($html,1024);
15、echo $w;
16、?
五、用fsockopen函數打開url,以get方式獲取完整的數據,包括header和body,需要輸入內容為
1、?php
2、function get_url ($url,$cookie=false)
3、{
4、$url = parse_url($url);
5、$query = $url[path]."?".$url[query];
6、echo "Query:".$query;
7、$fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30);
8、if (!$fp) {
9、return false;
10、} else {
11、$request = "GET $query HTTP/1.1\r\n";
12、$request .= "Host: $url[host]\r\n";
13、$request .= "Connection: Close\r\n";
14、if($cookie) $request.="Cookie:?? $cookie\n";
15、$request.="\r\n";
16、fwrite($fp,$request);
17、while(!@feof($fp)) {
18、$result .= @fgets($fp, 1024);
19、}
20、fclose($fp);
21、return $result;
22、}
23、}
24、//獲取url的html部分,去掉header
25、function GetUrlHTML($url,$cookie=false)
26、{
27、$rowdata = get_url($url,$cookie);
28、if($rowdata)
29、{
30、$body= stristr($rowdata,"\r\n\r\n");
31、$body=substr($body,4,strlen($body));
32、return $body;
33、}
34、 ? return false;
35、}
36、?
參考資料:
php-file_get_contents