Skip to content Skip to sidebar Skip to footer

Use Javascript Variable In Php Code

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 l

Solution 1:

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.

Solution 2:

functionValidateForm()
{
    var loc = $("#locality").val();
    var action = <?phpecho json_encode(JRoute::_('index.php?option=com_add&view=malllists&Itemid=233&districtname=' . $dit));?>';
    // Now action is the url  you want, but you need to add loc to it. 
    // If it already has a query string, do it like this:
    document.miniSurveyView481.action = action + '&loc=' + loc;
    // If it does not have a query string, do it like this:
    document.miniSurveyView481.action = action + '?loc=' + loc;
}

So the real question here is, "how do you determine if the url has a query string already?"

You might want to try this: http://medialize.github.io/URI.js/

Solution 3:

In Joomla Standard you have to try something like this.

From your code you're trying to set the action of the form from JavaScript.

In Joomla you have to create hidden fields for setting those url params like.

<inputtype='hidden' name='option' value='com_add' />
<inputtype='hidden' name='view' value='malllists' />
<inputtype='hidden' name='controller' value='malllists' />
<inputtype='hidden' name='task' value='yourtaskname' />

Once you set up these thing properly on form submission Joomla post the data to your controller file contain yourtaskname. These variable values can be easily changed from JavaScript using normal JavaScript/Jquery.

Then there is no need of using PHP codes inside JavaScript or something like that.

Hope it make sense..

Solution 4:

I think $dit is also php variable. Try to use below:-

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

Edit:-

for passing javascript value to php function I do below:-

<scripttype="text/javascript">functiontest()
{
    var pt = 3;
    document.write('<?php add("'+pt+'");?>');
}

test();
</script><?phpfunctionadd($param)
{
    echo$param;
}  
?>

and it's perfectly printing 3 .

So may be the below code will work for OP:-

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

Try once. Thanks.

Post a Comment for "Use Javascript Variable In Php Code"