這需要用ajax來實現(xiàn)
創(chuàng)新互聯(lián)公司主要從事網(wǎng)站制作、成都網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)梁河,10余年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220
index.php
html
titlephp+jquery+ajax+json簡單小例子/title
?php
header("Content-Type:text/html;charset=utf-8");
?
head
script?type="text/javascript"?src="
script?type="text/javascript"
$(function()?{
$("#subbtn").click(function()?{
var?params?=?$("input").serialize();
var?url?=?"1.php";
$.ajax({
type:?"post",
url:?url,
dataType:?"json",
data:?params,
success:?function(msg){
var?backdata?=?"您提交的姓名為:"?+?msg.name?+
"br?/?您提交的密碼為:"?+?msg.password;
$("#backdata").html(backdata);
$("#backdata").css({color:?"green"});
}
});
});
});
/script
/head
body
plabel?for="name"姓名:/label
input?id="name"?name="name"?type="text"?/
/p
plabel?for="password"密碼:/label
input?id="password"?name="password"?type="password"?/
/p
span?id="backdata"/span
pinput?id="subbtn"?type="button"?value="提交數(shù)據(jù)"?//p
/body
/html
1.php代碼:
?php
//接收數(shù)據(jù)-處理數(shù)據(jù)-返回數(shù)據(jù)
echo?json_encode($_POST);
?
這個可以通過PHP的curl請求來提交數(shù)據(jù)到其他的網(wǎng)站!獲取執(zhí)行的結(jié)果即可!以下是一個示例。
function?curlpost($data,$url){????????????????????//curlpost提交函數(shù)
$data=array('data'=$data);
$ch?=?curl_init?();
curl_setopt?($ch,?CURLOPT_URL,?$url?);
curl_setopt?($ch,?CURLOPT_POST,?true?);
curl_setopt?($ch,?CURLOPT_HEADER,?0?);
curl_setopt?($ch,?CURLOPT_RETURNTRANSFER,?1?);
curl_setopt?($ch,?CURLOPT_POSTFIELDS,?$data?);
$state=curl_exec?($ch?);
curl_close?($ch?);
return?$state;
}
/*server.c*/
#include string.h
#include stdio.h
#include netinet/in.h
#include stdlib.h
#define SERV_PORT 8000
int main()
{
int sockfd,n,servaddr_len,client_len,connfd;
char * server_ip = "127.0.0.1";
char buf[80];
struct sockaddr_in server,client;
sockfd = socket (AF_INET,SOCK_STREAM,0);
bzero(server,sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(server_ip);
server.sin_port = htons(SERV_PORT);
servaddr_len = sizeof(server);
client_len=sizeof(client);
bind(sockfd,(struct sockaddr *) server,servaddr_len);
listen(sockfd,6);
while(1)
{
bzero(buf,sizeof(buf));
printf("等待讀取數(shù)據(jù).......\n");
connfd=accept(sockfd,(struct sockaddr *)client,client_len);
n = read(connfd,buf,sizeof(buf));
write(connfd,buf,n);
printf("讀取的數(shù)據(jù)為:%s\n",buf);
}
close(sockfd);
return 0;
}
?php
$server_ip="127.0.0.1";
$port = 8000;
if($_POST['submit'])
{
$buf=trim($_POST['buf']);
$sockfd=socket_create(AF_INET,SOCK_STREAM,SOL_TCP)or die("創(chuàng)建失敗");
socket_connect($sockfd,$server_ip,$port)or die("連接錯誤");
socket_write($sockfd,$buf,strlen($buf))or die("寫入錯誤");
$a=socket_read($sockfd,80);
echo $a;
$buf="";
socket_close($sockfd);
}
?
form action="tcpclient.php" method="post"
請選則發(fā)送方式:brinput type=text name="buf"
input type=submit name="submit" value="發(fā)送"
/form
tcp方式不能通信解決方法
執(zhí)行:getsebool -a |grep http
查看httpd_can_network_connect -- off 是否為打開狀態(tài)
執(zhí)行:setsebool httpd_can_network_connect on 把httpd_can_network_connect設(shè)置為打開狀態(tài)
tcp方式不能通信解決方法
執(zhí)行:getsebool -a |grep http
查看httpd_can_network_connect -- off 是否為打開狀態(tài)
執(zhí)行:setsebool httpd_can_network_connect on 把httpd_can_network_connect設(shè)置為打開狀態(tài)