Page 1 of 1

form using PHP

Posted: Mon Sep 11, 2006 4:15 am
by Circle
I got 2 problems trying to use a form to collect information.

This is the code I'm using:

Code: Select all

echo "<form action='info.php' method='POST'>\n
	<input type='text' name='name'>\n
	<imput type='submit' value='Submit name'>\n
	</from>\n";
My first problem is that when I open the file no submit button shows.

The second is a question about how I can set the name to for example $name so I can write it to a different file.

Thanks in advance.

R.

Posted: Mon Sep 11, 2006 4:27 am
by GM
You spelt "input" wrong.

Second question: You can do

Code: Select all

"<form action='".$name."' method='POST'>"
etc. etc.

Posted: Mon Sep 11, 2006 4:34 am
by justin.he
first you write 'imput', So you can't see the button. change with 'input'.

second

Code: Select all

echo "<form action=$name method='POST'>\n 
        <input type='text' name='name'>\n 
        <imput type='submit' value='Submit name'>\n 
        </from>\n";

Posted: Mon Sep 11, 2006 4:38 am
by GM
justin.he wrote:

Code: Select all

echo "<form action=$name method='POST'>\n 
        <input type='text' name='name'>\n 
        <imput type='submit' value='Submit name'>\n 
        </from>\n";
You are missing the single quotes - also, the closing form tag is spelled wrong:

Code: Select all

echo "<form action='$name' method='POST'>\n 
        <input type='text' name='name'>\n 
        <imput type='submit' value='Submit name'>\n 
        </form>\n";

Posted: Mon Sep 11, 2006 4:39 am
by Circle
If I change it do I still need to keep

Code: Select all

<method='POST'>
if I use it in the same file?

Posted: Mon Sep 11, 2006 4:41 am
by volka
yes.

Posted: Mon Sep 11, 2006 4:49 am
by Circle
I just realised there will be more then one field that needs to have content before it gets submitted but if I will use:

Code: Select all

echo "<form action='".$name."' method='POST'>\n
everything will be set to $name

Is there any way to prevent this and I can get serveral for example $name, $street etc.?

Sorry for the noob questions but I never used PHP in a form before.

Posted: Mon Sep 11, 2006 4:49 am
by GM
Hmm. I think you need a crash course in HTML forms...

The action of the form is the file in which the user's data will be processed when the form is submitted.

The method of the form is the way in which the data gets to the processing page. The two methods are GET and POST.

GET is useful for results pages that can be bookmarked, because the data is appended to the querystring, so it's good for things like search forms, where the result page can be bookmarked.

POST is slightly more secure, because the data is not immediately visible to the end user, and therefore is not as easy to change (it is still possible though). POST is used for updates to databases. Most browsers will also issue a warning if the user attempts to submit post data twice, by using the back button.

In terms of PHP, all you need to know is that if our form uses method "GET", then all your data will appear in the PHP superglobal "$_GET". If you use "POST", then your data is available in "$_POST". If, for some reason, you are not sure how the data will arrive, you can also use "$_REQUEST" which contains both GET and POST data.

Posted: Mon Sep 11, 2006 4:55 am
by Circle
Thanks GM I'm aware of the differnce between POST and GET after reading a 450 pages book on PHP5 the only problem is it wasn't in my own language so I understood half of it witch now results in my asking people for help to understand some issues I run into.

But could you also look at my previous question about how I can make it possible to use more then only one variable in this like: $name, $street etc.?

Thanks for all the help you offered allready.

Edit: thanks apply's to you all

Posted: Mon Sep 11, 2006 4:55 am
by GM
Circle wrote:I just realised there will be more then one field that needs to have content before it gets submitted but if I will use:

Code: Select all

echo "<form action='".$name."' method='POST'>\n
everything will be set to $name

Is there any way to prevent this and I can get serveral for example $name, $street etc.?

Sorry for the noob questions but I never used PHP in a form before.
You are confusing the form action with the form fields.

The action is where all the data gets sent for processing. The fields contain the data, and are made up of various HTML input elements:

Code: Select all

<form name="myform" action="myscript.php" method="POST">
  <input name="username" type="text" value="" \>
  <input name="password" type="password" \>
  <input name="address" type="text" value="" \>
  <select name="favourite_colour">
    <option value="0" \>Red
    <option value="1" \>Blue
    <option value="2" \>Green
    <option value="99" \>Other
  </select>
  <input type="submit" name="submit" value="Submit Info!" \>
</form>

Posted: Mon Sep 11, 2006 5:02 am
by Circle
It's early so I'm a bit confused over here now is it possible to add a

Code: Select all

action=''
To each individual line like:

Code: Select all

<input name="username" type="text" action='".$name."' value="" \>

Posted: Mon Sep 11, 2006 5:08 am
by GM
No. All the elements in a form need to be processed on one page (often the same page that the form is on).

The entire form is processed by the script you specify in the action property. If you leave the action property out, then the data is sent to the same page that the form is on.

Think of the <form></form> tags as the container of all the fields. This provides the instructions of where the data elements are going to be sent, and how to send them. The actual data itself is made up of the user input.

Maybe if you could explain what you are trying to achieve I could help a little bit better. Why do you want an action on each data element? What would that achieve?

Posted: Mon Sep 11, 2006 5:13 am
by Circle
I'm trying to collect info from visitors like their name, adress etc. and then write it in a different file.

For example I want to collect their $name and $adres and the write it to a file called names_and_adresses.php

That result in something like:

Code: Select all

$name=inserted name
$adress=inserted adress
This so it can be used in other files with the include function.

Posted: Mon Sep 11, 2006 5:32 am
by GM
OK. When you say "write it to a different file" do you mean actually write the information (as a kind of log), or do you want the information to be available to use by the page?

Either way, you need to write some code that processes the data.

If all you want to do is log the information in a file, you can create a form similar to the one I wrote above, and in the action you could put (for example) "log_data.php".

You need then to create a file called log_data.php, which would need to look something like this:

Code: Select all

<?php

//check if the $_POST array is initialised
if(isset($_POST['username'])) {

   // open a text file in append mode. If the file doesn't exist, php will 
   // attempt to create it.
   $outfile = fopen('my_log_file.txt', 'a');

   // loop through each data in the $_POST array, and write it to the file
   foreach($_POST as $field_name=>$field_value) {
      fwrite($outfile, $field_name.'='.$field_value.'\r\n';
   }

} else {
//$_POST is not initialised, so no data
   die("No data submitted");
}
?>
Please note, I haven't tested any of this, and I haven't done any error checking or sanity checking of the data.

Notice that on this page, the $_POST array will contain all the data inputted by the user on their form.