Forum Moderators: coopster

Message Too Old, No Replies

Quick form question

         

Bubzeebub

3:35 pm on Feb 28, 2005 (gmt 0)

10+ Year Member



I have a form that I would like users to use to enter promo codes. After the correct promo code is used, they would then be redirected to another internal page. Jatar was very helpful in providing me with the following code (Thanks Jatar!) but I still need to know how and where to change it to redirect the user:

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.

FedExin

5:27 pm on Feb 28, 2005 (gmt 0)

10+ Year Member



<?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;
}
<script>document.location="$redirectpage";</script>
}
?>

It's javascript, yea, but it works!

- Pete

jatar_k

6:21 pm on Feb 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



let me break it down a bit more then

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?

Bubzeebub

2:44 am on Mar 1, 2005 (gmt 0)

10+ Year Member



wait....you lost me. Are you saying that once the user enters 'paper' that then the computer checks paper.php? From what I understand then a line in paper.php would redirect them to another page with the "Header" line? Correct me if I'm wrong. Can you show me what sample code would be so that if the user entered the promo code 'paper' they would be automatically forwarded to www.widget.com for example. I'm a complete newbie, so please, bear with me.

jatar_k

5:24 am on Mar 1, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> I'm a complete newbie, so please, bear with me.

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");
}
?>

Bubzeebub

3:07 am on Mar 2, 2005 (gmt 0)

10+ Year Member



wait...so would I have to change the header line to:

header("www.widget.com");

Do I need the quotes? Do I need the parentheses? I just don't see anything in the code there referencing www.widget.com

badone

6:15 am on Mar 2, 2005 (gmt 0)

10+ Year Member



switch($promocode) {
case "paper":
$redirectpage = 'paper.php';
break;
case "radio":
$redirectpage = 'radio.php';
break;
case "magazine":
$redirectpage = 'magazine.php';
break;
}
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

badone

6:28 am on Mar 2, 2005 (gmt 0)

10+ Year Member



Bubzeebub:

If you want some help with this, sticky me.

Cheers,
BAD

Bubzeebub

3:28 pm on Mar 3, 2005 (gmt 0)

10+ Year Member



I'm going to give this a shot now and see how this pans out...

Bubzeebub

4:05 pm on Mar 3, 2005 (gmt 0)

10+ Year Member



I don't know what I'm doing wrong. It's still not working. This is the code I'm using:

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?

jatar_k

5:28 pm on Mar 3, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



when you type in radio it switches the $redirectpage to radio.php so you won't get redirected to widgets.com, you will get redirected to a page called radio.php in the directory that you are in.

Bubzeebub

5:35 pm on Mar 3, 2005 (gmt 0)

10+ Year Member



ohh so I'm going to have to create a corresponding .php file for each of the promo codes? (radio.php, paper.php, etc)?

What should those files contain?

jatar_k

6:44 pm on Mar 3, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> What should those files contain?

no clue, it was an example, what are they entering different promo codes for anyway? Whatever they need based on the entered promo code.

Bubzeebub

9:26 pm on Mar 3, 2005 (gmt 0)

10+ Year Member



they're entering those codes so I know what info or page they should see. I don't understand why after they enter a code ('radio' for example) that they aren't just redirected to another page that I want to send them to (www.widget.com for example). What's the point of sending them to radio.php?

jatar_k

11:15 pm on Mar 3, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you can send them where ever you like

different domain
different page
product specific page for that code
tuned product listing for that code

doesn't matter to me. Do you want every code going to the same place? Or are you looking more for a specific page/product/promotion per code?

dreamcatcher

11:20 pm on Mar 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

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]

Bubzeebub

11:23 pm on Mar 3, 2005 (gmt 0)

10+ Year Member



Yup, every code entered would lead to a different page. If I can just get one working though, I'll be able to figure out the others. Perhaps you can try it on your end there to see if it works for you. Here is the code I'm using for the promocode.php as you suggested:

<?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.

dreamcatcher

11:26 pm on Mar 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just noticed something. In your form tag you have this:

method="get"

should be:

method="post"

dc

Bubzeebub

11:33 pm on Mar 3, 2005 (gmt 0)

10+ Year Member



Ok...I'm going to try that. By the way, I mentioned earlier that I'm using a form with a 'Submit' button (see code below)

<?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...

Bubzeebub

11:55 pm on Mar 3, 2005 (gmt 0)

10+ Year Member



DC,

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)

Bubzeebub

11:59 pm on Mar 3, 2005 (gmt 0)

10+ Year Member



I don't know, I'm just guessing there's something in the form tag that's screwing this up.

dreamcatcher

12:26 am on Mar 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, try this instead:


<?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

Bubzeebub

2:10 am on Mar 4, 2005 (gmt 0)

10+ Year Member



still didn't work...Do you think it could be server-related? Does it work on your end?

badone

5:13 am on Mar 4, 2005 (gmt 0)

10+ Year Member



Bubzeebub: Sent you a sticky but, I'll post it here as well for reasons of completeness :-)

switch(strtolower($_POST['type'])) {

Change 'type' to 'pcode'

Cheers,
BAD

dreamcatcher

8:52 am on Mar 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks badone, but thats not the problem apparently. I used that when explaining the drop down menu. I had already mentioned in a sticky that if you aren`t using a drop down you need to change 'type' to 'pcode'.

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]

Bubzeebub

3:58 pm on Mar 4, 2005 (gmt 0)

10+ Year Member



THAT DID IT .....That change you suggested Dreamcatcher did it!

WHOO HOO! ...Thanks a million guys... BADONE...DC...JATAR ...THANK YOU ALL!

dreamcatcher

4:12 pm on Mar 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



LOL! Glad we could help you out.

dc