1,105,310 Community Members

Getting values from php

Zaina jee
Deleted Member
 
0
 

I have made a form and want to get values in form.php when Submit button is clicked.These values are write on a file form.txt.How can I do this??

<html>
<form  action="form.php" method="post">
Name:<input type="text" name="Name" ><br/>
Address:<input type="text" name="Address"><br/>
Email:<input type="text" name="Email"><br/>
Why do you want to learn Javascript?
<input type="radio"> Do not know<br/> 
<input type="radio"> Because my boss told me to <br/>
<input type="radio"> Generally interested <br/>
<input type="radio"> It might come in useful <br/>
How did you get to this site? 
<select>
    <option> I found it by accident </option><br/>
    <option> Through WDF </option><br/>
    <option> Through ABC </option><br/>
    <option> Through a search engine </option><br/>
    </select> 
I'd like additional information about<br/> 
    <input type="Checkbox"> C++<br/>
    <input type="Checkbox"> Visual Basic<br/>
    <input type="Checkbox"> HTML <br/>
    <input type="Checkbox"> CSS<br/>
<textarea cols="10" rows="20" name="mytextarea">
    </textarea>
    <input type="button" value="Submit">
</form>
</body>
</html>

Here is the form.php.What is the mistake in it and how can I correct it?

<?php
$file = 'form.txt'; 
$fh = fopen($file, 'w') or die("Can't open file");
fwrite($fh, $Name); 
fwrite($fh, $Address); 
fwrite($fh, $Email); 
fclose($fh); 
?>
Member Avatar
pritaeas
mod_pritaeas
11,285 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,830 [?]
Skill Endorsements: 154 [?]
Moderator
Featured
Sponsor
 
1
 

Instead of $Name use $_POST['Name']

Zaina jee
Deleted Member
 
0
 

I have tried $_POST ['Name']...But error message comes that it is undefined.

Member Avatar
jstfsklh211
Junior Poster
155 posts since Apr 2011
Reputation Points: 38 [?]
Q&As Helped to Solve: 35 [?]
Skill Endorsements: 2 [?]
 
1
 

add print_r($_REQUEST); to the top of your page and post the results

Zaina jee
Deleted Member
 
0
 

I have also tried print_r($_REQUEST). But error comes that it is undefined.

Member Avatar
diafol
Where are my eyes?
12,940 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
1
 

It will be when you load the page for the first time or even refresh as it's looking for the variable before the page has been submitted to itself.

<form  action="form.php" method="post">

If the form is in form.php iteself, then this is a bad idea. Try to use a form handler file to deal with submissions. You then get to stop any resubmissions on page reload.

You could do something like this - have action="form_handler.php" and then in that file have:

<?php
session_start();
if(isset($_SESSION['formMsg']))unset($_SESSION['formMsg']);
if($_POST){
    $file = 'form.txt'; 
    $fh = fopen($file, 'w') or die("Can't open file");
    if(isset($_POST['Name']))fwrite($fh, $_POST['Name']); 
    if(isset($_POST['Address']))fwrite($fh, $_POST['Address']);
    if(isset($_POST['Email']))fwrite($fh, $_POST['Email']); 
    fclose($fh);
    //the above needs error checking and validation
    $_SESSION['formMsg'] = 1;
}else{
    $_SESSION['formMsg'] = 2;
}
header("Location: form.php");
?>
Zaina jee
Deleted Member
 
0
 

Thanks. Now I have known my mistake.I also want to know that if I want to get value selected from checkbox, radio button and drop down list, what should I have to do?

Member Avatar
rholdbataller
Newbie Poster
4 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

For the radio buttons and dropdown list you can do the same thing you did for "text" inputs. Simply put a "name" attribute value in each radio button(all radio buttons must have an identical "name"). While put a "name" attribute value only inside <select> tag for dropdown list. Lastly for the checkboxes, apply a different "name" attribute value in each checkbox and assign also a value for the "value" attribute in each checkbox (ie. <input type="Checkbox" name="langHTML" value="HTML"> HTML<br/> <input type="Checkbox" name="langC++" value="C++"> C++ <br/>).

Hope this helps.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article