Jump to content


Photo

Form link


  • Please log in to reply
9 replies to this topic

#1 akabugeyes

akabugeyes
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 15 July 2006 - 03:43 PM

Hi, I have created a filter system for a program I am creating that allows the users to select items from a multiple select box and see results with columns that contain one of the selected choices.

That much I have working. It is just when I want to use method="get" so it can be linked to, the url looks really bad.

With the current system the url comes out like this:
index.php?action=filter&platform%5B%5D=DS&platform%5B%5D=GBA&genre%5B%5D=RPG&genre%5B%5D=Adventure&submit=Submit

Where the options for the platform select: 'DS' and 'GBA' have been selected and the options for the genre select: 'RPG' and 'Adventure' have been selected.

What I would ideally like to do is get rid of all those %5B%5D characters in the url, I think it may have something to do with the use of making the name of selects an array (<select name="name[]"). I would also like to get rid of the submit=Submit if possible and condense the different selections into a comma separated list.

So the url could look something like:

index.php?action=filter&platform=DS,GBA&genre=Adventure,RPG

Does anyone know what could help me accomplish this? Would this even be possible by using the method="get" or would I perhaps need to use method="post" and maybe redirect using the header() function to the proper place?

Any advice would be appreciated. :)

#2 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 15 July 2006 - 03:49 PM

Whenever you put symbols in links (suchs as commas) it was automatically encode the URL properly...hence the reason you have all those funky things in there.

How are you appending the variables to the link. Can you post some code?

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#3 akabugeyes

akabugeyes
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 15 July 2006 - 04:01 PM

Well in Filter.php I have:

foreach($_GET['genre'] as $gen)
		{
			$filter['genre'][] = $gen;
		}

		$filter['genre'] = implode(',',$filter['genre']);


		// Same routine for platform! ;)
		$filter['platform'] = array();

		foreach($_GET['platform'] as $plat)
		{
			$filter['platform'][] = $plat;
		}

		$filter['platform'] = implode(',',$filter['platform']);

And the template code looks like this:

<form action="index.php" method="get">
		<fieldset class="row2">
		<legend>Do we have a platform to filter by?</legend>
		<input name="action" value="filter" type="hidden" />
			<select name="platform[]"  multiple="multiple">
				<option value="all" selected="selected" style="font-weight: bold;">All</option>
				<option value="DS">DS</option>
				<option value="GBA">GBA</option>
			</select>
	     </fieldset>';
	echo '
		<fieldset class="row2" style="margin-top: 10px;">
			<legend>How about filtering by genre?</legend>
			<select name="genre[]" multiple="multiple">
				<option value="all" selected="selected" style="font-weight: bold;">All</option>
				<option value="RPG">RPG</option>
				<option value="Adventure">Adventure</option>
			</select>
	      </fieldset>
	<input type="submit" value="Submit" name="submit" />
	</form>';

When I echo $filter['genre'] I get: RPG,Adventure and when I echo $filter['platform'] I get: DS,GBA, which they are used with FIND_IN_SET for the where part of my mysql query, but that doesn't really have to do with the link being produced.

#4 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 15 July 2006 - 04:02 PM

Even though the URL is ugly, does it actually work?

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#5 akabugeyes

akabugeyes
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 15 July 2006 - 04:04 PM

Yes it does work.

#6 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 15 July 2006 - 04:06 PM

Maybe try using different symbols to implode it with? Since the comma is looking funky, try a period or a plus sign.

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#7 akabugeyes

akabugeyes
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 15 July 2006 - 04:18 PM

Actually the implode doesn't really have anything to do with the url being produced.

The reason I use implode is so I can use it in the mysql query

$filter['where'] = "FIND_IN_SET(`for`, '{$filter['platform']}') AND FIND_IN_SET(`genre`, '{$filter['genre']}')";

Basically the url: index.php?action=filter&platform=DS,GBA&genre=Adventure,RPG is something I'd like to do but I have no current code that does anything for it to work.

Ignoring the characters being put in the url, the url currently looks like index.php?action=filter&platform=DS&platform=Adventure&genre=RPG&genre=Adventure

So if I can't get it to separate with a comma or plus sign, it would be nice to at least get rid of those extra characters getting produced in the url.

#8 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 15 July 2006 - 04:21 PM

I dont know WHY it would give you those wierd characters if you took the commas out. & is used to append the variables to the URL, so they shouldn't change...

Can you access the page through page.php?action=filter&platform=DS&platform=Adventure&genre=RPG&genre=Adventure

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#9 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 15 July 2006 - 04:37 PM

If you want to make the URL cleaner, why not submit the form to itself. The process the data into the url you want it to be, rather then how the browser formats the url. A bit like this:
<?php

if(isset($_POST['submit']))
{
    foreach($_POST['genre'] as $gen)
    {
        $filter['genre'][] = $gen;
    }

    $filter['genre'] = implode(',' , $filter['genre']);


    // Same routine for platform! ;)
    $filter['platform'] = array();

    foreach($_POST['platform'] as $plat)
    {
        $filter['platform'][] = $plat;
    }

    $filter['platform'] = implode(',' , $filter['platform']);

    // format our nice url
    header("Location: index.php?action=filter&genra={$filter['genre']}&platform={$filter['platform']}");
}

?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
   <fieldset class="row2">
    <legend>Do we have a platform to filter by?</legend>
    <input name="action" value="filter" type="hidden" />
	<select name="platform[]" multiple="multiple">
	  <option value="all" selected="selected" style="font-weight: bold;">All</option>
	  <option value="DS">DS</option>
	  <option value="GBA">GBA</option>
    </select>
  </fieldset>
  <fieldset class="row2" style="margin-top: 10px;">
    <legend>How about filtering by genre?</legend>
    <select name="genre[]" multiple="multiple">
      <option value="all" selected="selected" style="font-weight: bold;">All</option>
      <option value="RPG">RPG</option>
      <option value="Adventure">Adventure</option>
   </select>
  </fieldset>
  <input type="submit" value="Submit" name="submit" />
</form>
What it'll now do is process the submit data, and format the url so its like this:
index.php?action=filter$genra=blah,blah$platform=blah,blah

Then on the page you proces the data with you just have to use $_GET['genera'] and $_GET['platform'] whch you can now use in your database.

#10 akabugeyes

akabugeyes
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 15 July 2006 - 08:09 PM

Thanks for the help wildteen and pixy. :)

I have essentially gotten it working.

My urls are now starting to look like this:
index.php?action=filter&platform=DS,GBA&genre=RPG,Adventure

which is what I wanted, in part by taking the advice given by wildteen to use post instead of get and then use a header to redirect it.

I think the current way I have it coded is using a bit too much repitive coding, but I think I can work on fixing that. :)

Thanks for the support!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users