0

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.

user1312776
  • 11
  • 1
  • 6
  • this wont work JavaScript is executed in the browser and PHP executed on the server - you need to POST the variables from JavaScript to PHP and `dirnm` and `parent1` are JavaScript variables on AJAX variables ... [AJAX](http://en.wikipedia.org/wiki/Ajax_(programming)) is a technique not a language – Manse Apr 30 '12 at 09:41
  • You will need to use ajax to send variables from AJAX to PHP – 472084 Apr 30 '12 at 09:41
  • possible duplicate of [pass a js variable to a php variable](http://stackoverflow.com/questions/4716177/pass-a-js-variable-to-a-php-variable) – JJJ Apr 30 '12 at 09:46

2 Answers2

1

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))

Docs here for $.post() (jQuery) and Docs here for $_POST (PHP)

Manse
  • 37,765
  • 10
  • 83
  • 108
  • Thanks ManseUK but this code in jquery & javascript for focusout event. so can i do? – user1312776 Apr 30 '12 at 09:57
  • just put the JavaScript code within the focusout callback - `$('#yourid').focusout(function() { // here });` – Manse Apr 30 '12 at 10:01
  • actually i got this value using JavaScript code within the focusout callback. but i want pass this value to php array – user1312776 Apr 30 '12 at 10:09
  • @user1312776 i dont understand your request ... update you question with the full code and explain what you want to happen ... both PHP and JavaScript – Manse Apr 30 '12 at 10:27
  • how to pass javascript jQuery variable value in php array? – user1312776 Apr 30 '12 at 10:42
  • http://stackoverflow.com/questions/10382968/how-to-pass-javascript-jquery-variable-value-in-php-array Please check this link – user1312776 Apr 30 '12 at 11:41
0

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
  • 741
  • 4
  • 18