下载值onchange to PHP

时间:2013-07-02 09:57:03

标签: php ajax drop-down-menu onchange

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

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

4 个答案:

答案 0 :(得分: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']

答案 1 :(得分:0)

我认为这是因为你实际上正在打2个电话。一个在Javascript到PHP文件,另一个你的回声无论设置什么。我会评论,但还没有足够的果汁。

尝试并回显$ _GET,并在通话前和通话后查看您的实际设置。从那里你可以看到实际发生了什么。我希望这有助于指导您朝着正确的方向前进。

答案 2 :(得分:0)

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

    }
});

希望它会帮助你

答案 3 :(得分:-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);

?>