PHP Form Issue - Newbie

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
Khalan
Forum Newbie
Posts: 3
Joined: Sat Jun 14, 2008 9:41 pm

PHP Form Issue - Newbie

Post by Khalan »

Hey guys, newbie to this board, but I've been doing quite a lot of PHP over the recent months. For some reason this code is refusing to activate the post-submission section. I used a tutorial for the basis which the initial code worked fine, however my adaptation refuses to work.

Any assistance would be greatly appreciated.

Code: Select all

 
<?php
function showForm($errorEmail=false,$errorFirstName=false,$errorLastName=false,$errorMoBirth=false,$errorDoBirth=false,$errorYoBirth=false,$errorSecQuest=false,$errorSecAns=false){
    echo "<p align='center'>Account Creation</p>";
    echo "<form action='index.php' method='POST'>";
    echo "
    <table width='500px' border='0' cellspacing='5' cellpadding='0' align='center'>
        <tr>
            <td valign='top' align='right'>
                First Name:
            </td>
            <td width='10px'></td>
            <td valign='top' align='left'>
                <input type='text' name='firstName' length='15' size='15'>";
                if ($errorFirstName) echo "<br><font size='2' color='red'>Please enter a First Name.</font>";
                echo "
            </td>
        </tr>
        <tr>
            <td valign='top' align='right'>
                Last Name:
            </td>
            <td width='10px'></td>
            <td valign='top' align='left'>
                <input type='text' name='lastName' length='15' size='15'>";
                if ($errorLastName) echo "<br><font size='2' color='red'>Please enter a Last Name.</font>";
                echo "
            </td>
        </tr>
        <tr>
            <td valign='top' align='right'>
                Email Address:
            </td>
            <td width='10px'></td>
            <td valign='top' align='left'>
                <input type='text' name='emailAdd' length='32' size='32'>";
                if ($errorEmail) echo "<br><font size='2' color='red'>Please enter a valid Email.</font>";
                echo "
            </td>
        </tr>
        <tr>
            <td valign='top' align='right'>
                Date of Birth:<br>
                <font size='2'>(mm/dd/yyyy)</font>
            </td>
            <td width='10px'></td>
            <td valign='top' align='left'>
                <select name='MoBirth'>
                    <option value='01' selected='selected'>January</option>
                    <option value='02'>February</option>
                    <option value='03'>March</option>
                    <option value='04'>April</option>
                    <option value='05'>May</option>
                    <option value='06'>June</option>
                    <option value='07'>July</option>
                    <option value='08'>August</option>
                    <option value='09'>September</option>
                    <option value='10'>October</option>
                    <option value='11'>November</option>
                    <option value='12'>December</option>
                </select>&nbsp;
                <input type='text' length='2' size='2' name='DoBirth' value='01'>&nbsp;
                <input type='text length='4' size='4' name='YoBirth' value='2004'>";
                if ($errorMoBirth || $errorDoBirth || $errorYoBirth) echo "<br><font size='2' color='red'>Please enter a valid Date of Birth.</font>";
                echo "
            </td>
        </tr>
        <tr>
            <td valign='top' align='right'>
                Security Question:
            </td>
            <td width='10px'></td>
            <td valign='top' align='left'>
                <select name'secQuest'>
                    <option value='1'>
                        What is your primary frequent flyer number
                    </option>
                    <option value='2'>
                        What is your library card number
                    </option>
                    <option value='3'>
                        What was your first phone number
                    </option>
                    <option value='4'>
                        What was your first teacher's name
                    </option>
                    <option value='5'>
                        What is your mother's maiden name
                    </option>
                    <option value='6'>
                        What is your father's middle name
                    </option>
                </select>";
                if ($errorSecQuest) echo "<br><font size='2' color='red'>Please enter a valid question.</font>";
                echo "
            </td>
        </tr>
        <tr>
            <td valign='top' align='right'>
                Answer:<br>
                <font size='2'>(32 characters max.)</font>
            </td>
            <td width='10px'></td>
            <td valign='top' align='left'>
                <input type='text' name='secAns' length='32' size='32'>";
                if ($errorSecAns) echo "<br><font size='2' color='red'>Please enter an answer.</font>";
                echo "
            </td>
        </tr>
        <tr>
            <td colspan='3' valign='top' align='center'>
                <input type='submit' value='Create Account'>
            </td>
        </tr>
    </table>
        </form>";
}
 
if (!isset($_POST['SubmitForm'])) {
    showForm();
} else {
    //Init error variables
    $errorEmail = false;
    $errorFirstName = false;
    $errorLastName = false;
    $errorMoBirth = false;
    $errorDoBirth = false;
    $errorYoBirth = false;
    $errorSecQuest = false;
    $errorSecAns = false;
 
    $firstName  = isset($_POST['firstName'])  ? trim($_POST['firstName'])  : '';
    $lastName  = isset($_POST['lastName'])  ? trim($_POST['lastName'])  : '';
    $emailAdd  = isset($_POST['emailAdd'])  ? trim($_POST['emailAdd'])  : '';
    $MoBirth  = isset($_POST['MoBirth'])  ? trim($_POST['MoBirth'])  : '';
    $DoBirth  = isset($_POST['DoBirth'])  ? trim($_POST['DoBirth'])  : '';
    $YoBirth  = isset($_POST['YoBirth'])  ? trim($_POST['YoBirth'])  : '';
    $secQuest  = isset($_POST['secQuest'])  ? trim($_POST['secQuest'])  : '';
    $secAns  = isset($_POST['secAns'])  ? trim($_POST['secAns'])  : '';
 
    if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $emailAdd)) $errorEmail = true;
    if (strlen($firstName)<1 || strlen($firstName)>32) $errorFirstName = true;
    if (strlen($lastName)<1 || strlen($lastName)>32) $errorLastName = true;
    if (strlen($MoBirth)<2 || strlen($MoBirth)>2) $errorMoBirth = true;
    if (strlen($DoBirth)<2 || strlen($DoBirth)>2) $errorDoBirth = true;
    if (strlen($YoBirth)<2 || strlen($YoBirth)>2) $errorYoBirth = true;
    if (strlen($secQuest)<1 || strlen($secQuest)>6) $errorSecQuest = true;
    if (strlen($secAns)<1 || strlen($secAns)>32) $errorSecAns = true;
 
    if ($errorEmail || $errorFirstName || $errorLastName || $errorMoBirth || $errorDoBirth || $errorYoBirth || $errorSecQuest || $errorSecAns) {
        showForm($errorEmail,$errorFirstName,$errorLastName,$errorMoBirth,$errorDoBirth,$errorYoBirth,$errorSecQuest,$errorSecAns);
    } else {
        echo "Submission was success!";
    }
}
?> 
 
 

oh.. how embarrassing...

Code: Select all

<input type='submit' name='SubmitForm' value='Create Account'>
was..

Code: Select all

<input type='submit' value='Create Account'>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP Form Issue - Newbie

Post by califdon »

When you say it "refuses to work," exactly what do you mean? Does it throw an error? Does the form display? What doesn't work?
Khalan
Forum Newbie
Posts: 3
Joined: Sat Jun 14, 2008 9:41 pm

Re: PHP Form Issue - Newbie

Post by Khalan »

califdon wrote:When you say it "refuses to work," exactly what do you mean? Does it throw an error? Does the form display? What doesn't work?
No error message, was a stupid typo, I forgot to name the submit button so that when the if statement checked for the post, it was not catching it. Working perfectly after the fix.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: PHP Form Issue - Newbie

Post by superdezign »

Instead of using:

Code: Select all

if (isset($_POST['SubmitForm'])) {
You should use:

Code: Select all

if (!empty($_POST)) {
We've seen issues before where certain browsers do not submit the actual submit button if the user presses "Enter" instead of clicking the button manually.
Khalan
Forum Newbie
Posts: 3
Joined: Sat Jun 14, 2008 9:41 pm

Re: PHP Form Issue - Newbie

Post by Khalan »

superdezign wrote:Instead of using:

Code: Select all

if (isset($_POST['SubmitForm'])) {
You should use:

Code: Select all

if (!empty($_POST)) {
We've seen issues before where certain browsers do not submit the actual submit button if the user presses "Enter" instead of clicking the button manually.
Good idea, I've actually combated that by not allowing enter to submit the form. I have set it so that the actual submission is done by a linked image, using a hidden input named 'SubmitForm' with the value of 'Submit'. Of course it's just javascript to submit the form, which is normally ill-advised, incase users don't have JavaScript in, however it's a requirement for a lot of my site and my users are aware that it must be on (it's noted on the front page which does not use JS).
User avatar
VirtuosiMedia
Forum Contributor
Posts: 133
Joined: Thu Jun 12, 2008 6:16 pm

Re: PHP Form Issue - Newbie

Post by VirtuosiMedia »

I don't really see why you would need JavaScript for form submission unless you are doing client-side validation (which doesn't mean that you can get away with not validating server-side as well). I always just use a hidden field called 'submitted' with a value of TRUE as well as a submit button. Using the hidden field works in every browser I've tested in.
Post Reply