Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use javascript variable in php code [duplicate]

I have been working with a form submission in joomla and have a few trouble using javascript variable with php JRoute_

Here is the code snippet:

function ValidateForm()
{
    var loc = $("#locality").val();
    var action = '<?php echo JRoute::_('index.php?option=com_add&view=malllists&Itemid=233&districtname='.$dit.'&loc='Pass javascript Variable Here) ;?>';
    document.miniSurveyView481.action = action;
}

In the place of Pass javascript Variable Here I need to pass the loc value I tried plenty of things but Could not find a solution.

like image 415
Amit Avatar asked Nov 26 '22 15:11

Amit


1 Answers

You can't use javascript variable in php code. Php code run's on the serverside and javascript code runs in the client side. You can't ask the browser to run php code.

Your variable loc will have a value only when the code reaches the browser.

If you want to get some value from server and combine it with javascript variables then do the following.

Use an ajax request and send the desired values to server. The server will return with a response. Use that response text and store it in your action variable.

like image 160
Konza Avatar answered Dec 28 '22 08:12

Konza