Submitting form data to script in same php document

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
staylor505
Forum Newbie
Posts: 3
Joined: Mon Aug 31, 2009 4:14 pm

Submitting form data to script in same php document

Post by staylor505 »

I'm having difficulty passing form data to a script in the same document. When I run the document through a browser from my localhost server, it doesn't iterate through the 'if' statement and just drops through to the 'else' section. Can someone help me find the needle in this small hay stack. Thanks for any help.
I have the following small php document called Education.php:

<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Educational Achievements</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>Educational Achievements</h1><hr />
<?php
function checkEducation($Level) {
global $education;
if (isset($education)) {
$FindLevel = implode(",", $education);
if (strpos($FindLevel, $Level) !== FALSE)
return " selected = 'selected'";
}
return "";
}
if (isset($education)) {
echo "<p>You selected the following:</p>";
foreach ($education as $degree) {
echo "$degree<br />";
}
}
else
echo "<p>Select all your education achievements. (Hold your Ctrl key to select multiple items.)</p>";
?>
<form action="Education.php" method="get" enctype="application/x-www-form-urlencoded">
<p><select name="education[]" multiple="multiple" size="8">
<option value="High School Diploma" <?= checkEducation("High School Diploma"); ?> >High School Diploma</option>
<option value="Associate's Degree" <?= checkEducation("Associate's Degree"); ?> >Associate's Degree</option>
<option value="Bachelor's Degree" <?= checkEducation("Bachelor's Degree"); ?> >Bachelor's Degree</option>
<option value="Master's Degree" <?= checkEducation("Master's Degree"); ?> >Master's Degree</option>
<option value="Doctorate Degree" <?= checkEducation("Doctorate Degree"); ?> >Doctorate Degree</option>
<option value="Undergraduate Certificate" <?= checkEducation("Undergraduate Certificate"); ?> >Undergraduate Certificate</option>
<option value="Postbaccalaureate Certificate" <?= checkEducation("Postbaccalaureate Certificate"); ?> >Postbaccalaureate Certificate</option>
</select></p>
<p><input type="submit" value="Submit Education" /></p>
</form><hr />
</body>
</html>
ocpaul20
Forum Newbie
Posts: 12
Joined: Thu Jul 05, 2007 3:53 am

Re: Submitting form data to script in same php document

Post by ocpaul20 »

First of all, it helps to structure your code properly so that you can see you have all the matching wiggly brackets. Maybe this is just a function of the forum, I dont know.

What the program is doing is saying that
if (isset($education)) = FALSE because it is going to the else section.

Put some var_dump($education); statements in there and see what the variable is set to. This will help debug the code. Also, explicitly set it to something in the function ($education = "abc";) to see if it is following the path you expect it to. This and var_dump statements should help sort it out. Hope that helps a little.
Paul_F
Forum Newbie
Posts: 10
Joined: Sat Aug 29, 2009 6:17 pm

Re: Submitting form data to script in same php document

Post by Paul_F »

Code: Select all

 
if (isset($education)) 
    {
        echo "<p>You selected the following:</p>";
        foreach ($education as $degree) 
        {
            echo "$degree<br />";
        }
    }
else
    {
        echo "<p>Select all your education achievements. (Hold your Ctrl key to select multiple items.)</p>";
    }
 
staylor505
Forum Newbie
Posts: 3
Joined: Mon Aug 31, 2009 4:14 pm

Re: Submitting form data to script in same php document

Post by staylor505 »

ok sorry about the structure (being a newbie to php). I tried to correct it like you said so I hope that works. I tried your suggestions to no avail. I did have an idea to declare the variable outside the function before making it global inside. That seems to have worked but now I have backslashes before the apostrophes when output. I am pasting my new improved code below.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Educational Achievements</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>Educational Achievements</h1><hr />
<?php
$education = $_GET[education];
function checkEducation($Level)
{
global $education;
if (isset($education))
{
$FindLevel = implode(",", $education);
if (strpos($FindLevel, $Level) !== FALSE)
return " selected='selected'";
}
return "";
}
if (isset($education))
{
echo "<p>You selected the following:</p>";
foreach ($education as $degree)
{
echo "$degree<br />";
}
}
else
{
echo "<p>Select all your education achievements. (Hold your Ctrl key to select multiple items.)</p>";
}
?>
<form action="Education.php" method="get" enctype="application/x-www-form-urlencoded">
<p><select name="education[]" multiple="multiple" size="8">
<option value="High School Diploma" <?= checkEducation("High School Diploma"); ?> >High School Diploma</option>
<option value="Associate's Degree" <?= checkEducation("Associate's Degree"); ?> >Associate's Degree</option>
<option value="Bachelor's Degree" <?= checkEducation("Bachelor's Degree"); ?> >Bachelor's Degree</option>
<option value="Master's Degree" <?= checkEducation("Master's Degree"); ?> >Master's Degree</option>
<option value="Doctorate Degree" <?= checkEducation("Doctorate Degree"); ?> >Doctorate Degree</option>
<option value="Undergraduate Certificate" <?= checkEducation("Undergraduate Certificate"); ?> >Undergraduate Certificate</option>
<option value="Postbaccalaureate Certificate" <?= checkEducation("Postbaccalaureate Certificate"); ?> >Postbaccalaureate Certificate</option>
</select></p>
<p><input type="submit" value="Submit Education" /></p>
</form><hr />
</body>
</html>
staylor505
Forum Newbie
Posts: 3
Joined: Mon Aug 31, 2009 4:14 pm

Re: Submitting form data to script in same php document

Post by staylor505 »

OK so I did some research and found that magic_quotes_gpc in my php.ini file was turned on by default and it is recommended to turn it off (disable it) and use addslashes and stripslashes to escape the strings with single or double quotes. After doing this it seems to have worked out. Thanks for your help.
Post Reply