Can not capture Form data using PHP code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rpacdn
Forum Newbie
Posts: 7
Joined: Thu Feb 21, 2008 9:27 pm

Can not capture Form data using PHP code

Post 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>
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Re: Can not capture Form data using PHP code

Post by blacksnday »

TRY:
if ($_POST['submit']=='TRUE') {

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

OR
if ($_POST['form1]=='TRUE') {
rpacdn
Forum Newbie
Posts: 7
Joined: Thu Feb 21, 2008 9:27 pm

Re: Can not capture Form data using PHP code

Post 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?
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: Can not capture Form data using PHP code

Post 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">
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Re: Can not capture Form data using PHP code

Post 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'] ;
 }
.
.
rpacdn
Forum Newbie
Posts: 7
Joined: Thu Feb 21, 2008 9:27 pm

Re: Can not capture Form data using PHP code

Post 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.
rpacdn
Forum Newbie
Posts: 7
Joined: Thu Feb 21, 2008 9:27 pm

Re: Can not capture Form data using PHP code

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Can not capture Form data using PHP code

Post 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.
rpacdn
Forum Newbie
Posts: 7
Joined: Thu Feb 21, 2008 9:27 pm

Re: Can not capture Form data using PHP code

Post 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!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Can not capture Form data using PHP code

Post by RobertGonzalez »

Did you assign $register a value? Are you developing with error_reporting set to E_ALL and display_errors on?
rpacdn
Forum Newbie
Posts: 7
Joined: Thu Feb 21, 2008 9:27 pm

Re: Can not capture Form data using PHP code

Post 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.
rpacdn
Forum Newbie
Posts: 7
Joined: Thu Feb 21, 2008 9:27 pm

Re: Can not capture Form data using PHP code

Post 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.
Post Reply