的onchange到PHP下拉值onchange、PHP

2023-09-11 22:29:39 作者:了了无期

我试着去改变上一个下拉的值发送到一个PHP脚本。但与我的解决问题的方式,形式,也状态字符串都贴两次。一旦与GET参数集,并且没有其他。我不知道如何解决,但也许你比我聪明。

 <脚本类型=文/ JavaScript的SRC =htt​​p://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js >< / SCRIPT>
<脚本类型=文/ JavaScript的SRC =JS / jquery.easing.1.3.js>< / SCRIPT>
<脚本类型=文/ JavaScript的SRC =JS / jquery.ennui.contentslider.js>< / SCRIPT>
<脚本类型=文/ JavaScript的>
功能showUser()
{
VAR用户=的document.getElementById(用户)的价值。

如果(用户==)
{
。的document.getElementById(pictab)的innerHTML =;
返回;
}
如果(window.XMLHtt prequest)
{// $ C $下IE7 +,火狐,Chrome,歌剧,Safari浏览器
XMLHTTP =新XMLHtt prequest();
}
其他
{// code对IE6,IE5
XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.onreadystatechange =功能()
{
如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200)
{
的document.getElementById(txtHint)的innerHTML = xmlhttp.responseText。
}
}
xmlhttp.open(GET,?slider.php用户=+用户,真正的);
xmlhttp.send();
xmlhttp.reload();
}

< PHP
// .............
// ..............
// .............
// ..............
$索索= mysql_num_rows($ connn3);
为($ i = 0; $ I< $索索; $ I ++)
{
回声 
$(函数(){
$('#1 $ I')。ContentSlider({
宽度:'280px',
高度:180px,
速度:400,
宽松:easeOutQuad
});
});;
}
?>

< / SCRIPT>
<! - 网站的JavaScript  - >
<形式GT;
<选择一个id =用户NAME =用户的onChange =showUser()>
<期权价值=自行车>自行车< /选项>
<期权价值=ZUB>的东西/选项>
<期权价值=sonst>其他< /选项>

< /选择>
< /形式GT;
< BR>
< D​​IV ID =txtHint>< / DIV>
< PHP


如果(使用isset($ _ GET ['用户'])){
回声< H2>问贴< / H2>中;
$ Q = $ _GET ['用户'];
回声$ Q;
//数据库查询具有Q

} ELSEIF(!使用isset($ Q)){
回声KEIN Q GEPOSTET;
//数据库查询,而不Q
}
?>
 

解决方案

您已经包括jQuery的到你的项目,所以利用它的特性。天色 jQuery的阿贾克斯来处理Ajax请求。

  $(函数(){//文件被加载,所以我们可以绑定事件

  $(#用户)。改变(函数(){//改变事件的选择
     $阿贾克斯({// Ajax调用
        类型:POST,//方法== POST
        网址:slider.php,// URL被称为
       数据:{用户:$(#用户选项:选择)。VAL()} //数据被发送
     })。完成(功能(味精){//调用时请求成功味精
       //做一些味精是响应
             $(#txtHint)HTML(MSG)。 //这一行就会把id为`#txtHint`应对项目。
     });
   });
 });
 
PHP怎么设置默认下拉值

的code PHP的一部分应该是 slider.php 。此外,例如,在我使用的发布但是如果你想要获取只需更改键入:GET。在script.php的得到它的值是:

$ _ POST ['用户']; 或者如果你改变了类型要获得那么 $ _ GET ['用户'] 你也可以使用 $ _ REQUEST ['用户'] 用于处理POST,GET,COOKIE。

Im trying to send a value of a dropdown on change to a php script. But with my way of solving the problem, the form and also the status String are posted twice. Once with GET parameter set, and the other without. I dont know how to solve,but maybe you are smarter than me.

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.easing.1.3.js"></script>
<script type="text/javascript" src="js/jquery.ennui.contentslider.js"></script>
<script type="text/javascript">
function showUser()
{
var users = document.getElementById('users').value;

if (users=="" )
{
document.getElementById("pictab").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","slider.php?users="+users,true);
xmlhttp.send();
xmlhttp.reload();
}

<?php 
//.............
//..............
//.............
//..............
$soso = mysql_num_rows($connn3);
for($i=0;$i<$soso;$i++)
{
echo "
$(function() {
$('#one$i').ContentSlider({
width : '280px',
height : '180px',
speed : 400,
easing : 'easeOutQuad'
});
});";
}
?>

</script>
<!-- Site JavaScript -->
<form>
<select id="users" name="users" onChange="showUser()" >
<option value="bikes">Bikes</option>
<option value="zub">Stuff/option>
<option value="sonst">Other</option>

</select>
</form>
<br>
<div id="txtHint"></div>
<?php 


if(isset($_GET['users'])){
echo "<h2>Q posted</h2>";
$q = $_GET['users'];
echo $q;
//DB QUERY WITH Q

}elseif(!isset($q)){
echo "KEIN Q GEPOSTET";
// DB QUERY WITHOUT Q
}
?>

解决方案

You've included Jquery into your project so use its features. Mostly Jquery Ajax to handle Ajax requests.

$(function (){ //document is loaded so we can bind events

  $("#users").change(function (){ //change event for select
     $.ajax({  //ajax call
        type: "POST",      //method == POST 
        url: "slider.php", //url to be called
       data: { users:  $("#users option:selected").val()} //data to be send 
     }).done(function( msg ) { //called when request is successful msg
       //do something with msg which is response
             $("#txtHint").html(msg); //this line will put the response to item with id `#txtHint`.
     });
   });
 });

The php part of code should be in slider.php. Moreover, in example I use POST but if you want GET simply change type: "GET". In script.php to get value of it use:

$_POST['users']; or if you change the type to GET then $_GET['users'] you can also use $_REQUEST['users'] which handles POST, GET, COOKIE.