1

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>
1
  • 1
    replace $redirect with <?php echo $redirect; ?> and = with == Commented Jan 24, 2014 at 8:43

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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)

5 Comments

thanks for your reply i tried what you said but showing syntax erron near var redirect= <?php echo $redirect?> if(redirect==1){
its showing alert but if reditect==1 it not redirecting,can you tell me how to fix this
hmmm then mistake in your php file
@arok dispite the JS, it will take only one of the iterations, maybe the last, because you do reassignation everytime the loop goes. You need to use it as array i.e. $reditect[] = $row[...], but then you can simply echo it. You will need to loop through it, and maybe json_encode it to pass it to the javascript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.