Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-08-2008, 04:32 PM   PM User | #1
mrcurtains
New Coder

 
Join Date: Dec 2008
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
mrcurtains is an unknown quantity at this point
Form Post not working

Hello,
I am new to php and cannot see why this page is not working. I have spent days on trying to find the fault but can't. The problem is that sometimes the form works correctly and sometimes it doesn't. On the receiving page I use _POST to display the contents from the registration form page. Sometimes all of the reults are returned:

array (
'reg1_title' => 'Mr',
'reg1_surname' => 'Bill',
'reg1_forename' => 'Smith',
'reg1_email' => 'Bill@Smith.com',
)

And other times I only get:
array (
)

I have added Javascript to the code (which I have not added to the forum) so the form cannot be submitted without all the fields being completed. I would be very grateful if someone can see what could be wrong. Thank you for your time.

PHP Code:
<?php session_start(); ?>
<!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" lang="en">
<head>
    <title>Registration Form</title>
    <link rel="STYLESHEET" type="text/css" href="style.css">
</head>

<body>

<?php include_once('include/inc.htmlHeader.php5')?>
    <form action="register2.php5" method="POST" name="registrations" onsubmit="return fieldCheck()" style="margin: 0;">
        <table cellspacing="0" border="0" id="mainTable">
            <tr>    
                <td class="sectionSingle">
                    <ul id="info"><li>Fields marked with * must be filled.</li></ul>
                    <span class="title">Flight Crew - Personal Details</span>
                    
                    <table cellspacing="0" cellpadding="2">
                        <tr>
                            <td width="23">&nbsp;</td>
                            <td width="130"><font size="2">Title</font><b class="star"><font size="2">*</font></b></td>
                            <td colspan="3" width="399"><input maxlength="15" type="text" tabindex="5" name="reg1_title" size="20"></td>
                            </td>
                        </tr>                            
                        <tr>
                            <td width="23">&nbsp;</td>
                            <td width="130"><font size="2">Surname</font><b class="star"><font size="2">*</font></b></td>
                            <td colspan="3" width="399"><input maxlength="15" type="text" tabindex="5" name="reg1_surname" size="20"></td>
                        </tr>
                        <tr>
                            <td width="23">&nbsp;</td>
                            <td width="130"><font size="2">First Name</font><b class="star"><font size="2">*</font></b></td>
                            <td colspan="3" width="399"><input maxlength="15" type="text" tabindex="6" name="reg1_forename" size="20"></td>
                        </tr>                        
                        <tr>
                            <td width="23">&nbsp;</td>
                            <td width="130"><font size="2">Email Address</font><b class="star"><font size="2">*</font></b></td>
                            <td colspan="3" width="399"><input maxlength="35" tabindex="10" type="text" name="reg1_email" size="27"></td>
                        </tr>                
                    </table>
                </td>
            </tr>
        </table>
        <br>
        <br>
        <center><input type="submit" name="btnSubmit" value="Next" tabindex="200"></center>
        </form>
</body>
</html>
The following page is the form processing page:

PHP Code:
<?php session_start(); ?>
<!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" lang="en">
<head>
    <title>Registration Results</title>
    <link rel="STYLESHEET" type="text/css" href="style.css">
    <?php
        $message 
var_export($_POSTtrue);
    
$subject "Registration";
    
$to "me@mywebsite.com";
    
mail($to$subject$message);
    
?> 
</head>

<body>

    Thank you

</body>
</html>

Last edited by mrcurtains; 12-10-2008 at 02:14 PM.. Reason: Added name to submit button
mrcurtains is offline   Reply With Quote
Old 12-08-2008, 04:51 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
To find the problem you may want to remove the Javascript then try to reproduce the problem. If it doesn't happen with the Javascript removed, you know it's got to be something about the Javascript.
__________________
Fumigator is offline   Reply With Quote
Old 12-08-2008, 05:25 PM   PM User | #3
mrcurtains
New Coder

 
Join Date: Dec 2008
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
mrcurtains is an unknown quantity at this point
Thank you. I have removed the javascript and I can't see a difference. I have tested the page hundreds of times and it works everytime for me if the Javascript has been removed or not. However, if fails to receive information 1 in 20-30 customers.

Here is the Javascript and I can't see reason why it would stop posted values from being sent to the receiving page.

PHP Code:
<!--
function 
fieldCheck(){

    if(
document.registrations.reg1_title.value=="")
    {
        
alert("Title field should not be empty.");
        
document.registrations.reg1_title.focus();
        return 
false;
    }    
    if(
document.registrations.reg1_surname.value=="")
    {
        
alert("Surname field should not be empty.");
        
document.registrations.reg1_surname.focus();
        return 
false;
    }

    if(
document.registrations.reg1_forename.value=="")
    {
        
alert("First Name field should not be empty.");
        
document.registrations.reg1_forename.focus();
        return 
false;
    }        
    
    if(
document.registrations.reg1_email.value=="")
    {
        
alert("Email Address field should not be empty.");
        
document.registrations.reg1_email.focus();
        return 
false;
    }
    
        
    var 
agree=confirm("Are you sure you want to continue?");
    if (
agree)
        return 
true ;
    else
        return 
false ;    
}
//-->
</Script> 
mrcurtains is offline   Reply With Quote
Old 12-08-2008, 06:07 PM   PM User | #4
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,712
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
Your form processing code is not checking in any way that the form was submitted, so if the symptom is an email with array ( ) in it, then it is because someone (or a search engine) requested the page directly without submitting the form.

You need to give your submit button a name="..." parameter and then check in the form processing code that the $_POST variable by that name is set.
__________________
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.
CFMaBiSmAd is offline   Reply With Quote
Old 12-09-2008, 10:37 AM   PM User | #5
mrcurtains
New Coder

 
Join Date: Dec 2008
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
mrcurtains is an unknown quantity at this point
Excellent, well noticed. I didn't even think about that. I have added the check and will hopefully see if this has solved the problem. Thank you for your help .
mrcurtains is offline   Reply With Quote
Old 12-10-2008, 08:56 AM   PM User | #6
mrcurtains
New Coder

 
Join Date: Dec 2008
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
mrcurtains is an unknown quantity at this point
It didn't work. I used the following code around the form processing:

if(isset($_POST['btnSubmit'])) {

}

But empty fields are still returned. Is the above code correct? But it works some times and not others it is difficult to find out why is it not working. Thanks.
mrcurtains is offline   Reply With Quote
Old 12-10-2008, 09:32 AM   PM User | #7
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Where is your submit button having an attribute name="btnSubmit"?
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 12-10-2008, 10:58 AM   PM User | #8
mrcurtains
New Coder

 
Join Date: Dec 2008
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
mrcurtains is an unknown quantity at this point
I have edited the original post and have added the name to the button, thanks.
mrcurtains is offline   Reply With Quote
Old 12-10-2008, 01:17 PM   PM User | #9
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,712
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
It would take seeing your form processing code with the code to check $_POST['btnSubmit']

I copy and pasted your form at http://validator.w3.org and there are some closing td/tr/table tags that it found a problem with. This could cause different browsers to treat the form differently and perhaps not submit the data. Having error free markup would eliminate this as a possible reason.

Do you believe real visitors are entering data and it does not get submitted or could you have a spam bot script submitting data to or probing your form processing code? Why are you adding the javascript validation? If it is to prevent spam, then it won't because spam bot scripts submit data directly to your form processing page and could care less about anything you do to your form. Even if you have javascript validation in the form as an aid to your real visitors entering data, you must validate the data after it has been received on the server in your form processing code. This would eliminate empty emails, prevent spam, and help the visitors in the cases where they have javascript disabled.
__________________
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.
CFMaBiSmAd is offline   Reply With Quote
Old 12-10-2008, 01:43 PM   PM User | #10
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,712
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
The code you posted in the first post, is that actually two separate files and just got posted in one set of code tags or is all of that in one file?
__________________
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.
CFMaBiSmAd is offline   Reply With Quote
Old 12-10-2008, 02:39 PM   PM User | #11
mrcurtains
New Coder

 
Join Date: Dec 2008
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
mrcurtains is an unknown quantity at this point
Hi,

I had posted the two pages together in the script tag. I have now separated them. I believe the user are submitting the forms, though I can't be completely sure. The Javascript is only to stop users submitting the form before all fields are complete. Though there seems to be a lot of registrations from people who have Javascript turned off.

The </td> error was my lack of skill with cutting and pasting. My original form contains about 100 fields so I have drastically shortened the fields to make it easier to read. I have checked the fields that I have removed and there is nothing incorrect about them.
mrcurtains is offline   Reply With Quote
Old 12-10-2008, 02:46 PM   PM User | #12
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,712
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
How about this part -
Quote:
Originally Posted by CFMaBiSmAd View Post
It would take seeing your form processing code with the code to check $_POST['btnSubmit']
__________________
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.
CFMaBiSmAd is offline   Reply With Quote
Old 12-10-2008, 03:11 PM   PM User | #13
mrcurtains
New Coder

 
Join Date: Dec 2008
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
mrcurtains is an unknown quantity at this point
The form processing code just takes the fields passed to the page, concatinates a string then saves them to a session to use later. As the information doesn't get this far I have not included this so that the code is easier to read.

PHP Code:
<?php
// If the previous page has not been submitted do not check the form posted
if(isset($_POST['btnSubmit'])) {
    
$sqlColumn "reg1_title, reg1_surname, reg1_forename, reg1_email";
    
    
$sqlValue "'".$_POST['reg1_title']."', '".$_POST['reg1_surname']."', '".$_POST['reg1_forename']."', '".$_POST['reg1_email'];
    
    
    
    
$_SESSION["sqlColumn_1"] = $sqlColumn;
    
$_SESSION["sqlValue_1"] = $sqlValue;
    
    
// Add the SQL to an email for testing
    
$message $sqlColumn."    ".$sqlValue;
    
$subject "Reg 1";
    
$to "me@mywebsite.com";
    
mail($to$subject$message);
    
}
?>
At the moment I have inserted the following processing code instead until I can find out why I occassionally get blank fields:

PHP Code:
   <?php 
        $message 
var_export($_POSTtrue); 
    
$subject "Registration"
    
$to "me@mywebsite.com"
    
mail($to$subject$message); 
    
?>
The majority of the time I get an email with all the fields filled in. However, a few times a day I get blank emails sent to me.
mrcurtains is offline   Reply With Quote
Old 12-10-2008, 03:39 PM   PM User | #14
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,712
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
I recommend using error_log() to create a log file of all the available information each time your form processing code is requested. You should do this for any real application so that you have a record of activity in your application. Use any or all of the following -

PHP Code:
<?php
// basic form and form processing code with logging of both good and bad submission data -

// condition the inputs and setup default values -
$submitted = isset($_POST['submit']) ? $_POST['submit'] : FALSE// was the form submitted?
$name_field = isset($_POST['name']) ? $_POST['name'] : ""// condition the form's name field

if($submitted) {
    
$form_error = array(); // array to hold any form validation errors

    // validate the form data here (set elements in $form_error to hold error messages)
        
if(empty($name_field)) {
        
$form_error['name_field'] = "Please fill in the name";
    }

    
// if there were no form validation errors, use the data that was submitted
    
if(empty($form_error)) {
        
// do something with the data here
        
echo "The name you entered was: $name_field";
        
        
// setup to log the result of this form submission
        
$result "Result:Ok";
    } else {
        
$result "Result:" implode("|"$form_error);
    }
        
// log the who, what, when, and result of this form submission
        
$who $_SERVER['REMOTE_ADDR']; // if your system has a login, who would also include user id information
        
$who .= "|User:" .$_SESSION['user_id'];            
        
$who .= "|Agent:" .$_SERVER['HTTP_USER_AGENT'];        
        
$who .= "|Ref:" .$_SERVER['HTTP_REFERER'];
        
$who .= "|Post:";
        foreach(
$_POST as $key => $value) {
            
$who .= "[$key]=>$value|";
        }
        
$who .= "Get:";
        foreach(
$_GET as $key => $value) {
            
$who .= "[$key]=>$value|";
        }
        
$who .= "Cookie:";
        foreach(
$_COOKIE as $key => $value) {
            
$who .= "[$key]=>$value|";
        }
        
$who rtrim($who'|');
        
$what "What:Form submission"// if this was a query, the actual query would be logged.
        
$where "Where:".__file__." (".__line__.")";
        
$when date('Y-m-d h:i:s');
        
error_log("$when, $who, $what, $where, $result\r\n"3"form_submission.log");    
}

// display the form if it has not been submitted or there are form validation errors
If(!$submitted || !empty($form_error)) {
    
// check for and display any form validation errors
    
if(!empty($form_error)) {
        echo 
"Please correct these errors -<br />";
        foreach(
$form_error as $error) {
            echo 
"$error<br />";
        }
    }

    
// display the form, with any previously submitted values
?>
    <form method="post" action="">
    Name: <input type="text" name="name" value="<?php echo $name_field?>">
    <input type="submit" name="submit">
    </form>
<?php
}
?>
__________________
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.
CFMaBiSmAd is offline   Reply With Quote
Users who have thanked CFMaBiSmAd for this post:
mrcurtains (12-10-2008)
Old 12-10-2008, 04:22 PM   PM User | #15
mrcurtains
New Coder

 
Join Date: Dec 2008
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
mrcurtains is an unknown quantity at this point
Excellent, that is some useful code. I'll be sure to use some of that, thank you.
mrcurtains is offline   Reply With Quote
Reply

Bookmarks

Tags
$_post, post

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:59 AM.


Advertisement
Log in to turn off these ads.