Form Value to URL

#1
[eluser]mjo1337[/eluser]
Hi,

I'm converting a website into a Codeigniter based version

I have a form which allows you to select colour, size and texture. The form uses GET method and produces search.php?size=65&colour=red&txtr=rough which the search script uses to pick the variables.

How would i do this with CI? so i get something like this search/65/red/rough.

Thanks

#2
[eluser]tokyotech[/eluser]
I usually use $_POST for forms, but it can be done with the $_GET. You need a controller with a method that processes the URL:

Code:
Color extends Controller
{
   public function search
   {
      $size = $this->uri->segement(2);
      $colour = $this->uri->segement(3);
      $txtr = $this->uri->segement(4);

      printf('you entered %s %s %s', $size, $colour, $txtr);
   }
}

So then you call http://localhost/projectName/index.php/color/search/65/red/rough to use it.

#3
[eluser]pistolPete[/eluser]
I'd rather use this:

Code:
Color extends Controller
{
   public function search($size, $colour, $txtr)
   {
      printf('you entered %s %s %s', $size, $colour, $txtr);
   }
}

User guide

#4
[eluser]mjo1337[/eluser]
How do i get the form values into the URL segments to start with?

#5
[eluser]tokyotech[/eluser]
[quote author="pistolPete" date="1252195486"]I'd rather use this:

Code:
Color extends Controller
{
   public function search($size, $colour, $txtr)
   {
      printf('you entered %s %s %s', $size, $colour, $txtr);
   }
}

User guide[/quote]

So this puts the URI segements into the parameters? Interesting. I never knew that! Definitely more traditional style programming and easier for other people to read your code.

#6
[eluser]tokyotech[/eluser]
[quote author="mjo1337" date="1252196791"]How do i get the form values into the URL segments to start with?[/quote]

Using GET for forms is quite awkward, but if you insist:

Code:
$this->load->helper('url');
redirect(sprintf('color/search/%s/%s/%s', $size, $color, $texture ), 'refresh');

#7
[eluser]mjo1337[/eluser]
Suppose I could do it this way? POST from the form to begin with then add those values to the navigation links so 'next' would be

Code:
'<a href="/search/' . $clr . '/' . $size . '/' . $txtr . '">Next</a>';

I could check to see if POST values are present, use those, otherwise use URI segments.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.