包含ajaxphpmysql的词条

发布时间:2022-11-16

本文目录一览:

  1. [PHP + ajax + Mysql的原理是怎么样的?](#PHP + ajax + Mysql的原理是怎么样的?)
  2. php+mysql+ajax乱码。。。
  3. [php+Ajax+mysql 中文乱码问题](#php+Ajax+mysql 中文乱码问题)
  4. 在php中用ajax如何操作mysql啊,求助啊。谢谢啦啊
  5. 关于php+mysql+ajax省市区三级联动菜单,求帮助

PHP + ajax + Mysql的原理是怎么样的?

AJAX就是一种网页和后台服务器交流的协议,一个简单的AJAX发送代码:

$.post{
  '服务器的网址',
  '{data: '这里是要发送的数据'}',
  function(return){
    // return 是后台服务器接收到你网页发的数据后返回的数据
  }
}

PHP的echo输出的结果就是返回网页的return数据,格式是直接做返回值。PHP要返回数组就要用json封装,代码是json_encode(数组);之后网页接受的是json格式的字符串,要自己转换为json数组。

php+mysql+ajax乱码。。。

如果数据库的字符集设置的是gb2312,其余的编码也需要是gb2312。这个看起来像是最后一个php文件编码不对造成的。可以考虑直接请求一下最后一个php文件,看看返回是不是正确。顺便看一下返回的字符集是什么的,最后能在指定对应的Html也是gb2312的。

php+Ajax+mysql 中文乱码问题

utf-8改成gb2312。数据库中有文字的字段的编码方式也改成中文的就ok!

在php中用ajax如何操作mysql啊,求助啊。谢谢啦啊

1. 静态网页文件 index.html

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  <title>无标题文档</title>
  <script src="selecttest.js"></script>
</head>
<body>
  <form>
    请选择一个:
    <select name="test" onChange="showtest(this.value)">
      <option value="a">test1</option>
      <option value="b">test2</option>
      <option value="c">test3</option>
    </select>
  </form>
  <p>
    <h2><div id="txtdisplay"><b>测试信息将在此处列出。</b></div></h2>
  </p>
</body>
</html>

2. ajax文件(selecttest.js脚本):

var xmlHttp
function showtest(str) {
  xmlHttp = GetXmlHttpObject();
  if (xmlHttp == null) {
    alert("您的浏览器不支持AJAX!");
    return;
  }
  var url = "gettest.php";
  url = url + "?q=" + str;
  url = url + "sid=" + Math.random();
  xmlHttp.onreadystatechange = stateChanged;
  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
}
function stateChanged() {
  if (xmlHttp.readyState == 4) {
    document.getElementById("txtdisplay").innerHTML = xmlHttp.responseText;
  }
}
function GetXmlHttpObject() {
  var xmlHttp = null;
  try {
    xmlHttp = new XMLHttpRequest();
  } catch (e) {
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}

3. 后台处理文件 gettest.php

<?php
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
mysql_connect("localhost", "root", "password") or die("连接失败");
mysql_select_db(ajax);
mysql_query("set names gbk");
$q = $_GET['q'];
$sql = "SELECT * FROM test WHERE testID='".$q."'";
$result = mysql_query($sql);
if ($result) {
  echo ("<table>");
  while ($arr = mysql_fetch_array($result)) {
    $name = $arr['name'];
    $value = $arr['value'];
    echo ("<tr><td><em>" . $name . "</em></td>");
    echo ("<td>" . $value . "</td></tr>");
  }
  echo ("</table>");
} else {
  echo "对不起,没有查找到!";
}
?>

以上三个文件可以放到同一文件夹里。试试吧。

关于php+mysql+ajax省市区三级联动菜单,求帮助

基本思想就是:在JS动态创建select控件的option,通过Ajax获取在PHPSQL数据库获取的省市区信息,代码有点长,但很多都是类似的,例如JS中省、市、区获取方法类似,PHP中通过参数不同执行不同的select语句。

index.html代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head>
  <title>省市区三级联动</title>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  <script src="scripts/thumbnails.js" type="text/javascript"></script>
</head>

thumbnails.js代码:

window.onload = getProvince;
function createRequest() {
  try {
    request = new XMLHttpRequest();
  } catch (tryMS) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (otherMS) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = null;
      }
    }
  }
  return request;
}
function sech(id) {
  var aa = document.getElementById(id);
  if (id == "sheng") {
    getCity(aa.value);
  }
  if (id == "shi") {
    getCounty(aa.value);
  }
}
function getProvince() {
  request = createRequest();
  if (request == null) {
    alert("Unable to create request");
    return;
  }
  var url = "getDetails.php?ID=0";
  request.open("GET", url, true);
  request.onreadystatechange = displayProvince;
  request.send(null);
}
function getCity(id) {
  request = createRequest();
  if (request == null) {
    alert("Unable to create request");
    return;
  }
  var url = "getDetails.php?ID=" + escape(id);
  request.open("GET", url, true);
  request.onreadystatechange = displayCity;
  request.send(null);
}
function getCounty(id) {
  request = createRequest();
  if (request == null) {
    alert("Unable to create request");
    return;
  }
  var url = "getDetails.php?ID=" + escape(id);
  request.open("GET", url, true);
  request.onreadystatechange = displayCounty;
  request.send(null);
}
function displayProvince() {
  if (request.readyState == 4) {
    if (request.status == 200) {
      var a = new Array;
      var b = request.responseText;
      a = b.split(",");
      document.getElementById("sheng").length = 1;
      var obj = document.getElementById("sheng");
      for (i = 0; i < a.length; i++) {
        obj.options.add(new Option(a[i], i + 1));
      }
    }
  }
}
function displayCity() {
  if (request.readyState == 4) {
    if (request.status == 200) {
      var a = new Array;
      var b = request.responseText;
      a = b.split(",");
      document.getElementById("shi").length = 1;
      document.getElementById("xian").length = 1;
      if (document.getElementById("sheng").value != "province") {
        var obj = document.getElementById('shi');
        for (i = 0; i < a.length; i++) {
          obj.options.add(new Option(a[i], document.getElementById("sheng").value * 100 + i + 1));
        }
      }
    }
  }
}
function displayCounty() {
  if (request.readyState == 4) {
    if (request.status == 200) {
      var a = new Array;
      var b = request.responseText;
      a = b.split(",");
      document.getElementById("xian").length = 1;
      if (document.getElementById("sheng").value != "province" && document.getElementById("shi").value != "city") {
        var obj = document.getElementById('xian');
        for (i = 0; i < a.length; i++) {
          obj.options.add(new Option(a[i], i + 1001));
        }
      }
    }
  }
}

getDetails.php代码:

<?php
header("Content-Type: text/html; charset=gb2312");
$conn = new COM("ADODB.Connection") or die("Cannot start ADO");
$connstr = "Provider=SQLOLEDB;Persist Security Info=False;User ID=root;Password=123456;Initial Catalog=area;Data Source=localhost";
if ($_REQUEST['ID'] == 0) {
  $conn->Open($connstr);
  $sqlstr = "select name from Province";
  $rs = $conn->Execute($sqlstr);
  $num_cols = $rs->Fields->Count();
  $Province = array();
  $i = 0;
  while (!$rs->EOF) {
    $Province[$i] = $rs->Fields['name']->Value . ",";
    $rs->MoveNext();
    $i++;
  }
  foreach ($Province as $val) {
    echo $val;
  }
  $conn->Close();
  $rs = null;
  $conn = null;
}
if ($_REQUEST['ID'] > 0 && $_REQUEST['ID'] <= 35) {
  $conn->Open($connstr);
  $sqlstr = "select name from City where cid=" . $_REQUEST['ID'];
  $rs = $conn->Execute($sqlstr);
  $num_cols = $rs->Fields->Count();
  $City = array();
  $i = 0;
  while (!$rs->EOF) {
    $City[$i] = $rs->Fields['name']->Value . ",";
    $rs->MoveNext();
    $i++;
  }
  foreach ($City as $val) {
    echo $val;
  }
  $conn->Close();
  $rs = null;
  $conn = null;
}
if ($_REQUEST['ID'] > 100) {
  $conn->Open($connstr);
  $sqlstr = "select name from County where cid=" . $_REQUEST['ID'];
  $rs = $conn->Execute($sqlstr);
  $num_cols = $rs->Fields->Count();
  $County = array();
  $i = 0;
  while (!$rs->EOF) {
    $County[$i] = $rs->Fields['name']->Value . ",";
    $rs->MoveNext();
    $i++;
  }
  foreach ($County as $val) {
    echo $val;
  }
  $conn->Close();
  $rs = null;
  $conn = null;
}
?>

数据库设计

表格:Province表,City表,County表。 要求:

  • Province表需要idnameid建议从134,例如北京id1,广东id2,以此类推;
  • City表需要idnamecididcid*100+1cid为该市的上级,例如深圳的上级为广东省,cid2的话,深圳的id就是201,以此类推;
  • County表需要idnamecid,因为是三级的关系,id可以随意,建议从10001开始自增。cid为所在上级,例如宝安区的cid201,龙岗区的cid也为201

截图

HTML效果: 完成后效果: