Welcome Guest, Not a member yet? Register   Sign In
form creation and passing variables
#1

[eluser]arcology29[/eluser]
hi everyone, first of all I've been a couple of months with CI and I love it.

never had problems though today I've been bashing my head trying to understand how to pass variables from a form with a segmented url. I have done it through POST and then using

Code:
this->input->post('myFormElementName');

to retrieve data and works fine, but now I realize I want the info on the URL and since CI discourages GET parameters I have tried to use the segmented option. But can't find anywhere how to do it. Do I have to build the segmented url myself with a helper? If this is the case where do you do it?

Basically I don't understand how to get the data from my form to my controller (passing it with segmented urls).
#2

[eluser]Nick_MyShuitings[/eluser]
So, first off I'm not sure I understand what you're asking.

Lets start with assumptions then:

Code:
<form action="mycontroller/submit" method="post">
<input type="text" name="test" id="test" value="testing"/>
<input type="submit"/></form>

and in your controller you'd have something like:

Code:
function submit() {
$testing = $this->input->post('test');
}

From there I am not sure what you want to do, but it sounds like you want to redirect to a url that makes use of the info submitted by the form. If that is the case check out the URL helper http://ellislab.com/codeigniter/user-gui...elper.html Adding the following to the example controller would send you to a url that makes use of the info submitted by the form.

Code:
redirect('/controller/method/'.$testing, 'location');

If I am totally off base then please provide a bit more clarity into what you are trying to achieve.
#3

[eluser]arcology29[/eluser]
I'm not sure but I think you got it right. The thing is that since learning that CI discourages the use of GET, I thought it would provide an easy alternative to create the segmented urls. Basically to have this:

http://www.mysite.com/index.php/MyContro...ar2=hello2

but in it's segmented version (automatically):

http://www.mysite.com/index.php/MyContro...lo1/hello2

and then using
Code:
$myValue = $this->uri->segment(1);
to retrieve values.

But by what you are saying it seems theres is no short way to do it? You have to send your form data through POST always, encode the url yourself in a function and then redirect to another function to work with the data?
I thought there would be a way where you don't have to create an additional function since when you are using GET the url is created for you.
#4

[eluser]Nick_MyShuitings[/eluser]
What I don't get really is your USE CASE. For example, if you are sending a form and want to work with that data in a controller, just have the form submit the data to that controller/method. There is no need to have it submit it to one spot, use the values whether get or post to make a url, and then redirect, retrieve the values from the URI and then do stuff with them... that just doesn't make any sense to me and it seems like what you are requesting.

I just don't understand what it is that you are trying to do or why. If you really really for some odd reason need the form to submit to http://example.com/MyController/MyFuncti...FromInput2 then you could use Javascript to dynamically change the form action so that when the person hits submit it will just redirect them to that method... but once again that makes no sense from a logical standpoint nor from a "write less code" standpoint, when you could just post to that method and get the values out.

Please provide a USER STORY for what you're trying to do, with a few steps:

1) user goes to Controller/Method1 and see the form
2) user submits the form to Controller/Method2...

And that's where I lose your logic.
#5

[eluser]arcology29[/eluser]
[quote author="Nick_MyShuitings" date="1284186994"]What I don't get really is your USE CASE. For example, if you are sending a form and want to work with that data in a controller, just have the form submit the data to that controller/method. There is no need to have it submit it to one spot, use the values whether get or post to make a url, and then redirect, retrieve the values from the URI and then do stuff with them... that just doesn't make any sense to me and it seems like what you are requesting.[/quote]

You got it right what I want to do, now let me explain the reasons behind. The thing is I am creating a vacation website, which shows hotel information. I want to have the variables on the URL because let's suppose you find a nice hotel you want to show to a friend, so you give him the URL to show it to him, but without the variables on the URL it will just show your friend the vacation site main page.
Just like in youtube, i.e. http://www.youtube.com/watch?v=2fEkcht8TCo the video info on the last part via Get, but since I am using Post you can't share hotel info through a URL. Of course I can make CI work with Get, but I wanted to see if I can avoid it just because CI discourages it (no other reason really), and tells you to use segmented urls.

I hope that explains clearly my reasoning, and have in mind I'm a newbie to CI so maybe I'm missing something here.
#6

[eluser]Nick_MyShuitings[/eluser]
I think I understand better now, you want the person to be able to grab the url from the address bar after their search and be able to copy and paste it to a friend.

First: If you really want to do that, then you'd need to either do the javascript way I mentioned, or the redirect way.

Second: What I'd actually recommend, is a better User Experience in that you would do the post normal to Controller/Method which would handle the search. Then in the new page that is shown you would have some links like the standard share links, which would have a url that they can copy and paste, a button to send that url via twitter and facebook, a button that would pop up a little email form so that they can send that url to their friends etc. That type of user experience is considered "newer" and more "friendly" and it has the added advantage of allowing you to track some additional statistics about how many people share the url.

Hope that helps.
#7

[eluser]arcology29[/eluser]
I guess I will use the standard share links, hadn't thought about that I guess I just followed along with what the designer sent me without looking into other options.

Thank you Nick you've been of great help.




Theme © iAndrew 2016 - Forum software by © MyBB