0

I got a problem regarding how to assign a java script variable to a smarty variable. Here is the code snippet.

function getDateInfo(date, wantsClassName)
{                  
    var as_number = Calendar.dateToInt(date); //This as_number is the variable which should be assigned to smarty variable    
}

How can i accomplish this.

any help will be appreciated..

Thanks n advance -- Fero

2 Answers 2

7

You can't assign a client-side value to a smarty variable, as smarty is a templating language that runs on the server. Smarty assignments can only be done on the server-side, that is to say, from PHP. E.g.:

$smarty->assign('timestamp',time());

So what you can do is something like:

    $smarty->assign('timestamp',time()); //in your PHP script

    //in your JS
    var currentTS = {$timestamp};

See http://www.smarty.net/manual/en/api.assign.php

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

2 Comments

thanks for your reply karim. but is there any other way to get the value of java script variable and assigning it to smarty.
This code not working for the array variable.Is there any modifications needed for array variable? :-@karim79
2

For anything AJAX with PHP, I'd propose the xajax library:

http://www.xajax-project.org/

Using xajax, you register a PHP function to be exposed to your client side JS code. By magic, if you call registered functions in JS, xajax packs the request with parameters and passes it back to the server.

Moreover, you are able to push more than update from your PHP code to the browser. Something like 'Replace element X with ... and replace element Y with ...' in a single XHR reply.

xajax makes AJAX style code very clean.

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.