CodeIgniter Forums
passing variable from javascript to a controller via link - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: passing variable from javascript to a controller via link (/showthread.php?tid=18930)



passing variable from javascript to a controller via link - El Forum - 05-22-2009

[eluser]agubski[/eluser]
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 > ...

Thanks

Alex


passing variable from javascript to a controller via link - El Forum - 05-22-2009

[eluser]jdfwarrior[/eluser]
Since URL's for CI are in the form of..

http://www.example.com/controller/method/arg1/arg2/etc/

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


passing variable from javascript to a controller via link - El Forum - 05-22-2009

[eluser]agubski[/eluser]
thanks for your post. I wonder if there is a less transparent way to send variables


passing variable from javascript to a controller via link - El Forum - 05-22-2009

[eluser]TheFuzzy0ne[/eluser]
What do you mean by "less transparent"?


passing variable from javascript to a controller via link - El Forum - 05-22-2009

[eluser]kgill[/eluser]
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.


passing variable from javascript to a controller via link - El Forum - 05-22-2009

[eluser]agubski[/eluser]
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.


passing variable from javascript to a controller via link - El Forum - 05-22-2009

[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
Code:
&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;
&lt;/form&gt;
<a href="#" id="submit-the-form">Submit Form</a>

In js (!this is jQuery code)
Code:
$sumbitLink = $('#submit-the-form')
$submitLink.click(function(){
    $.ajax({
               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



passing variable from javascript to a controller via link - El Forum - 05-22-2009

[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


passing variable from javascript to a controller via link - El Forum - 05-22-2009

[eluser]agubski[/eluser]
Thanks a lot. Will try and let you know