...

View Full Version : Form Post not working



mrcurtains
12-08-2008, 04:32 PM
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 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 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($_POST, true);
$subject = "Registration";
$to = "me@mywebsite.com";
mail($to, $subject, $message);
?>
</head>

<body>

Thank you

</body>
</html>

Fumigator
12-08-2008, 04:51 PM
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.

mrcurtains
12-08-2008, 05:25 PM
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.


<!--
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>

CFMaBiSmAd
12-08-2008, 06:07 PM
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.

mrcurtains
12-09-2008, 10:37 AM
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 :thumbsup:.

mrcurtains
12-10-2008, 08:56 AM
:confused: 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.

abduraooft
12-10-2008, 09:32 AM
Where is your submit button having an attribute name="btnSubmit"?

mrcurtains
12-10-2008, 10:58 AM
I have edited the original post and have added the name to the button, thanks.

CFMaBiSmAd
12-10-2008, 01:17 PM
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.

CFMaBiSmAd
12-10-2008, 01:43 PM
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?

mrcurtains
12-10-2008, 02:39 PM
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.

CFMaBiSmAd
12-10-2008, 02:46 PM
How about this part -

It would take seeing your form processing code with the code to check $_POST['btnSubmit']

mrcurtains
12-10-2008, 03:11 PM
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
// 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
$message = var_export($_POST, true);
$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.

CFMaBiSmAd
12-10-2008, 03:39 PM
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
// 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
}
?>

mrcurtains
12-10-2008, 04:22 PM
Excellent, that is some useful code. I'll be sure to use some of that, thank you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum