PDA

View Full Version : Passing variable to PHP in JS



hemi519
05-27-2012, 12:20 PM
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
}

djr33
05-27-2012, 12:27 PM
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:

var url = <?php echo $url1; ?>+jsvar+<?php echo $url2; ?>;

hemi519
05-27-2012, 12:36 PM
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.

djr33
05-27-2012, 12:43 PM
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.