Page 1 of 1

Can not capture Form data using PHP code

Posted: Thu Feb 21, 2008 9:33 pm
by rpacdn
I have the following code, but in my text.txt output file, the values of $name and $email are not getting stored. Also in the Thank you message, the $name value is not displayed. Any help would be greatly appreciated.

Save this as mytest.php

Code: Select all

<html>
<head>
<?php
if ($_POST['submit']=='OK')  {
$name = $_POST['name'] ;
$email = $_POST['email'] ;
echo $name;
$f=fopen("text.txt","a");
fwrite($f,"****************************************\r\n\r\n");
fwrite($f,"Name: $name| $email\r\n");
fclose($f);
}
?>
<title>Thank You</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<body bgcolor="#FFFFFF" text="#000000">
 
<?php
 
echo "Thank you " . $name . " we have added you to the list";
 
?>
</body>
</html>
 
Save this as mytestFrm.html

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
<form action="./mytest.php" method="POST" name="form1" id="form1">
Name <br><input type="text" name="name"><br>
Email Address <br><input type="text" name="email">
<input type="submit" value="submit">
</form>
</body>
</html>

Re: Can not capture Form data using PHP code

Posted: Thu Feb 21, 2008 11:57 pm
by blacksnday
TRY:
if ($_POST['submit']=='TRUE') {

OR
if ($_POST['name']=='TRUE') {

OR
if ($_POST['form1]=='TRUE') {

Re: Can not capture Form data using PHP code

Posted: Fri Feb 22, 2008 7:21 am
by rpacdn
Thank you. I tried that, but didn't make any difference. In fact those three lines were added on later. The $name & $email values were not getting displayed or saved even without those lines. The script actually do write out the ****** line and the word "Name: " in the text.txt output as well, but not the values of the passing parameters. I tried GET method and I can see the values being passed to the mytest.php file as well. Very strange problem.

I am running this on my localhost running the latest install of WAMP. Any other suggestions? Is it working for you?

Re: Can not capture Form data using PHP code

Posted: Fri Feb 22, 2008 9:59 am
by liljester
your submit button doesnt have a name, and therefore isnt included in the $_POST array. change:

Code: Select all

<input type="submit" value="submit">
to:

Code: Select all

<input type="submit" name="submit" value="OK">

Re: Can not capture Form data using PHP code

Posted: Fri Feb 22, 2008 10:20 am
by anjanesh
Rather than checking if the submit button has been submitted, check if all fields of the form have been submitted (expect checkboxes, if any)

Code: Select all

<html>
<head>
<?php
if (count(array_diff(array('name', 'email'), array_keys($_POST))) == 0) # POSTed
 {
        $name = $_POST['name'] ;
        $email = $_POST['email'] ;
 }
.
.

Re: Can not capture Form data using PHP code

Posted: Fri Feb 22, 2008 11:51 am
by rpacdn
liljester wrote:your submit button doesnt have a name, and therefore isnt included in the $_POST array. change:

Code: Select all

<input type="submit" value="submit">
to:

Code: Select all

<input type="submit" name="submit" value="OK">
Thank you! That worked.

Re: Can not capture Form data using PHP code

Posted: Fri Feb 22, 2008 11:51 am
by rpacdn
anjanesh wrote:Rather than checking if the submit button has been submitted, check if all fields of the form have been submitted (expect checkboxes, if any)

Code: Select all

<html>
<head>
<?php
if (count(array_diff(array('name', 'email'), array_keys($_POST))) == 0) # POSTed
 {
        $name = $_POST['name'] ;
        $email = $_POST['email'] ;
 }
.
.
Thank you for the suggestion. Will use that as well.

Re: Can not capture Form data using PHP code

Posted: Fri Feb 22, 2008 11:55 am
by RobertGonzalez
I had brought this up in another thread recently, but you should never check that a button value has been passed. Instead you should check if the post array is empty or better, if the request method is post:

Code: Select all

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  // Form was submitted
}
?>
In some browsers, when you hit the enter key inside of the form without actually hitting the submit button, the form will submit but the form button, because it wasn't activated, will not be sent with the post data. So checking for it will return false.

Re: Can not capture Form data using PHP code

Posted: Fri Feb 22, 2008 12:42 pm
by rpacdn
Everah wrote:I had brought this up in another thread recently, but you should never check that a button value has been passed. Instead you should check if the post array is empty or better, if the request method is post:

Code: Select all

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  // Form was submitted
}
?>
In some browsers, when you hit the enter key inside of the form without actually hitting the submit button, the form will submit but the form button, because it wasn't activated, will not be sent with the post data. So checking for it will return false.
Thank you for the input. Interestingly enough I just added a Radio button to my page.

Code: Select all

Register <br><input type="radio" name="register" value="Yes">Yes
<input type="radio" name="Register" value="No">No
And then I added this line to the fwrite command:

Code: Select all

fwrite($f,"$name|$email|$register\r\n");
But the $register value (Yes or No based on the selection) does not get written to the file anymore? Ought to be something simple that I am missing!

Re: Can not capture Form data using PHP code

Posted: Fri Feb 22, 2008 12:46 pm
by RobertGonzalez
Did you assign $register a value? Are you developing with error_reporting set to E_ALL and display_errors on?

Re: Can not capture Form data using PHP code

Posted: Fri Feb 22, 2008 12:52 pm
by rpacdn
Yes i selected one of the two radio buttons in each case which should send either "Yes" or "No" as the value in $register.

No I do not have E_All enabled.

Re: Can not capture Form data using PHP code

Posted: Fri Feb 22, 2008 12:56 pm
by rpacdn
Never mind.... it is working now. I had the wrong spelling for the radio button name - Register and register!!!

Thank you to all for the help.