Could anyone tell what's wrong with this script, please?
Discuss Could anyone tell what's wrong with this script, please? in the PHP Development forum on Dev Shed. Could anyone tell what's wrong with this script, please? PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
Posts: 55
Time spent in forums: 10 h 32 m 31 sec
Reputation Power: 1
Could anyone tell what's wrong with this script, please?
I'm trying to create a form system where 3 pages will ask you your name, age, address, and all the missing input will be listed on the confirmation page after the 3rd page.
Please someone help me... been trying for a couple of hours and I'm new to PHP.
----- This is the script for the confirmation page----------------
<?php
// unset the formStarted session variable
unset($_SESSION['formStarted']);
// go through the elements in the $_SESSION
foreach ($_SESSION as $key => $value) {
// skip the missing element
if ($key == "missing"){continue;}
// skip the submit buttons
// use identity operator with strpos to prevent false negatives
if (strpos($key, 'Submit') === 0) {
continue;
}
echo "<li>$key: $value</li>";
}
// clear the $_SESSION array and destroy session
$_SESSION = array();
session_destroy();
?>
</ul>
<?php
if(isset($_SESSION['missing'])){
$missingList = implode(", ", "$_SESSION['missing']");
echo "<p>The following required field(s) are missing: $missingList. <br />Please fill them out below.</p>";
}
?>
---------And the script below is from previous page. (there are 2 other pages to ask name and age before this page. )-----------
session_start();
if (!isset($_SESSION['formStarted'])) {
header('Location: multiple01.php');
exit;
}
// each page needs a different name for the submit button
if (array_key_exists('Submit3', $_POST)) {
include('shared.php');
nukeMagicQuotes();
// set required fields
// must be an array, even if only one item is required
// if no fields are required, an empty array is needed
// otherwise, in_array() later in the script will generate an error
$required = array('address');
// create empty array for any missing fields
//$missing = array();
// process the $_POST variables and save them in the $_SESSION array
foreach ($_POST as $key => $value) {
// assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
// if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
array_push($_SESSION['missing'], $key);
}
// otherwise, assign to a variable of the same name as $key
else {
$_SESSION[$key] = $temp;
}
}
// if no required fields are missing, redirect to next page
//if (!$missing) {
header('Location: multiple04.php');
exit;
//}
}
?>
<!DOCTYPE html>
Posts: 9,801
Time spent in forums: 2 Months 3 Weeks 16 h 59 m 20 sec
Reputation Power: 6112
Please read the new user guide directly below this message.
If you're developing without error_reporting turned on, turn it on. Either make the change to error_reporting in php.ini or put this at the top of every page:
error_reporting(E_ALL);
That will tell you what's wrong.
Your function nukeMagicQuotes() isn't defined. Even if it were, it's entirely unnecessary if you're developing on a modern system.
The rest of your code is badly formatted and uncolored, so it's difficult to tell what's going on. Please wrap your code in [ PHP ] and [ /PHP ] tags (remove the spaces) which will allow us to better read your code.
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Posts: 55
Time spent in forums: 10 h 32 m 31 sec
Reputation Power: 1
Thanks
Did you mean like this??
PHP Code:
<?php
// unset the formStarted session variable
unset($_SESSION['formStarted']);
// go through the elements in the $_SESSION
foreach ($_SESSION as $key => $value) {
// skip the missing element
if ($key == "missing"){continue;}
// skip the submit buttons
// use identity operator with strpos to prevent false negatives
if (strpos($key, 'Submit') === 0) {
continue;
}
echo "<li>$key: $value</li>";
}
// clear the $_SESSION array and destroy session
$_SESSION = array();
session_destroy();
?>
</ul>
PHP Code:
<?php
if(isset($_SESSION['missing'])){
$missingList = implode(", ", "$_SESSION['missing']");
echo "<p>The following required field(s) are missing: $missingList. <br />Please fill them out below.</p>";
}
?>
---------And the script below is from previous page. (there are 2 other pages to ask name and age before this page. )-----------
PHP Code:
<?php
session_start();
if (!isset($_SESSION['formStarted'])) {
header('Location: multiple01.php');
exit;
}
// each page needs a different name for the submit button
if (array_key_exists('Submit3', $_POST)) {
include('shared.php');
nukeMagicQuotes();
// set required fields
// must be an array, even if only one item is required
// if no fields are required, an empty array is needed
// otherwise, in_array() later in the script will generate an error
$required = array('address');
// create empty array for any missing fields
//$missing = array();
// process the $_POST variables and save them in the $_SESSION array
foreach ($_POST as $key => $value) {
// assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
// if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
array_push($_SESSION['missing'], $key);
}
// otherwise, assign to a variable of the same name as $key
else {
$_SESSION[$key] = $temp;
}
}
// if no required fields are missing, redirect to next page
//if (!$missing) {
header('Location: multiple04.php');
exit;
//}
}
?>
Posts: 55
Time spent in forums: 10 h 32 m 31 sec
Reputation Power: 1
Sorry for the late reply
Thanks ManiacDan.
yeah I just realized that.. My problem was the page named 'multiple04.php' doesn't show the script at all.
So I wanted to ask someone about why the page isn't showing anything.
Since I assumed there are some problems on the fourth and the third page, I just posted 2 scripts on the fourth page and 1 big chunk of script on the third page
And am I supposed to use 'error_reporting(E_ALL);' right after the '<?php'?
Actually, my Dreamweaver is telling me that there is error on the 2nd line of the 2nd chunk of the script, where it says '$missingList = implode(", ", "$_SESSION['missing']"); '.
Can you tell what's wrong with this line or things above??
Posts: 1,884
Time spent in forums: 2 Weeks 4 Days 14 h 1 m 42 sec
Reputation Power: 1798
If that's the line that the error is occuring on, it's quit easy. You don't want to have quotes around $_SESSION[]. If you have it wrapped in quotes it is seen as a string, and you can't pass a string to explode(). If you're going to be sure, you should check that the value is an array with is_array($_SESSION['missing']) before you try ot use explode() on it.