doulong2782 2013-06-25 07:48
浏览 8

检查$ _POST ['']中是否存在值相同

I am working on Ajax.i have multiple select box now i want recall same page again and again but problem is that we have lots sql query.with the of if condition i select query according to my need.now i want check like if ($_POST['field'] contain state or region ); ajax part:-

$('#country').change(function()
        {
            var id=$("#country").val();
            var dataString = 'id='+ id;

            $.ajax
            ({
                type: "POST",
                url: "ajax_centre.php",
                data: {state:dataString,field:'state'},
                cache: false,
                success: function(html)
                {
                $("#state").html(html);
                } 
            });
        });
        $('#state').change(function()
        {
            var state_id=$('#state').val();
            var country_id=$('#country').val();
            var dataString = 'state_id='+ state_id;
            var country_id = 'country_id='+ country_id;


            $.ajax
            ({
                type: "POST",
                url: "ajax_centre.php",
                data: {country:country_id,state:dataString,field:'region'},
                cache: false,
                success: function(html)
                {
                $("#region").html(html);
                } 
            });

        }); 

in upper part i pass field:state and field:region now i want to select my on behalf of that //ajax_centre.php:-

if($_POST['field']='state')
{
   //execute query

}

if($_POST['field']='region')
{
    //execute query
}

is this possible i know isset() but i have to differentiate each other

i finally i find the bug problem exist in passing post value correct script:-

$('#country').change(function()
        {
            var id=$("#country").val();
            //var dataString = 'id='+ id;

            $.ajax
            ({
                type: "POST",
                url: "ajax_centre.php",
                data: {id:id,field:'state'},
                cache: false,
                success: function(html)
                {
                $("#state").html(html);
                } 
            });
        });
        $('#state').change(function()
        {
            var state_id=$('#state').val();
            var country_id=$('#country').val();
            //var dataString = 'state_id='+ state_id;
            //var country_id = 'country_id='+ country_id;


            $.ajax
            ({
                type: "POST",
                url: "ajax_centre.php",
                data: {con_id:country_id,sta_id:state_id,field:'region'},
                cache: false,
                success: function(html)
                {
                $("#region").html(html);
                } 
            });

        }); 

one think i got both operator work in my case "==" and "===".

  • 写回答

4条回答 默认 最新

  • duanmen2189 2013-06-25 07:52
    关注

    You should check for [strict] equality with the === operator:

    if($_POST['field'] === 'state')
    {
       //execute query
    
    }
    
    else if($_POST['field'] === 'region')
    {
        //execute query
    }
    

    with the if/else control block, you are sure to execute just one portion of the code.

    By the way, beware that the single = operator is an assignment, and not a comparison.

    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示