PHP 可以通過POST、GET方法獲取到表單提交的數(shù)據(jù)
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名注冊、網(wǎng)頁空間、營銷軟件、網(wǎng)站建設(shè)、余姚網(wǎng)站維護(hù)、網(wǎng)站推廣。
獲取到的POST、GET是數(shù)組形式的值,需要通過鍵值來詳細(xì)獲取相應(yīng)的值
比如: index.php 頁面
下面是POST方法
form name="form1" method="post" action="index.php"
input type="text" name="contents" value=""
input type="submit" value="提交"
/form
?php
//獲取表單提交的數(shù)據(jù)
$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
//獲取表單提交的數(shù)據(jù)
$contents = $_GET['contents'];
echo $contents;
?
POST相對于GET方法,更好一些,可以提交大量數(shù)據(jù),以及更安全些。
三中接受方式:
$_GET ? ?//get過來的數(shù)據(jù)
$_POST ?//post過來的數(shù)據(jù)
file_get_contents("php://input") ? //接口過來的xml等字符串?dāng)?shù)據(jù)用這個(gè)接
這三個(gè)方法足以接受任何數(shù)據(jù)了,具體你還要百度一下用法
最近有一個(gè)需求,前端向后臺(tái)提交json,后臺(tái)解析并且將提交的值插入數(shù)據(jù)庫中,
難點(diǎn)
1、php解析json(這個(gè)不算難點(diǎn)了,網(wǎng)上實(shí)例一抓一大把)
2、解析json后,php怎樣拿到該拿的值
?php
require
('connect.php');
/*
本例用到的數(shù)據(jù):
post_array={"order_id":"0022015112305010013","buyer_id":"2","seller_id":"1","all_price":"100.00","json_list":[{"product_id":"3","product_number":"3"},{"product_id":"8","product_number":"2"},{"product_id":"10","product_number":"4"}]}
*/
$post_array=$_POST['post_array'];
//--解析Json,獲取對應(yīng)的變量值
$obj=json_decode($post_array,TRUE);
$order_id
=
$obj['order_id'];
$buyer_id
=
$obj['buyer_id'];
$seller_id
=
$obj['seller_id'];
$all_price
=
$obj['all_price'];
$i=0;//循環(huán)變量
//--得到Json_list數(shù)組長度
$num=count($obj["json_list"]);
//--遍歷數(shù)組,將對應(yīng)信息添加入數(shù)據(jù)庫
for
($i;$i$num;$i++)
{
$list_product_id[]=$obj["json_list"][$i]["product_id"];
$list_product_number[]=$obj["json_list"][$i]["product_number"];
$insert_order_product_sql="INSERT
INTO
tbl_order_product
(order_id,product_id,product_number)
VALUES
(?,?,?)";
$result
=
$sqlconn
-
prepare($insert_order_product_sql);
$result
-
bind_param("sss",
$order_id,$list_product_id[$i],$list_product_number[$i]);
$result-execute();
}
//--添加訂單信息
$insert_order_sql="INSERT
INTO
tbl_order
(order_id,buyer_id,seller_id,all_price)
VALUES
(?,?,?,?)";
$result=$sqlconn-prepare($insert_order_sql);
$result-bind_param("ssss",$order_id,$buyer_id,$seller_id,$all_price);
$result-execute();
$result
-
close();
$sqlconn
-
close();
?
投稿者信息
昵稱:
Hola
Email:
jamcistos@outlook.com
在php中,表單POST提交的數(shù)據(jù)是存放在$_POST變量中.$_POST變量是一個(gè)數(shù)組,它是一個(gè)以表單字段名作索引的數(shù)組.比如有以下表單:
form?method="post"
p姓名:input?type="text"?name="name"?value=""/p
p年齡:input?type="text"?name="age"?value=""/p
pinput?type="submit"?value="提交"/p
/form
輸入值后提交,按你的要求,在php層處理輸出提交的內(nèi)容,那么可以這樣:
?php
echo?'你的姓名是:'.$_POST['name'];//其中$_POST['name']中存放的是上面表單名為name的值
echo?'你今年'.$_POST['age'].'歲';//其中$_POST['age']中存放的是上面表單名為age的值
?
如果字段內(nèi)容很多,有時(shí)就可能需要循環(huán)處理.
foreach($_POST?as?$val){
echo?$val;
}
或直接:
print_r($_POST);
當(dāng)然處理或輸出的格式很多,以上只是一個(gè)示例.
標(biāo)題名稱:php數(shù)據(jù)接收數(shù)據(jù)代碼,php怎么接收前端的數(shù)據(jù)
文章鏈接:http://sd-ha.com/article36/phhipg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、云服務(wù)器、企業(yè)網(wǎng)站制作、Google、網(wǎng)站內(nèi)鏈、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)