0

Why can't I use a calculated number, from php in jQuery?

I've tried this:

<div id="time">
    <? echo until();?>
</div>
var until = $("#time").html();

And

var until = <? echo until();?>;

But no one works D:

EDIT: The calculated number will be 472541, and the function:

<?
/**
* @desc Time until tuesday
* @param string Time format
* @return string timestamp
*/
function until($format = ""){
    $now = strtotime("now");
    $nextTuesday = strtotime("-1 hour next tuesday");
    $until = $nextTuesday - $now;
    if(empty($format)){
        return $until;
    }else{
        return date("$format",$until);
    }
}

?>

EDIT EDIT: Don't know what just happend, but it seems to work now :O :D Thanks a lot, everyone :D

6
  • Please update your answer in order to get (doog) help: What is happening (or should happen) What error(s) do you get? etc? Commented Aug 10, 2011 at 16:38
  • 1
    Can you post until method and the generated HTML? Commented Aug 10, 2011 at 16:38
  • Have you tried looking at the resulting source code? Commented Aug 10, 2011 at 16:39
  • What does the until() function look like? Also, is "short_open_tag" set to "1" in php.ini? Commented Aug 10, 2011 at 16:41
  • Aside from the problem, 'hideorshow' is a bad name for a field. just call it 'hide' or call it 'show'. That makes it obvious what a '1' will mean to anyone else who has to work with this db. 'hideorshow' makes the 1/0 values completely ambiguous Commented Aug 10, 2011 at 16:46

5 Answers 5

1

The way I have been pulling PHP values from something like this into a variable is to make a new attribute in the DIV, something like

<div id="time" value="<?= until(); ?>">

</div>

then, by using JQuery, I can pull the variable by saying

var until = $('#time').attr(value);
Sign up to request clarification or add additional context in comments.

1 Comment

and you made sure the $variable can echo properly in the first place? I guess I meant to say $variable is until(); I will edit the above to show this
0

Did you try it inside a script block, like so:

<script type="text/javascript">
/* <![CDATA[ */

    var until = <?php echo until();?>;
    alert(until);

/* ]]> */
</script>

3 Comments

Yea, just didn't want to write it down (Too lazy)
and what is printed to the html, if you echo your until() result there?
last but not least, what do you see, if you add an alert right after the definition of the variable?
0

can you try this

  <? $output =until();  echo $output?>

var until = <? echo $output ?>;

Comments

0

If you want to echo out the results of the until function, the function would have to actually return something. The following would work perfectly.

function until() {
    return '1';
}

<script type="text/javascript">
    var until = <?php echo json_encode(until()); ?>;
</script>

Note the use of json_encode(). While not necessary in this case, since it'd just output a '1'. using json_encode in general is a good idea, as it guarantees that whatever's coming out of PHP will be syntactically valid Javascript. It's very easy to generate Javascript on the fly that contains syntax errors, which will kill the rest of your script immediately.

Comments

0

First things first, you shouldn't be using shorthand <?php ?> tags, it's not recommended. And second, I can't trace your problem, really.

http://codepad.viper-7.com/ufrfYR

Comments

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.