5

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.

2
  • Read this: stackoverflow.com/questions/2379224/… Feb 12, 2014 at 7:36
  • you need to improve the formulation of question. The quotes are unnecessarily causing confusion. Also echo values must be in quotes. Please try to convey exactly what you want!
    – Puneet
    Feb 12, 2014 at 7:40

4 Answers 4

6

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.

1
  • Is there any alternative solution for this???
    – Amit
    Feb 12, 2014 at 7:54
0
function ValidateForm()
{
    var loc = $("#locality").val();
    var action = <?php echo 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/

3
  • I need to pass the loc value to JRoute Okonomiyaki3000 in order to have a sef url..
    – Amit
    Feb 12, 2014 at 8:03
  • It's not really all that important that URLs used by javascript be SEF but if it's important to YOU that they be so, then you should create a service that you can pass some variables with an ajax request and it will run JRoute for you and return the SEF url to your javascript function. Kind of troublesome if you ask me. Feb 12, 2014 at 8:24
  • I'm not sure if it's available in the version of joomla that you're using but there's a component called com_ajax which you can use to do some interesting things. By itself, it does nothing. But you can write a plugin or a module (I'd recommend a plugin) for it. So let's say you write a plugin called plg_ajax_jroute which should have a function called onAjaxJroute. You would make a call to index.php?option=com_ajax&plugin=jroute&data=<yourSpecificDataHere> and then, in your plugin, handle that stuff. Sorry, I can't tell you exactly how to do it here but you've given me a good idea. Feb 12, 2014 at 9:16
0

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.

<input type='hidden' name='option' value='com_add' />
<input type='hidden' name='view' value='malllists' />
<input type='hidden' name='controller' value='malllists' />
<input type='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..

5
  • Down Voter should explain the reason.
    – Jobin
    Feb 12, 2014 at 9:16
  • 1
    I've upvoted you as the only person to use Joomla standards
    – Lodder
    Feb 12, 2014 at 9:19
  • It wasn't me, but I can tell you that this doesn't really address the problem in the question. If you read it more carefully, you'll understand. What he's doing might not be exactly the 'standard' joomla way but we have to assume he has some reasons for the way he is doing it. Feb 12, 2014 at 9:19
  • @Okonomiyaki3000 this much explanation is not sufficient for the answer! he just need to set another hidden fields with name loc and set the value using javascript inside validation code Job Done! he has some reason for doing this( may be ) .The fact is he needs to get the loc value as a post param. I think my answer is sufficient for his task. thanks.
    – Jobin
    Feb 12, 2014 at 9:25
  • 1
    I'm not saying your answer is wrong. In fact, it's how I'd probably do it. I'm just saying that it looks like he's doing some non-standard stuff here and whether he has a good reason for that or not is outside the scope of this question. Feb 12, 2014 at 9:36
-9

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=<?php echo $dit;?>&loc="+loc;
    var action = "<?php echo JRoute::_(url) ;?>";
    document.miniSurveyView481.action = action;
}

Edit:-

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

<script type="text/javascript">
function test()
{
    var pt = 3;
    document.write('<?php add("'+pt+'");?>');
}

test();
</script>

<?php

function add($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=<?php echo $dit;?>&loc="+loc;
    var action = '<?php echo JRoute::_("'+url+'") ;?>';
    document.miniSurveyView481.action = action;
}

Try once. Thanks.

6
  • How are you gonna run the php function JRoute::_() on the javascript variable url? Feb 12, 2014 at 7:51
  • @Okonomiyaki3000 , :( I also realize the actual problem after posting answer. Sorry for bad answer.
    – Ripa Saha
    Feb 12, 2014 at 7:56
  • @Okonomiyaki3000 Your above comment not clear to me. are u ordering me to stop editing my answer? have u tried any one of my edited code?
    – Ripa Saha
    Feb 12, 2014 at 8:47
  • 1
    What you wrote is completely wrong. PHP is run on the server side, JavaScript on the client. At the moment the PHP code runs, the pt variable doesn't exist. It only seems to work, because what the PHP sees is add("'+pt+'");, i.e. you are passing the string "'+pt+'" to the function, which just outputs it again. Hence the generated JavaScript code will be document.write(''+pt+'');. So when this code runs, it will indeed print 3, but not because the JS variable was passed to PHP, but because the PHP code generated JavaScript that makes use of the variable pt. Feb 12, 2014 at 9:02
  • Just have a look at the generated HTML returned by the server and you will see. Maybe it's more obvious for you if you omit the " quotation marks: document.write('<?php add('+pt+');?>');. The generated JS code will then be document.write('+pt+');, which literally prints the string +pt+. Feb 12, 2014 at 9:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.