Member Avatar

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

<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? 
    <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/>
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">
    <input type="button" value="Submit">

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

$file = 'form.txt'; 
$fh = fopen($file, 'w') or die("Can't open file");
fwrite($fh, $Name); 
fwrite($fh, $Address); 
fwrite($fh, $Email); 

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

Member Avatar

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

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

Member Avatar

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

Member Avatar

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:

    $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']); 
    //the above needs error checking and validation
    $_SESSION['formMsg'] = 1;
    $_SESSION['formMsg'] = 2;
header("Location: form.php");
Member Avatar

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?

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.