繁体   English   中英

下拉值更改为PHP

[英]Dropdown value onchange to PHP

我试图发送更改的值下拉到PHP脚本。 但是用我解决问题的方式,表单和状态String都被发布了两次。 一次设置了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
}
?>

您已经将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部分应该在slide.php中 此外,在示例中,我使用POST,但如果要GET,只需更改type: "GET" 在script.php中获取其价值,请使用:

$_POST['users']; 或者,如果将类型更改为GET,则$_GET['users']也可以使用$_REQUEST['users']处理POST,GET和COOKIE。

我认为这是因为您实际上正在打2个电话。 一个用Javascript编写到PHP文件,另一个无论设置什么都在呼应。 我会发表评论,但还没有足够的果汁。

尝试回显$ _GET并查看您在通话之前和通话之后的实际设置。 从那里您可能能够看到实际发生的情况。 我希望这有助于您朝正确的方向发展。

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

    }
});

希望对你有帮助

//查看文件

<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);

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM