How do I make a dropdown option, appear in ShortURL?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do I make a dropdown option, appear in ShortURL?

Post by simonmlewis »

Code: Select all

<form method='POST' name='manufacturerselect' action='/manufacturer'>
<select name='manufacturer' onChange=\"document.forms['manufacturerselect'].submit()\" style='background-color: #000000; color: #ffffff; border: 0px'>  
<option value=''>Choose Manufacturer</option>
<option value='1'>One</option>
<option value='2'>Two</option>
</select>
</form>
Here is the form.
I want to be able to pass the data through, so the URL becomes http://www.domain.com/manufacturer/1

I don't know how to do this. I can do it with a hyperlink, but I really need this as a dropdown menu.

Perhaps it's with a GET method, but even then I don't know how to do it, specially with the shorturl code. If given some guidance I can possibly work the rest out.

UPDATE:
I've tried this in HTACCESS
RewriteRule ^manufacturers/([^/]+) /index.php?page=manufacturers&menu=home&manufacturer=$1 [L]
With the form code above, but it doesn't generate the URL like this. Or if it does, it does it like this:
http://domain.local/manufacturers/?manufacturer=CYMA

(using it locally for now)
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I make a dropdown option, appear in ShortURL?

Post by requinix »

Normal HTML forms cannot go directly to a "short URL". Your options include: using Javascript to go to that URL (I doubt you actually need a POSTed form); having /manufacturer?manufacturer=X redirect to /manufacturer/X.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I make a dropdown option, appear in ShortURL?

Post by simonmlewis »

How would I achieve the second one? When using that method it doesnseem to show the result of 'manufacturer' within the page
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I make a dropdown option, appear in ShortURL?

Post by requinix »

Change your form to method=get (because a POST isn't appropriate here) and then look at the REQUEST_URI:

Code: Select all

$querystring = parse_url($_SERVER["REQUEST_URI"], PHP_URL_QUERY);
parse_str($querystring, $get);
if ($_SERVER["REQUEST_METHOD"] == "GET" && $get && isset($get["manufacturer"])) {
    $id = $get["manufacturer"];
    unset($get["manufacturer"]);
    header("Location: " . strtok($_SERVER["REQUEST_URI"], "?") . ($get ? "?" . http_build_query($get) : ""));
    exit;
}
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I make a dropdown option, appear in ShortURL?

Post by simonmlewis »

Ok I now have the initial dropdown back to this:

Code: Select all

<form method='get' action='/manufacturers name='manufacturerselect'>
<select name='manufacturer'  onChange=\"this.form.submit()\">";
         
      $resultm = mysql_query ("SELECT DISTINCT manufacturer FROM products WHERE pause = 'off' AND manufacturer <> '' AND manufacturer <> 'SORRY NO INFO' ORDER BY manufacturer");
  while ($rowm = mysql_fetch_object($resultm))
    {
    echo "<option value='$rowm->manufacturer'>$rowm->manufacturer</option>";
    }

      echo "</select>
Where on manufacturers.inc do I put that REQUEST_URI code? I see it collect the manufacturer $get, but not what it does with it.

So far, quite ignorantly I have it like this:

Code: Select all

<?php 
$cookietype = $_COOKIE['type'];
if(isset($_GET['manufacturer']))
{
    $manufacturer = $_GET['manufacturer'];
    $_SESSION['manufacturer']=$manufacturer;
} else {
    $manufacturer=$_SESSION['manufacturer'];
}

$querystring = parse_url($_SERVER["REQUEST_URI"], PHP_URL_QUERY);
parse_str($querystring, $get);
if ($_SERVER["REQUEST_METHOD"] == "GET" && $get && isset($get["manufacturer"])) {
    $id = $get["manufacturer"];
    unset($get["manufacturer"]);
    header("Location: " . strtok($_SERVER["REQUEST_URI"], "?") . ($get ? "?" . http_build_query($get) : ""));
    exit;
}

But it doesn't do anything, and you probably think "well no, coz you haven't................".

What haven't I done??
As it seems your script looks at the URL, captures the word, then redirects to the proper URL in a "flicker".
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I make a dropdown option, appear in ShortURL?

Post by simonmlewis »

IT shows an error about modifying headers.
So I look at the URL and tried to see if I could capture the variable for $manufacturer, if I do $manufacturer = $_GET['manufacturer']; on
[text]http://domain.local/manufacturers?manufacturer=FRED[/text]
But it doesn't show anything on screen.

Is this because it isn't "passing it"? I have the form set to GET, not POST.
If I could capture that word, then the script would actually work, or I could just redirect them to /manufacturers/FRED very easily.

But I don't know why it isn't putting "FRED" into $manufacturer = $_GET['manufacturer'];.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I make a dropdown option, appear in ShortURL?

Post by simonmlewis »

I've altered it slightly.
It's now a POST method. It then collects what's posted and redirects the user to the same include file, via the SHORTURL.

Works a treat on both browsers.
But.... in IE9 when the dropdown is not clicked (or after just clicking and letting go) it appears like this.
In Firefox it does of course show it properly.

I've set the <select> to be

Code: Select all

<select name='man'  onChange=\"this.form.submit()\" style='background-color: #000000; color: #ffffff; border: 1px solid #000000; font-size: 12px; width: 196px'>
Am I missing something in here to get rid of that border in IE??
Attachments
&lt;select&gt; shows like this in IE9 when not clicked.
<select> shows like this in IE9 when not clicked.
dd.png (1.1 KiB) Viewed 1552 times
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I make a dropdown option, appear in ShortURL?

Post by simonmlewis »

I've read many blogs about this, and it seems it's been an IE Bug for years.
It's now (just) 2013. Is that bug STILL going on???
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I make a dropdown option, appear in ShortURL?

Post by requinix »

You lost me.

Are you still getting the header errors? You can't redirect with a header() if there's been any output of any kind anywhere, and the error message will tell you when something was outputted (it may be an error message stemming from that line).

Are you able to get the manufacturer? The code I gave you should be very early on in the script, ideally the very first thing it does (since there's no point doing any work if the script is going to redirect someplace) but where you've shown it is fine. What URL does the browser show? Did you use something to follow the redirects? The form should go to /manufacturers?manufacturer=whatever and then redirect to /manufacturers/whatever.

What border? How does Firefox show it?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I make a dropdown option, appear in ShortURL?

Post by simonmlewis »

Ok it's sorted now. Used another script I found that is CSS based, does not use dropdowns and IE is a right pain in the ........... with it's bug that MS has never fixed.

Firefox doesn't show it. But search for it on Google and you will find countless developers reporting it, and microsoft doing nothing about it.

With your script, in the end I did try (and it worked) a dropdown that then captured the word, and redirected to a shorturl page. But because of the border issue, I had to scrap it.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply