Topic: PHP to get a variable to be used in my target URL

I'm running ExpressionEngine but I don't think I can use it's native power to accomplish what I want.

I have a form with 3 select fields. I want to take the values of the three fields (all of which will be integers) and combine them together, separated only by pipe characters. Then I want to use those results as part a url segment for the next page.

For example.

<form>
	<fieldset>
		<legend>When</legend>
		<select name="when">
			<option value="1">hello</option>
		</select>
	</fieldset>

	<fieldset>
		<legend>What</legend>
		<select name="what">
			<option value="2">hi</option>
		</select>
	</fieldset>

	<fieldset>
		<legend>Who</legend>
		<select name="who">
			<option value="3">hello</option>
		</select>
	</fieldset>

	<input type='submit' value='Search' />
</form>

I'd obviously have more option fields but for this example, I'll keep it simple.

So when the user clicks the submit button, I'd like to pull the three values from my form "1", "2" and "3" then place them together, separated only by pipe characters. So I'd have "1|2|3". I would then like to take that string (I think that is what you'd call it) and add it to the URL that the user would be going to. So the user would be taken to "mysite.com/1|2|3"

Now I've done very little PHP, so I don't know how hard or easy it is, or even if PHP is the best choice, but if anyone has any ideas, I would GREATLY APPRECIATE YOUR HELP.

Thanks!

Re: PHP to get a variable to be used in my target URL

I think what I would do, personally, is create a quick little page which only functioned as a redirect. So, you pass the values over to that page, compile them into a variable and use header('Location: whateverurl'.$variable) to redirect to your real URL. You could use the form page itself to compile the results through a few if statements and hidden inputs, that would be your choice. Theoretically the user shouldn't even know they are being redirected if your server can keep up with the load of traffic. Hopefully that helps you out a bit.

Re: PHP to get a variable to be used in my target URL

Jim,
The best way to do this is either use luz's solution above or to use Javascript to get the values of the select boxes when they're changed and dynamically change the form's action. Unfortunately, I don't have the time right now to type out how to do that. Let me know if you need help though and I'd be glad to write something up this evening.

Josh

Web Developer - AM Design | I specialize in XHTML, CSS, PHP and jQuery

Re: PHP to get a variable to be used in my target URL

I'm not comfortable at all with PHP and have little JS experience. I've worked with JQuery a little bit, but overall I'm looking for the easiest way to accomplish something like this.

If you have time to write up what you're thinking, I really appreciate it. I plan on spending the day trying to figure out how to make this work, so I'll post something if I get anywhere, so that you don't waste your time.

Thanks!

Re: PHP to get a variable to be used in my target URL

Jim -

Josh is correct about the JavaScript taking care of the solution without a a redirect page needing to be involved.  Keep in mind, though, that if the visitor has JavaScript disabled, so would your pre-processing of the destination.  You're safer, in the long run, to use the suggestion that luz cannon had.  The interim page could be as simple as:

<?php $path = $when . "|" . $what . "|" . $who; header("Location: " . $path); ?>
Honored to Serve for Him - Tom ('Mas) Pickering <)><

Re: PHP to get a variable to be used in my target URL

Great. I think I've got it working now. I'll post update if more help is needed.

Thanks guys!