Form re-direct to a different URL

Okay apologies this is’nt technically PHP yet I trying to develop a simple select form that will action another url and fulfill the request of the option selected.

The site is http://www.ding.com/

The pages that each option will open are;

a-distirct
b-district

So option a on the form would take the user to http://www.ding.com/a-district

I cant seem to get this right heres the form markup I’m toying with;


<form id="select" action="http://ding.com" method="get">
	<select name="/">
		<option value="">Please choose</option>				
		<option value="a-district">A district</option>	
		<option value="b-district">B-District</option>	
</select>
	<input type="submit" value="Go" />
</form>


The resulting url is showing “/?%2F=a-district”…

Just noticed ding.com is being held - i’m only using this as an example…

Hi there,

You could do this with JavaScript.
Would using JS be a solution you would consider?
If so, let me know and I’ll provide you with an example.

Edit: Just to be clear, the reason I ask is because this would disable this functionality for anyone who had JS switched off.

Pullo JS would be perfect. An example would be excellent - thanks!

You could always use JS and a normal form that posts to a php script which in turn redirects to the correct URL.
In any case, just a <select> without any scripting isn’t going to cut it.


<form id="select" action="/choose-district.php" method="get">
	<select name="disctrict" id="district">
		<option value="">Please choose</option>				
		<option value="a">A district</option>	
		<option value="b">B-District</option>	
        </select>
	<input type="submit" value="Go" />
</form>
<script type="text/javascript">
document.getElementById('district').onChange = function() {
    var elem = document.getElementById("district");
    var selected = elem.options[elem.selectedIndex].value;
    if (selected != '') {
        window.location = '/' + selected + '-district';
    }
}
</script>

choose-district.php


<?php
if (isset($_GET['district']) && $_GET['district'] != '')
{
    // district selected
    $district = $_GET['district'];
    header("Location: http://www.example.com/$disctrict-district");
    exit;
}
// user didn't select a disctrict, redirect to the homepage
header('Location: http://ww.example.com/');

How would you best do this Rémon?
Using cURL?

See my previous post. I’ve added a lot in an edit. :slight_smile:

Pullo: His second code block is the PHP page for what he described.

I think what you’re thinking of is an AJAX call, which is essentially a combination of the two methods.

EDIT: Too many people posting at once ! lol :smiley:

The PHP solution is nice.
Thank you!

Starlion, nah, I was thinking in too complicated terms, as usual :slight_smile:

Okay implemented ScallioXTX code yet I’m only getting -district ?

My code;

HTML


<script type="text/javascript">
document.getElementById('district').onChange = function() {
    var elem = document.getElementById("district");
    var selected = elem.options[elem.selectedIndex].value;
    if (selected != '') {
        window.location = '/' + selected + '-district';
    }
}
</script>

<form id="select" action="redirect.php" method="get">
	<select name="district" id="district">
		<option value="">Please choose</option>				
		<option value="a">blah</option>	
		<option value="b">blah</option>		
	</select>
	<input type="submit" value="Go" />
</form>

redirect.php


 <?php
if (isset($_GET['district']) && $_GET['district'] != '')
{
    // district selected
    $district = $_GET['district'];
    header("Location: http://www.ding.com/$disctrict-district");
    exit;
}
// user didn't select a disctrict, redirect to the homepage
header('Location: http://www.ding.com/'); 
?>

There is a typo in this code, see $district vs $disctrict

Okay I’m a numpty - thanks all changed the php and select options

 <?php
if (isset($_GET['district']) && $_GET['district'] != '')
{
    // district selected
    $district = $_GET['district'];
    header("Location: http://www.ding.com/$district");
    exit;
}
// user didn't select a disctrict, redirect to the homepage
header('Location: http://www.ding.com/'); 
?>

Select option is now…


<option value="b">blah</option>

THANKS TO ALL OF YOU!

PS I’ve another more complicated one which I’d love to figure out - anyone interested PM me…

It’s probably best to start a new thread.