Jump to content

Using data entered in text field to redirect page


nerotic

Recommended Posts

Hello,

 

I'm really new to PHP and am trying to get a something rather simple set up but finding little success.

 

In a nutshell. I have a OSCommerce set up, actually three stores.  From the informational website I have a Store Region select page before redirecting to the store. The first two stores have USDollar and Euro pricing.

 

The 3rd store is brand new and has special pricing.

 

What I want to do on the Store Selector page is to add a a simple text form field. I will be issuing special discount codes (multiple and varied, but they will all redirect to the same place).

 

So I need a way to verify the code which means storing them on the server so I need to understand how to secure that file, or am I better off using a DB since I already have MySQL running with OSC.

 

In the event the code is wrong I'd like to implement a simple AJAX error message that displays right on the page. If it's correct then I simply want to redirect them to that store.

 

I've already set up a robots.txt on the server so that the page doesn't get indexed, at least by legit search engines. The idea being that there is only one entry point to these special prices.

 

Can anyone please give me some guidance as to how to best accomplish this?

 

Thanks in advance.

Link to comment
Share on other sites

Link to comment
Share on other sites

Hi Gareth,

 

Thanks for shining a light on the path :)  For now I'm not going to worry about AJAX, I want to get this working first and to understand exactly what goes on.

 

I'm having problems but I'm sure this has to do with my syntax, the damn error tells me so  8). Here is the code that is giving me problems, it resides in a file called storeentry.php:

 

<?php
if ($storentry="test1" or $storentry="test2" or $storentry="test3") {
	window.location 'shop.php?page=store3'; 
}
elseif ($storentry="test4" or $storentry="test5" or $storentry="test6") {
	window.location 'shop.php?page=store4'; 
}
else window.location 'shop.php?page=wrongcode';
)
?>

 

 

This is the error that I'm getting:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in D:\Nero\Works\A.U.X\Web2.0\storeentry.php on line 5

 

I have a page called shop_page_content.inc where I have snippets that look like this:

 

       

case 'store3':
	echo "http://www.auxout.com/oscomm3/";
	break; 
case 'store4':
	echo "http://www.auxout.com/oscomm4/";
	break; 
case 'wrongcode':
	echo "storeerror.php";
	break;
default:  
	echo "select.php";  
	break; 

 

 

The form on the php page looks like this:

<form action="storeentry.php" method="post">
If you have a code please enter it here: <input type="text" size="15" maxlength="10" class="input" name="storeentry" /> 
<input type="submit" value="SUBMIT" class="button" />
</form>

 

 

Being new to php, I'm not sure if this is possible but I've decided to bypass using the DB at all. We're only going to have 4-6 codes and it's rarely going to update. Should this work just using the POST ... wait a sec...I think I know what the problem is...need to use GET_POST maybe?  Anyway, I'm going to post this but keep on testing.

 

I've googled non-stop. I don't see much choice but to ask you whether you prefer milk, dark, or filled chocolates :)

Link to comment
Share on other sites

I've updated storeentry.php so it now looks like the following (it now has the switch statement:

 

 

<?php switch($_POST['storeentry']) {
if ($storentry="test1" or $storentry="test2" or $storentry="test3") {
	window.location = 'shop.php?page=store3'; 
}
elseif ($storentry="test4" or $storentry="test5" or $storentry="test6") {
	window.location = 'shop.php?page=store4'; 
}
else window.location = 'shop.php?page=wrongcode';
)
}
?>

 

And now I'm getting this error:

Parse error: syntax error, unexpected T_IF, expecting T_CASE or T_DEFAULT or '}' in D:\Web2.0\storeentry.php on line 3

 

Line 3 begins with "if"

Link to comment
Share on other sites

A case statement removes the need for an if-else ladder

 

<?php 
switch($storeentry) 
{
     case 'test1': 
     case 'test2':
     case 'test3':
          window.location = 'shop.php?page=store3'; 
          break;
     case 'test4':
     case 'test5':
     case 'test6':     
           window.location = 'shop.php?page=store4'; 
           break;
    default:
           window.location = 'shop.php?page=wrongcode';
}
?>

Link to comment
Share on other sites

PHP does not see

window.location = 'shop.php?page=store3'; 

As JavaScript. You'll need to echo the above line out in script tags for it to work. Eg

echo '<script type="text/javascript">window.location = \'shop.php?page=store3\';</script>';

 

However personally I'd use header to redirect users. You cannot always assume all users will have javascript enabled. If you're going the JavaScript route you may also want to have a 'Plan B' by displaying a short message saying where the user is being redirected to, just in case the redirect does not occur for some reason.

 

Link to comment
Share on other sites

First off, thanks to the both of you for taking the time to assist me. I'm all for doing things the PHP way...doing my best to learn it :)

I'd rush rather make the extra effort.

 

So this is what storeentry.php looks like now:

 

<?php 
switch($storeentry) 
{
     case 'test1': 
     case 'test2':
     case 'test3':
	header('Location: shop.php?page=store3');
		break;
     case 'test4':
     case 'test5':
     case 'test6':     
        header('Location: shop.php?page=store4');
           break;
default:
        header('Location: shop.php?page=wrongcode');
}
?>

 

And the result:

Warning: Cannot modify header information - headers already sent by (output started at D:\Web2.0\storeentry.php:1) in D:\Web2.0\storeentry.php on line 15

 

Just for my own understanding wouldn't an if, elseif loop preclude php from sending multiple headers as appears to be the case here?

 

How would I go about fixing this? I've been googling but the concepts are a bit beyond my grasp, more like the wording actually  :(

Link to comment
Share on other sites

Ok...I managed to get rid of the nasty nesting problem.

 

Now everything seems to be working. Oh, everything expect doing what it's supposed to do when a correct code is entered and that's redirect to the correct page.

 

PHP currently looks like this:

<?php
switch($storeentry) 
{
     case 'test1': 
     case 'test2':
     case 'test3':
	header('Location: oscomm3/');
		break;
     case 'test4':
     case 'test5':
     case 'test6':     
        header('Location: oscomm4/');
           break;
default:
        header('Location: storeerror.php');
}

 

And this is the form code:

<form action="storeentry.php" method="post">
If you have a code please enter it here: <input type="text" size="15" maxlength="10" class="input" name="storeentry" />
<input type="submit" value="SUBMIT" class="button" />
</form>

Link to comment
Share on other sites

You need to be using $_POST['storeentry'] not $storeentry

 

Thanks for responding Wildteen. My code look slike this now:

 

<?php
switch  $_POST['storeentry']
{
case 'test1':
    case 'test2':
    case 'test3':
	header('Location: oscomm3/');
break;

case 'test4':
    case 'test5':
    case 'test6':
	header('Location: oscomm4/');
    break;

default:
	header('Location: storeerror.php');
}
?>

 

And it's giving me this error:

Parse error: syntax error, unexpected T_VARIABLE, expecting '(' in D:\Nero\Works\A.U.X\Web2.0\storeentry.php on line 2

Link to comment
Share on other sites

 

<?php
switch ($_POST['storeentry'])
{
   case 'test1':
    case 'test2':
    case 'test3':
      header('Location: oscomm3/');
   break;

   case 'test4':
    case 'test5':
    case 'test6':
      header('Location: oscomm4/');
    break;

   default:
      header('Location: storeerror.php');
}
?>

switch is a statement, just like if - therefore needs brackets surrounding its argument.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.