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.

3
  • 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 is a technique not a language Commented Apr 30, 2012 at 9:41
  • You will need to use ajax to send variables from AJAX to PHP Commented Apr 30, 2012 at 9:41
  • possible duplicate of pass a js variable to a php variable Commented Apr 30, 2012 at 9:46

2 Answers 2

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)

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

6 Comments

Thanks ManseUK but this code in jquery & javascript for focusout event. so can i do?
just put the JavaScript code within the focusout callback - $('#yourid').focusout(function() { // here });
actually i got this value using JavaScript code within the focusout callback. but i want pass this value to php array
@user1312776 i dont understand your request ... update you question with the full code and explain what you want to happen ... both PHP and JavaScript
how to pass javascript jQuery variable value in php array?
|
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.

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.