PHP示例代码:
<?php
$funtype=$_POST['funtype'];
switch($funtype)
{
case 'returnvalue':
echo "value: $value" ;
break;
case 'searchMessage':
LoginMySql() ;
searchMessage() ;
break;
case 'InsertMessage':
LoginMySql();
InsertMessage();
break;
case 'UpdateMessage':
LoginMySql();
UpdateMessage();
break;
case 'DeleteMessage':
LoginMySql();
DeleteMessage();
break;
}
function LoginMySql()
{
$host = 'localhost';
$database = 'tablename';
$username = 'xxxxxx';
$password = '********';
$connection = mysql_connect($host, $username, $password);//连接到数据库
mysql_query("set names 'utf8'");//编码转化
if (!$connection) {
die("could not connect to the database.\n" . mysql_error());//诊断连接错误
}
$selectedDb = mysql_select_db($database);//选择数据库
if (!$selectedDb) {
die("could not to the database\n" . mysql_error());
}
//echo "ok";
}
function searchMessage()
{
$selectName = $_POST['value'];
$selectName = mysql_real_escape_string($selectName);//防止SQL注入
$query = "select * from buycount where id = '$selectName'";//构建查询语句
$result = mysql_query($query);//执行查询
if (!$result) {
die("could not to the database\n" . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
//取出结果并显示
$clothid = $row["clothid"];
$price = $row["price"];
$resultvalue = "{\"clothid\":\"$clothid\"}";
echo "$resultvalue";
}
//while ($row = mysql_fetch_row($result)) {
//取出结果并显示
//$id = $row[0];
//$buyurl = $row[1];
//$buystr = $row[2];
//$resultvalue = "{\"buyurl\":\"$buyurl\",\"buystr\":\"$buystr\"}";
//echo "$resultvalue";
//}
}
function InsertMessage()
{
$InsertValue = $_POST['value'];
$json_InsertValue=json_decode($InsertValue);
$infostr = $json_InsertValue->info;
$ipstr = getIP();
$datetimestr =date("Y-m-d H:i:s");
$portstr = $_SERVER['REMOTE_PORT'];//端口号
$insert = "INSERT INTO couponct (datetime,ip,port,info) VALUES ('$datetimestr ','$ipstr','$portstr ','$infostr')";//构建插入语句
$resultInsert = mysql_query($insert);//执行插入
if (!$resultInsert) {
die("could not to the database\n" . mysql_error());
}else{
echo "InsertOK";
}
}
function UpdateMessage()
{
$UpdateValue = $_POST['value'];
$json_InsertValue=json_decode($UpdateValue);
$id = $json_InsertValue->id; //prints foo
$buyurl = $json_InsertValue->buyurl; //prints foo
$buystr = $json_InsertValue->buystr;
mysql_query("update buycount SET buyurl = '$buyurl' WHERE buystr = '$buystr' ") or die('set Error: ' . mysql_error());
echo "UpdateOK";
}
function DeleteMessage()
{
$DeleteValue = $_POST['value'];
$json_InsertValue=json_decode($DeleteValue);
$id = $json_InsertValue->id; //
//删除 fname='wenjian' and lname='31'
mysql_query("delete from buycount where id='$id' ") or die('delete Error: ' . mysql_error());
echo "DeleteOK";
}
function getIP() {
if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
}elseif (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
}elseif (getenv('HTTP_X_FORWARDED')) {
$ip = getenv('HTTP_X_FORWARDED');
}elseif (getenv('HTTP_FORWARDED_FOR')) {
$ip = getenv('HTTP_FORWARDED_FOR');
}elseif (getenv('HTTP_FORWARDED')) {
$ip = getenv('HTTP_FORWARDED');
}else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
?>
html 向php通过表单提交数据且不打开php页面(需jquery.js)
function tophp(curtype)
{
var userName = getQueryString("uid");
if(userName ==""){
userName = "null";
}
$poststring = "uid="+ userName +"&type="+ curtype +"&info="+ "null";
$.ajax({
//alert("aaaaaaaaaaa"); //拿到结果
type: "post",
url: "http://xxx/aaa.php",
//data: "name=" + $("#你要传输的文本框id").val(),
data: $poststring,
success: function (result) {
$("#returncontent").html(result);
// alert(result.msg); //拿到结果
}
});
}
aaa.php 取数据代码:
$uidstr = $_POST['uid'];
$typestr =$_POST['type'];
$infostr = $_POST['info'];