How to pass javascript jQuery variable value in php array?

how to pass javascript jQuery variable value in php array

  • For example var dirnm = jQuery.trim($('#dirname').val()); var parent1 = jQuery.trim($('#parent').val()); var url = '<?=$this->url(array('controller'=>'index','action'=>'dirnameex','dirname'=>dirnm ,'parent'=>parent1))?>'; dirnm and parent1 are a ajax variables. So I want to pass dirnm and parent1 to a php array.

  • Answer:

    This wont work as JavaScript is executed in the browser and PHP executed on the server .. so the PHP would be executed before the page is loaded in the browser and the JavaScript executed .... What you could do is POST the variables to PHP : var dirnm = jQuery.trim($('#dirname').val()); var parent1 = jQuery.trim($('#parent').val()); $.post("sript.php", { dirname: dirnm, parent: parent1 } ); then in PHP (script.php) : // get the variables from $_POST $dir = $_POST['dirname']; $par = $_POST['parent']; $this->url(array('controller'=>'index','action'=>'dirnameex','dirname'=> $dir,'parent'=> $par)) http://api.jquery.com/jQuery.post/ and http://php.net/manual/en/reserved.variables.post.php

user1312776 at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

php runs server side, javascript runs client side so you can't do that. You can pass the variables to php using ajax. but this would be a seperate thread.

encodes

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.