Jump to content

Form link


akabugeyes

Recommended Posts

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:
[code]index.php?action=filter&platform%5B%5D=DS&platform%5B%5D=GBA&genre%5B%5D=RPG&genre%5B%5D=Adventure&submit=Submit[/code]

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. :)
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

Well in Filter.php I have:

[code] 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']);[/code]

And the template code looks like this:

[code] <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>';[/code]

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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:
[code]<?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>[/code]
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.
Link to comment
Share on other sites

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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.