字符数据没有从php传递到jquery [英] Character data is not passing from php to jquery

查看:75
本文介绍了字符数据没有从php传递到jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Html文件

子组合框的值由php select查询检索,值为字符。

The values for the sub combobox is retrieved by a php select query and the values are characters.

我试过整数值完全通过。

i have tried with integers values are perfectly passed.

<select name="sub" id="sub">
        <option value="">Select semester first</option>
    </select>
<select name="staff" id="staff">
        <option value="">Select sub first</option>
    </select>

Javascript

Javascript

$('#sub').on('change',function(){
        var SUB = $(this).val();
        if(1){
            $.ajax({
                type:'POST',
                url:'sta.php',
                data:'sub='+SUB,//character is not passing.
                success:function(html){
                    $('#staff').html(html);
                }
            }); 
        }else{
            $('#staff').html('<option value="">Select sub first</option>'); 
        }
    });

php file

if(isset($_POST["sub"]) && !empty($_POST["sub"]))
{
    //Get all city data
    $query = mysql_query("SELECT staff_name FROM subject WHERE course_name = ".$_POST['sub']);

    //Count total number of rows
    $rowCount = mysql_num_rows($query);//It always shows 0

    //Display cities list
    if($rowCount > 0){
        echo '<option value="">Select staff</option>';
        while($row = mysql_fetch_assoc($query)){ 
            echo '<option value='.$row['staff_name'].'>'.$row['staff_name'].'</option>';
        }
    }else{
        echo '<option value="">staff not available</option>';//when i tried to execute it comes here
    }
}


推荐答案

有简单的语法错误在您的PHP查询中只需将其重写为:

There is simple syntactical error in your query in php just rewrite it as:

$query = mysql_query("SELECT staff_name FROM subject WHERE course_name = '".$_POST['sub']."'");

无论您传递数字以外的数据,总是使用单个倒置逗号,例如 'myData'包围值。

wherever you pass the data other than numbers always use single inverted comma such as'myData' to surround the values.

这篇关于字符数据没有从php传递到jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆