0

Basically I want to perform this check:

   var done = "<?= $test_details['done_test'] ?>";
    if(typeof done == 'undefined'){
    $('WORKING').appendTo('#bodyArea'); // just for testing (code here)
    }

Obviously the check is using a value from an SQL query. So, what I want to do and cant seem to get working is:

on loading page: run query with PHP then run the JQuery which checks result value from query is not NULL.

this is what I have tried:

$(window).load(function () {
 // code here
});

and:

$(document).ready(function () {
    // code here
});

no luck with both.

Many thanks for help.

1
  • 1
    javascript is always executed after php, since php runs on the server it's executed even before the page is sent to the browser. then (in the browser) the javascript is executed. so both your versions should work. what does the sourcecode for "var done = ..." look like in the browser? also done will never be undefined but an empty string at most (if the php $test_details is empty) Commented Apr 21, 2011 at 11:48

1 Answer 1

1
var done = "<?= $test_details['done_test'] ?>";
if(typeof done == 'undefined'){
   $('WORKING').appendTo('#bodyArea'); // just for testing (code here)
}

typeof done would never be undefined since you'd always be defining it.

If you want to run some jquery based on your PHP, you can try this

<?php if (empty($test_details['done_test'])) : ?>
   <script type='text/javascript'>
       //Code runs ONLY if $test_details['done_test'] is empty
       $(document).ready(function(){
         $('WORKING').appendTo('#bodyArea'); // just for testing (code here)
       });
   </script>
<?php endif; ?>

This way, the check is done on a PHP variable with PHP. When the page is processed, if the variable is empty PHP will output the JS making it run.

DEBUGGING

If you're still having problems, put the code below and see what happens

<?php if (empty($test_details['done_test'])) : ?>
   <script type='text/javascript'>
       alert("$test_details['done_test'] is empty");
   </script>
<?php else : ?>
   <script type='text/javascript'>
       alert("$test_details['done_test'] is not empty");
   </script>
<?php endif; ?>

If you're not getting any output, then you're doing something wrong. We'll have to see your HTML.

If your getting that $test_details['done_test'] is not empty message, then that means your earlier code was working properly because it was not executing if the variable was not empty

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

4 Comments

You can put it in the head. Make sure you wrap that code in $(document).ready(). Not sure about the highligher, but the code is fine. Make sure you aren't putting it in the middle of another <script> tag
it doesnt appear to do anything, I get no output.
this is what im trying: $(document).ready(function () { $('WORKING').appendTo('#bodyArea'); // just for testing (code here) }
I updated the answer. Check the updated code as well as the debugging note I added

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.