arok
arok

Reputation: 1845

Using if statement in ajax using values from the database

I have column name status in Database it contains values(0,1) ,in front end i have image if i click the image it will redirect to the next page i am using ajax here, what i want before redirect it should perform if condition i want to check the database column status if the column contain any row '1' it should redirect if all rows in column '0' its not checking the if condition it will redirecting can anyone guide me how to perform this .below is my code(i.e now in row status all values are 0 it will not redirect it want to show alert but its redirecting)

html

<img  id='redirect'  src="/image/exporttt.png"  style="margin:-30 0 0 0px;cursor:pointer;"  >

$sql_selectsupplier  = "********";
$result1 = mysql_query($sql_selectsupplier);
while($rows=mysql_fetch_array($result1))
{
  $reditect=$rows['status'];
}

Ajax

<script>

$(document).ready(function() {
    $("#redirect").click(function() {


        if($reditect=1){

     var clientid=document.getElementById("client").value;

        $.blockUI(

       { 

         message: '<h2>Please wait...</h2><img src="/image/loader.gif" />',
     timeout: 2000

       }); 


    $.ajax({
            type:"post",

        data:"clientid="+clientid,
        success:function(data){

                         window.location = '?action=clientroutingchange&clientid='+clientid+'';
                          $("#result").html(data);
                         $('.blockUI').hide();
        }
    }); 
        }
        else{

            alert("no changes made")
        }   

    });
});

</script>

Upvotes: 1

Views: 1290

Answers (2)

Linga
Linga

Reputation: 10553

Use == for comparision

if($reditect==1)

and your $redirect is a php variable which doesn't make sense in script

So, use

var reditect = <?php echo $reditect ?>

and check by,

if(reditect==1)

Upvotes: 1

Ivan Yonkov
Ivan Yonkov

Reputation: 7034

From your code it's not clear which one is PHP code, and which one is out of the PHP block.

Inside your block you have

if($reditect=1){ which does not seem like a javascript code.

Is that php?

If yes, then you are using it wrong way. = is an assignation operator, so you do assign the value 1 explicitly this way.

I would suggest, if you are using PHP conditions, also to move thme outside the script block:

<?php if ($reditect == 1): ?> # `==` for comparison
    <script>
        var clientid=document.getElementById("client").value;
        // .. all the code
    </script>
<?php else: ?>
    <script>
        alert('.....');
    </script>
<?php endif; ?>

If it was an Javascript condition, then you need to assign the value of the php variable to the js var

<script>
var reditect = "<?= $reditect; ?>";
if (reditect == '1') {
   // something
</script>

Upvotes: 1

Related Questions