Results 1 to 4 of 4

Thread: Passing variable to PHP in JS

  1. #1
    Join Date
    Nov 2010
    Posts
    115
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Passing variable to PHP in JS

    Hi All,

    Can anyone help me, i ma trying to pass js variable into PHP variable in Javascript.

    function tested()
    {


    var order = document.getElementById('list_filter').value;

    alert (order); // i am getting my value here

    <?php $name = order; ?>

    he_list.callback = "<?php echo $this->callback; ?>";
    he_list.list_type = "<?php echo $this->list_type; ?>";
    he_list.params = <?php echo Zend_Json_Encoder::encode($this->params); ?>;
    he_list.list = '<?php echo $this->list; ?>';
    he_list.module = '<?php echo $this->module; ?>';



    he_list.ajax_url = '<?php echo $this->url(array('module' => 'hecore', 'controller' => 'index', 'action' => 'list','search' => $name )); ?>'; // in this line it is taking $name as order. can anyone help me, how to get the JS varaible in place of $name



    he_list.page = <?php echo $this->items->getCurrentPageNumber() ? $this->items->getCurrentPageNumber() : 1; ?>;
    he_list.vlaue = <?php echo $name; ?>

    alert(he_list.ajax_url);
    alert(he_list.vlaue); //this is giving me JS value
    }

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    PHP operates entirely and then writes pure text (HTML+JS+CSS), with no PHP remaining. Then and only then does anything else (including JS) operate.
    In other words, you simply cannot do this. You'll need to order it differently.

    The easiest way is something like this:
    PHP Code:
    var url = <?php echo $url1?>+jsvar+<?php echo $url2?>;
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Nov 2010
    Posts
    115
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the reply, can u help me in my case

    var order = document.getElementById('list_filter').value;

    <?php $name = order; ?>

    he_list.ajax_url = '<?php echo $this->url(array('module' => 'hecore', 'controller' => 'index', 'action' => 'list','search' => $name )); ?>';

    how can i separate into two url's. I need to replace 'order' value with '$name'. I am trying this for last 2 hours but unable to do it.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You can't. As I said in my last post, this is entirely impossible. You need to generate the URL in PHP in two parts and leave a blank space to insert the $name part in Javascript. You'll need to rewrite your class's url() method (function) to create two values, not one, and then to combine that with javascript to add in the JS 'name' variable. You cannot put the JS variable "into" PHP.

    (The only way to do this is to use Ajax, and that's more complicated than you need here, because you can instead use JS to build the final URL.)

    There's another alternative: you could use a dummy value for $name, like ###. Then use a Javascript string replacement function to search for "###" and replace it with whatever your JS 'name' variable's value is.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. The Following User Says Thank You to djr33 For This Useful Post:

    hemi519 (05-27-2012)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •