Forum Moderators: coopster
form would be something like this
promoform.php
<?php
if (isset($errmsg)) echo '<p>',$errmsg;
?>
<p><form name="mypromo" method="get" action="checkpromo.php">
<input type="text" name="pcode" size="20"> <input type="submit" value="Go">
</form>
now the checkpromo.php needs to be in the same directory, it would look something like this
<?php
$errmsg = "";
if (empty($_POST['pcode']) ¦¦!isset($_POST['pcode'])) {
$errmsg = 'Please enter a promotional code';
include 'promoform.php';
} else {
$promocode = trim(strtolower($_POST['pcode']));
switch($promocode) {
case "paper":
$redirectpage = 'paper.php';
break;
case "radio":
$redirectpage = 'radio.php';
break;
case "magazine":
$redirectpage = 'magazine.php';
break;
}
header("Location: $redirectpage");
}
?>
Can anyone help with the format of this code above that Jatar suggested to redirect the user to a different page after they enter in the promo code? I don't think it's difficult for you experts, but I'm just a newbie.
It's javascript, yea, but it works!
- Pete
switch($promocode) {
case "paper":
$redirectpage = 'paper.php';
break;
case "radio":
$redirectpage = 'radio.php';
break;
case "magazine":
$redirectpage = 'magazine.php';
break;
}
so our switch here looks at the $promocode that was submitted from the user.
switch [php.net]($promocode)
so depending on the value of $promocode it will execute different code. I used examples of possible codes and pages to explain the code. So let's look at a single case within our switch
our user types in the word paper in the promocode form and submits it. The script finds that the value of paper was entered and when it gets to the switch knows to execute this code
case "paper":
$redirectpage = 'paper.php';
break;
so the above code would be read like so
in the case that the variable $promocode has a value of 'paper' then set the value of $redirectpage to 'paper.php' and then break out of the switch.
so in this example we assign 'paper.php' as the page we want them sent to for that promo code.
then once the switch is done it sends them off to the appropriate page using
header("Location: $redirectpage");
so all you need to do is create a case for each promo code and then put the path to the page into the $redirectpage var as I have shown.
make sense?
don't worry, my pleasure
>> sample code would be so that if the user entered the promo code 'paper' they would be automatically forwarded to www.widget.com
as follows
<?php
$errmsg = "";
if (empty($_POST['pcode']) ¦¦!isset($_POST['pcode'])) {
$errmsg = 'Please enter a promotional code';
include 'promoform.php';
} else {
$promocode = trim(strtolower($_POST['pcode']));
if ($promocode == "paper") {
$redirectpage = 'paper.php';
} else {
$errmsg = 'Please enter a valid promotional code';
include 'promoform.php';
}
header("Location: $redirectpage");
}
?>
This is what accomplishes the redirection.
$redirectpage can be 'http://www.widgets.com/paper.php' if you want, it's up to you.
header takse a string of the format "Location: [new.location.com",...] or "Location: ./new_location.php", or "Location: /new/location.php"
Anything that's valid in a HTTP 302 header is fine although I just read HTTP/1.1 requires an absolute URL so I would code your redirect as;
header( "Location: $redirectpage");
Where $redirectpage = something like
"http://www.widgets.com/subdirectory/paper.php"
Yes, you need the quotes and parentheses as they appear here.
HTH,
BAD
switch($promocode) {
case "paper":
$redirectpage = "http://www.widget.com");
break;
case "radio":
$redirectpage = 'radio.php';
break;
case "magazine":
$redirectpage = 'magazine.php';
break;
}
header("Location: $redirectpage");
}
When I enter 'radio' in the form and click the 'Submit' button it takes me to a white page that shows the code above. What could be wrong? I only have two .php files (promocode.php and checkpromo.php) I'm not getting redirected to www.widget.com
Any ideas why?
When you say you got a white page with the code displayed, did you forget to include the PHP tags?
<?php?>
Also, this line will give you a parse error:
$redirectpage = "http://www.widget.com");
Lose the closing bracket after .com"
Other observations:
1. You have a input type text field, in which a user enters data. How do you know if anyone is entering paper, radio or magazine? Should you not also have a select box?
<form name="mypromo" method="post" action="checkpromo.php">
<select name="type">
<option value="paper">Paper</option>
<option value="radio">Radio</option>
<option value="magazine">Magazine</option>
</select>
<input type="text" name="pcode" size="20">
<input type="submit" value="Go">
</form>
Otherwise your switch statement could return nothing if something else is entered in the text box. Or am I missing something? If none of the options are going to be found, you should also add a default option to your switch statement. Using the select box will eliminate this.
Now try this with your switch
<?php
$errmsg = "";
if (empty($_POST['pcode']) ¦¦!isset($_POST['pcode'])) {
$errmsg = 'Please enter a promotional code';
include 'promoform.php';
} else {
switch($_POST['type']) {
case "paper":
$redirectpage = 'http://www.google.com';
break;
case "radio":
$redirectpage = 'http://www.widget.com';
break;
case "magazine":
$redirectpage = 'http://www.webmasterworld.com';
break;
}
header("Location: $redirectpage");
}
?>
This should be ok. If you are using your existing script, you are directing to paper.php, magazine.php or radio.php. Do you have pages created called these?
Is this making senses so far?
dc
[edited by: dreamcatcher at 11:27 pm (utc) on Mar. 3, 2005]
<?php
if (isset($errmsg)) echo '<p>',$errmsg;
?>
<p>Please enter your promo code<form name="mypromo" method="get" action="checkpromo.php">
<input type="text" name="pcode" size="20"> <input type="submit" value="Go">
</form>
Here is the checkpromo.php code:
<?php $errmsg = ""; if (empty($_POST['pcode']) ¦¦!isset($_POST['pcode'])) { $errmsg = 'Please enter a promotional code'; include 'promoform.php'; } else { $promocode = trim(strtolower($_POST['pcode'])); if ($promocode == "paper") { $redirectpage = 'http://www.widgets.com; } else { $errmsg = 'Please enter a valid promotional code'; include 'promoform.php'; } header("Location: [widgets.com");...] }?>
These are the only .php files I have on the server. Try that out on your end and see if that works for you.
<?php
if (isset($errmsg)) echo '<p>',$errmsg;
?>
<p>Please enter your promo code<form name="mypromo" method="get" action="checkpromo.php">
<input type="text" name="pcode" size="20"> <input type="submit" value="Submit">
</form>
I'll try it now and post the results...
I used the code below as you suggested:
<?php
$errmsg = "";
if (empty($_POST['pcode']) ¦¦!isset($_POST['pcode'])) {
$errmsg = 'Please enter a promotional code';
include 'promoform.php';
} else {
switch($_POST['type']) {
case "paper":
$redirectpage = 'http://www.google.com';
break;
case "radio":
$redirectpage = 'http://www.widget.com';
break;
case "magazine":
$redirectpage = 'http://www.webmasterworld.com';
break;
}
header("Location: $redirectpage");
}
?>
I also changed the "get" to "post" and STILL, when I enter in any of the promo codes it just takes me to a blank white page. If you need any other specifics, let me know, as I'm really tried to solve this. I didn't think it would be so tough! :o)
<?php
$errmsg = "";
if (empty($_POST['pcode']) ¦¦!isset($_POST['pcode'])) {
$errmsg = 'Please enter a promotional code';
include 'promoform.php';
} else {
switch(strtolower($_POST['type'])) {
case "paper":
header("Location: [google.com");...]
break;
case "radio":
header("Location: [widget.com");...]
break;
case "magazine":
header("Location: [webmasterworld.com");...]
break;
}
}
?>
dc
Ok, I ran the code on my local server and this line gave me a parse error:
if (empty($_POST['pcode']) ¦¦!isset($_POST['pcode']))
Try changing that to simply:
if (empty($_POST['pcode']))
:)
[moderator comment]Remember, you can't copy and paste the pipe ¦¦ from this forum into your code. It gets transformed and will cause a paste error. you must retype any pipes. This may or may not have caused Dreamcatcher's parse error[/moderator comment]
[edited by: ergophobe at 4:33 pm (utc) on Mar. 4, 2005]
[edit reason] Comment for the record [/edit]