Page 1 of 1

PHP Form Issue - Newbie

Posted: Sat Jun 14, 2008 9:45 pm
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'>

Re: PHP Form Issue - Newbie

Posted: Sat Jun 14, 2008 10:54 pm
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?

Re: PHP Form Issue - Newbie

Posted: Sun Jun 15, 2008 12:41 am
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.

Re: PHP Form Issue - Newbie

Posted: Sun Jun 15, 2008 5:19 am
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.

Re: PHP Form Issue - Newbie

Posted: Sun Jun 15, 2008 8:27 pm
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).

Re: PHP Form Issue - Newbie

Posted: Mon Jun 16, 2008 1:37 am
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.