简体   繁体   English

Ajax / Jquery下拉菜单,使用Javascript获取变量,然后在PHP中使用变量。 无法正常工作?

[英]Ajax/Jquery drop down menu, Javascript to get variable, variable is then used in PHP. Can't get it to work?

I am using Ajax/Jquery to display a dropdown menu with results from my SQL database. 我正在使用Ajax / Jquery显示一个下拉菜单,其中包含来自我的SQL数据库的结果。 I'm using javascript to get a variable and then that variable is used within PHP. 我正在使用javascript获取变量,然后在PHP中使用了该变量。 However it does not work. 但是,它不起作用。

I used Jquery .val() to get the variable from a select html tag when the user clicks the choices available. 当用户单击可用选项时,我使用Jquery .val()从select html标记获取变量。

Then, .on() to execute some php code depending on what the selected value from the dropdown box is. 然后,.on()根据从下拉框中选择的值来执行一些php代码。

My scenario is I have car classes (Sports, Hatchback) and cars available. 我的情况是我有可用的汽车课程(运动,掀背车)。 What I am trying to do is, put the car classes in a dropdown box and then display the cars available dependent upon what the user has selected. 我想做的是,将汽车分类放在下拉框中,然后根据用户的选择显示可用的汽车。 I'm trying to do this using the above methods. 我正在尝试使用上述方法来执行此操作。 (All this information is taken from a SQL database). (所有这些信息均取自SQL数据库)。

Has anyone got any solutions? 有人有解决方案吗?

This is my my javascript code here: 这是我的JavaScript代码在这里:

<script>
var carid = $("#carid").val();

$("select[name='carid']").on("select",function(){$.post( "sDn.php", x, function( data ) {  $( ".availablecar" ).append( data );});});
</script>

It appears to me that you are not using the carid variable anywhere. 在我看来,您没有在任何地方使用carid变量。 I think you mean to use carid instead of x 我想你是说用carid代替x

<script>
var carid = $("#carid").val();

$("select[name='carid']").on("select",function(){$.post( "sDn.php", carid, function( data ) {  $( ".availablecar" ).append( data );});});
</script>

Also I would suggest parsing you form input if you don't already on the php side. 另外,如果您还不支持php,我建议您解析表单输入。

Try this: 尝试这个:

$("#carid").on("change",function(e){
    e.preventDefault();

    var value = $(this).find("option:selected").val();

    var $avaliable = $( ".availablecar" );

    $.ajax({
        url:'sDn.php',
        type:'post',
        data:{
            'carid':value
        },
        dataType:'html',
        success:function(result){
            $avaliable.append( result );
        }
    });
});

Changed the $.post to $.ajax , passing the ' cardid ' value. $.post更改为$.ajax ,并传递' cardid '值。

Getting the selected option, on ' change '. 在“ change ”上获取选定的选项。

You probably want something like this 你可能想要这样的东西

JavaScript 的JavaScript

$("select[name='carid']").on("change", function() {
  $.post(
    "sDn.php",
    { carId: $("#carid").val() },
    function(data) {
      $(".availablecar").append(data);
    }
  );
});

PHP 的PHP

$carId = $_POST['carId'];

You need to send the car id in the request and you probably need to bind to the change event, not select 您需要在请求中发送汽车ID,并且可能需要绑定到change事件,而不是select

You also need to get the car id value when the id has been changed, otherwise it will always remain the same value and never get updated when you change it 您还需要在更改ID后获取汽车ID值,否则它将始终保持不变,并且在更改ID时永远不会更新

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

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