Forward to different URL based on submitted keyword

Hi all

This is my first post here and I’m trying to learn to use PHP more efficiently. Currently I just manipulate existing open source code but would like to become a little more proficient with the language.

I’m attempting to create a script which I submit a form to. The form will consist of a simple, single text field with ID “submit”

I would like to be able to define a range of keywords that forward to a page, another range of keywords that forward to another page, and another range of keywords that forwards to another page.

For testing, I’ve currenty got this far:

[php]<?php

$URL=“http://www.itseasier.co.uk”;
$URL2=“http://www.macbookscreenreplacement.com”;
$URL3=“http://www.iq-pc.com/index.php”;

$submit = $_POST[‘submit’] ;

if
($submit == ‘testsearch2’ OR $submit == ‘testsearch3’);
{
echo header (“Location: $URL3”);
}

if
($submit == ‘testsearch’ OR $submit == ‘testsearch1’);
{
echo header (“Location: $URL”);
}

?> [/php]

Now, what this does is, regardless of what is submitted into the form, forward to $URL. That workds, but I need it to vary this URL depending on the input

Any help greatly appreciated.

Many thanks

Hello, and welcome!

You do not need echo before the header() function call. Also, after sending redirection header, I’d suggest to terminate execution of your script with the exit function call, like this:
[php]$keywords3 = array(‘testsearch2’, ‘testsearch3’);
if (in_array($submit, $keywords3));
{
header (“Location: $URL3”);
exit;
}[/php]

Many thanks for this, and sorry for being such a novice, but I seem to be getting the same result

I’m not using the following:

[php]<?php

$URL = “http://www.itseasier.co.uk”;
$URL2 = “http://www.macbookscreenreplacement.com”;
$URL3 = “http://www.iq-pc.com/index.php”;

$submit = $_POST[‘submit’] ;

//if
//($submit == ‘testsearch2’ OR $submit == ‘testsearch3’);
//{
// echo header (“Location: $URL3”);
//}

//if
//($submit == ‘testsearch’ OR $submit == ‘testsearch1’);
//{
// echo header (“Location: $URL”);
//}

$keywords2 = array(‘testsearch’, ‘testsearch1’);
$keywords3 = array(‘testsearch2’, ‘testsearch3’);

if (in_array($submit, $keywords2));
{
header (“Location: $URL2”);
exit;

}
if (in_array($submit, $keywords3));
{
header (“Location: $URL3”);
exit;

}

?> [/php]

With this, it doesn’t matter what I type in the box, I get taken to the $URL2 path

Probably something simple now and any help much appreciated again.

Sorry that should read ‘now using the following’ instead of ‘not’

Yes, I overlooked in your first post, and just did copy/paste your code - there is extra semicolon after each if statement - you need to delete it. Here is how your conditions should look:
[php]if (in_array($submit, $keywords2))
{
header (“Location: $URL2”);
exit;
}

if (in_array($submit, $keywords3))
{
header (“Location: $URL3”);
exit;
}
[/php]

And just to clarify, the exit needed only if there is some other code below the redirection header() function calls. In your case code would work fine without those exit’s too - the problem was in semicolons.

That’s a beautiful thing… Working perfectly.

Many thanks for your help with this

Damn semi colons!

Sponsor our Newsletter | Privacy Policy | Terms of Service