Welcome Guest, Not a member yet? Register   Sign In
passing variable from javascript to a controller via link

Hi Everyone,

I wonder if somone can share the knowledge. I need to pass a variable from Javascript to a controller via link. I was thinking along the lines of hidden inputs, but not sure if this can work. It should be possible to send the data in URL request i.e. <a > ...



Since URL's for CI are in the form of..


Yes you can pass a variable. Just make your variable the next part of the URI after the controller and method(function)

thanks for your post. I wonder if there is a less transparent way to send variables

What do you mean by "less transparent"?

If you want to submit it via a link then what jdfwarrior said is the right way, if you want it less transparent don't use a link, submit the form via post.

As I understand Post method will require submission button. I'd like to try to avoid new buttons as they don't align well with overall site design. I wonder if I could still use link, but send variable without exposing it in URL.

[eluser]Mushex Antaranian[/eluser]
I you're using js frameworks, it's pretty simple (and with pure js it's not so hard Wink )

In your markup
&lt;form id="the-form"&gt;
<label for="input">Input</label>&lt;input type="text" value="" name="input"/&gt;
<label for="input-2">Second Input</label>&lt;input type="text" value="" name="input-2"/&gt;
<a href="#" id="submit-the-form">Submit Form</a>

In js (!this is jQuery code)
$sumbitLink = $('#submit-the-form')
               url: 'url/to/post/',
               type: 'POST',
               // how you'll receive data controller outputs (can set as html, xml, json)
               dataType: 'json',
               data: $('#the-form').serialize(),
               success: function(data){
                            //do something with response
    }) // close $.ajax
}) //close .click

[eluser]Mushex Antaranian[/eluser]
Ooh, sorry, I get you wrong..
You can just style your submit button as link with css, if it is the problem Smile

Thanks a lot. Will try and let you know

Theme © iAndrew 2016 - Forum software by © MyBB