2013-07-02 67 views
0

我试图将更改下拉列表的值发送到PHP脚本。但以我解决问题的方式,表单和状态字符串都会发布两次。一旦设置了GET参数,另一个没有。我不知道如何解决,但也许你比我聪明。Dropdown value onchange to PHP

<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 
} 
?> 
+2

什么是'xmlhttp.reload()'应该做?我无法找到该方法的文档。 – Barmar

+0

为什么你不想使用jQuery? – Robert

+2

如果你使用jQuery,你为什么不使用它的AJAX方法而不是编写所有的XMLHTTP的东西? – Barmar

回答

0

我认为这是因为你实际上正在打2个电话。一个用Javascript写到PHP文件中,另一个用回显,不管设置什么。我会评论,但尚未得到足够的果汁。

尝试并回显出$ _GET并查看您在通话之前和通话之后实际设置的内容。从那里你可能会看到实际发生的事情。我希望这可以帮助你指引正确的方向。

4

您已经将Jquery包含到您的项目中,以便使用它的功能。主要是Jquery Ajax来处理Ajax请求。的代码

$(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`. 
    }); 
    }); 
}); 

PHP的部分应该是在slider.php。此外,在示例中我使用POST但是如果你想GET只需更改type: "GET"。在script.php中获取它的值使用:

$_POST['users'];或者如果您更改类型为GET然后$_GET['users']您还可以使用处理POST,GET,COOKIE的$_REQUEST['users']

+0

我不会推荐使用'$ _REQUEST' - 使用起来不是很安全。使用每个单独的超全球变量 – Bojangles

+0

@Bojangles它是可能性之一不推荐:)但我同意你 – Robert

0
var name=document.forms["myform"]["user"].value; 
$.ajax({ 
type: "POST", 
url: "yourpagenmae.php", 
data: { name: user } 
    , 
    success: function(msg) { 
     // alert(msg); 

    } 
}); 

希望它会帮助你

-1

//查看文件

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
    //use jquery get method 
    $('#users').change(function() { 
     //get the user value 
     var user = $('#users').val(); 

     $.getJSON("http://localhost/test.php?users="+user, function(data1) { 

      console.log(data1); 

      //status 
      if (data1.status) { 
       alert(data1.q); 

       //whatever response variable key 
       $('#txtHint').html(data1.q) 
      } else { 
       alert('error'+data1.q); 
      } 
     }); 
    }); 
}); 
</script> 
<!-- Site JavaScript --> 
<form> 
    <select id="users" name="users" > 
    <option value="bikes">Bikes</option> 
    <option value="zub">Stuff</option> 
    <option value="sonst">Other</option> 
</select> 
</form> 
<br> 

<div id="txtHint"></div> 

//test.php文件

<?php 

$data = array(); 

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

$data['status'] = true; 
$data['q'] = $q; 


}elseif(!isset($q)){ 

$data['status'] = false; 
$data['q'] = 'KEIN Q GEPOSTET'; 

} 

header('Content-type:application/json'); 
//json response 
echo json_encode($data); 

?> 
+0

在哪里解释?文档未准备好时绑定事件可能会导致问题,并且不会始终有效。 Onchange select是binde到不存在的函数。你的回答非常无效。 – Robert

+0

检查行前的注释。我希望你在按下投票 – Sundar

+0

之前仔细阅读整个代码,有什么意见?没有准备好的事件,你的代码甚至不会执行,因为它有我上面写的错误。例如,这个函数'onChange =“showUser()”'是否被声明?用ajax引用同一个文件也不明智。 – Robert