We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,313 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Getting values from php

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); 
?>
5
Contributors
7
Replies
1 Week
Discussion Span
4 Months Ago
Last Updated
8
Views
Zaina jee
Deleted Member

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

pritaeas
Posting Prodigy
Moderator
9,310 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,465
Skill Endorsements: 86

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

Zaina jee
Deleted Member

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

jstfsklh211
Junior Poster
100 posts since Apr 2011
Reputation Points: 34
Solved Threads: 27
Skill Endorsements: 1

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

Zaina jee
Deleted Member

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");
?>
diafol
Keep Smiling
Moderator
10,668 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,514
Skill Endorsements: 57

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?

Zaina jee
Deleted Member

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.

rholdbataller
Newbie Poster
4 posts since Jan 2013
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0830 seconds using 2.77MB