ajax传值给php
test.php
<script type="text/javascript"> function selectInput(oSelect) { var value= oSelect.value; var url = "2.php"; var postStr = "value="+ value ; //-------------------------------------------------------------------------------- //实例化Ajax //var ajax = InitAjax(); var ajax = false; //开始初始化XMLHttpRequest对象 if(window.XMLHttpRequest) { //Mozilla 浏览器 ajax = new XMLHttpRequest(); if (ajax.overrideMimeType) {//设置MiME类别 ajax.overrideMimeType("text/xml"); } } else if (window.ActiveXObject) { // IE浏览器 try { ajax = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { ajax = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!ajax) { // 异常,创建对象实例失败 window.alert("不能创建XMLHttpRequest对象实例."); return false; } //-------------------------------------------------------------------------------------------- ajax.open("POST", url, true); ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); ajax.send(postStr); //---------------------------------------------------------------------------------------------- ajax.onreadystatechange = function() { //如果执行状态成功,那么就把返回信息写到指定的层里 if (ajax.readyState == 4 && ajax.status == 200) { msg.innerHTML = ajax.responseText; //写入到id为msg的div里 } } //------------------------------------------------------------------------------------------------- } </script> <div id="msg"></div> <?php $cnx = new PDO("odbc:Driver={SQL Server};Server=(local);Database=DBCT_Dev;",'sa','123456'); //连接数据库 ?> <select onchange="selectInput(this)""> <?php $Province=$cnx->query("select ProvinceName,ProvinceId from S_Province"); foreach($Province as $sheng){ echo "<option value='".$sheng['ProvinceId']."'>".$sheng['ProvinceName']."</option>"; }?> </select>
2.php
<?php print_r ($_POST['value']); ?>