user1817669
user1817669

Reputation: 11

check if value exist in $_POST[''] is identical

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 "===".

Upvotes: 0

Views: 198

Answers (4)

kuldeep.kamboj
kuldeep.kamboj

Reputation: 2606

You must use == instead of =. = is assignment operator not comparison operator. Optionally you can use === for strict comparison.

Upvotes: 2

Vivek Sadh
Vivek Sadh

Reputation: 4268

Change this:-

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

}

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

to

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

}

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

Upvotes: 1

KhAn SaAb
KhAn SaAb

Reputation: 5366

do like this

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

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

this

"  === "

for comparing variable with identity

if we compare

"5"===5 returns false
 "5"==="5" returns true

Upvotes: 0

moonwave99
moonwave99

Reputation: 22817

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.

Upvotes: 2

Related Questions