[SOLVED] Conditional redirect based on vars from fo

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

[SOLVED] Conditional redirect based on vars from fo

Post by Sinemacula »

I'm trying to create a two part form, where all answers for the first part must be "Yes" in order to go on to the second part. If any answers are "No" or left unanswered, then I want the user to be redirected to a different page.

I've put together a script to process the form data, but it doesn't seem to be working. Unfortunately, I'm not exprienced enough wth PHP to figure this out... it's probably obvious...

Anyway, here's the code I'm using:

Code: Select all

<?php require "db_connect.php";  //this gets the username, host, dbname, password variables to connect to the database

//we check to make sure this page was accessed by someone hitting the submit button
if(isset($_POST['Submit'])) {

//then we make sure that all questions were answered Yes - if not then redirect to not qualified page
if $_POST['age'] != "Yes" {
header('Location: not-qualified.html');
}
if $_POST['before13'] != "Yes" {
header('Location: not-qualified.html');
}
if $_POST['waking'] != "Yes" {
header('Location: not-qualified.html');
}
if $_POST['substancefree'] != "Yes" {
header('Location: not-qualified.html');
}
if $_POST['humanlike'] != "Yes" {
header('Location: not-qualified.html');
}

//made it through all validation without being redirected, so need to reset variables to take account of globals being off
$age = $_POST['age'];
$before13 = $_POST['before13'];
$waking = $_POST['waking'];
$substancefree = $_POST['substancefree'];
$humanlike = $_POST['humanlike'];
$date = date('Y-m-d H:i:s');

//This is where we connect to the database

$db = mysql_pconnect($db_host, $db_user, $db_pass);
	if (!$db) { 
	
//this gives a warning if we can't connect to the database, and then exits
	
echo( "<p>Unable to connect to the " . 
"database server at this time.</p>" ); 
exit(); 
}
//this is the statement that connects to the database table and inserts the new values
//first we do the selecting of the database
//then we define the command with $sql -- telling it to INSERT data into the table fields listed
//and then telling it what data to put in those fields by calling on the POSTed variables from the form

mysql_select_db($db_name, $db);
	$sql_qualify = ("INSERT INTO qualify (
	age,
	before13,
	waking,
	substancefree,
	humanlike,
	date)
	VALUES (
	'$age',
	'$before13',
	'$waking',
	'$substancefree',
	'$humanlike',
	'$date');");
	$result_qualify=mysql_query($sql_qualify,$db) or die("<P>Query failed: ".mysql_error()); //then we tell it to perform the instructions, or give an error message
	
//Database query is then be followed by another redirect to the second part of the prequalification questionnaire
header(location: initial-q2.html);

//if this page was reached without hitting the submit button on the form, give an error message
} else {
echo "You did not submit the associated form... you should not be here.";
}
?>
Can anyone help me get this working?

Thanks,
Scott

edit patrikG: replaced

Code: Select all

with

Code: Select all

-tags for readibility
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

other than this:

Code: Select all

<?php header(location: initial-q2.html); ?>
not being a string... the header calls should probably have an exit() after each. You may want to check the case difference between the "Yes" you check against versus the yes you have in the form.
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

I think that you should use something like this:

Code: Select all

<?php

if(!strcasecmp($_POST['age'], 'Yes')){
header('Location: not-qualified.html'); 
}

?>
Because "Yes" there is a string. I think that is better for you (if the user chooses the answers from a select) to change the option value from a string to a value (0==No and 1==Yes).
In this way your code will look:


Code: Select all

<?php
if ($_POST['age'] != 0) { 
header('Location: not-qualified.html'); 
}

?>
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

Post by Sinemacula »

Thanks! I was still having trouble after making the changes that would allow me to use "Yes"... so I changed the form to use 1 and 0 and changed my script accordingly. It's now working!
Post Reply