php+ajax的簡單應用

來源:互聯網
上載者:User

<script language='JavaScript' type='text/JavaScript'>

function InitAjax()//初始化ajax對象
{
    //return "adfasd";
   var ajax=false;
 try {
  ajax = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   ajax = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   ajax = false;
  }
 }
 if (!ajax && typeof XMLHttpRequest!='undefined') {
  ajax = new XMLHttpRequest();
 }
 return ajax;
}

</script>

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

<select name="typeid" id="typeid" onchange="getchlidtype();">
  <option value="-1">商戶類型</option>
   <? foreach ($type as $v){?>
  <option value="<?=$v[linkageid]?>"><?=$v['name']?></option>
  <? }?>
   </select></span><span id="show"></span>

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

<script>
function getchlidtype()
{

 //需要進行Ajax的URL地址
 var pid = document.getElementById("typeid");

 var url = "http://192.168.0.102/youyuanhui_com/ajax/getoption.php?pid="+ pid.value;

 //擷取新聞顯示層的位置
 var show = document.getElementById("show");
    
 //執行個體化Ajax對象
 var ajax = InitAjax();

 //使用Get方式進行請求
 ajax.open("GET", url, true);

 //擷取執行狀態
 ajax.onreadystatechange = function() {
  //如果執行是狀態正常,那麼就把返回的內容賦值給上面指定的層  
if (ajax.readyState == 4 && ajax.status == 200) {

   show.innerHTML = ajax.responseText;
  }
 }
 //發送空
 ajax.send(null);
}
</script>

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

getoption.php

<?php
require '../include/common.inc.php';
require '../include/system_info.php';
global $db;
if($pid=$_GET["pid"])
{
    $options=$db->select("select * from yyh_linkage where parentid=".$pid);
    echo "<select name='typeid2' id='typeid2'>";
    echo "<option value='-1'>-選擇子類</option>";
    foreach($options as $opt)
    {
        echo "<option value=".$opt['linkageid'].">".$opt['name']."</option>";
    }
    echo "</select>";
}
?>

原理:假設A,B兩個頁面

A頁面是AJAX請求,在A頁面中要顯示資料資訊,則A通過ajax請求B頁面,B頁面在取得相應資料後顯示在B頁面上,則B頁面的內容就會返回到A頁面中。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.