3

I have a code like this

$(document).ready(function() {
    $('#myHref').change(function(){
        var value = $('#myHref').val();
        $.get('get_projectName.php',{id:value},function(data)
        { 
        .....
        .....
        if(condition here){}
        }); 
    }); 
});

I need to check a condition according to the value returned from get_projectName.php. Let get_projectName.php have $abc = 1; and according to this value I need to use if condition.

0

4 Answers 4

3

Your jquery condition will be totally depend on the type of data returned from the php function. Let's check for the example:-

Example 1 :-

If your php code is :-

<?php
if(isset($_GET['id'])){ // check id coming from `ajax` or not
  $data = 1; // as you said
}
echo $data;
?>

Then jquery will be:-

<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script><!-- library needed-->
<script type = "text/javascript">
$(document).ready(function() {
   $('#myHref').change(function(){ // you need to check that it is working or not because i don't know from where it is coming
        var value = $('#myHref').val(); // same as above check yourself.
        $.get('get_sales_price.php','',function(data){ 
            if(data ==1){
                alert('hello');
            }else{
                alert('hi');
            }
        });
    });
});
</script>

Example 2:-

But if your php code is like below:-

<?php
if(isset($_GET['id'])){ // check id coming from `ajax` or not
     $data = Array('a'=>1,'b'=>2);
}
echo json_encode($data);
?>

then jquery will be like below:-

<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script type = "text/javascript">
$(document).ready(function() {
    $('#myHref').change(function(){
        var value = $('#myHref').val();
        $.get('get_sales_price.php','',function(data){ 
            var newdata = $.parseJSON(data);//parse JSON
            if(newdata.a ==1 && newdata.b !== 1){
                alert('hello');
            }else{
                alert('hi');
            }
        });
    });
});
</script>

Note:- these are simple examples, but conditions will vary in jquery, based on returned response from php. thanks.

Sign up to request clarification or add additional context in comments.

Comments

2

Forgot the .done

  $.get('get_projectName.php',
   {id:value}
  ).done(function(data) { 
     console.log(data)
     var data2 = JSON.parse(data);

     if(data2.abc === 1)
     {
       //Do something
     }else{
       //Else Do something
     }
  }); 

3 Comments

How can I use if($abc == 1){DO SOMETHNG}, get_projectName.php returns $abc and according to returned vale $abc i need to apply if condtion
$.get as a callback parameter that works fine. How is that an answer?
Can you console.log(data)?
2

You can write code as below -

//At javscript end
$.get( "get_projectName.php", function( data ) {
   if(data == "1"){
     // do your work
   }
});
// At php end
<?php
$abc = 1;
echo $abc;
?>

Hope this will help you.

Comments

1
  • In main-php-file On change of value, Fetch the value pass it a php file sub-php-file via ajax
  • In sub-php-file, echo the ouput in json format
  • Retrieve the output and do the calculation


jQuery library, if never included

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

ajax

<script type="text/javascript">
$(document).ready(function(){ 

    $('#myHref').on('change', function(){ 
        var value = $('#myHref').val();
        $.ajax({
                type: "POST",  
                url: "get_projectName.php",  
                data: { id:value },
                dataType: "json",
                success: function(theResponse) {

                        var abc = theResponse['abc'];

                        if (abc == 1) {
                        //Do something
                        } else {
                        //Else Do something
                        }

                    }  
            });
    });
});                 
</script>

get_projectName.php

<?php
$id = isset($_POST['id']) ? $_POST['id'] : '';

$ReturnArray['abc'] = 1;

echo json_encode( $ReturnArray );
?>

1 Comment

OP used get so try to give answer based on get

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.