Script to Post Entered Content directly to the webpage

OK. I’m not at all familiar with PHP, so I’m looking for someone to help (read: write) me with a short script to do the following:

User goes to website, enters text into field 1A (Username), 2A (Age), and 3A (Month of Birth Number - i.e. June=6).

User hits submit button and data is posted to the webpage.

Thanks a lot,
~TehYoyo

Add this code to a PHP file and load it up into your browser.

form_test.php


<?php 
if( isset($_POST) ) {
var_dump( $_POST );
echo '<hr />';
?>

<form action="" method ="POST">
<input type="text" name="field1A" />
<input type="submit" / >
</form>

That’ll get you started, now add html form fields.

If you don’t know what form fields are then look for a basic html forms tutorial.

OK. First, thanks a lot. It looks to me like the only part of the HtMLmthe PHP refers to is the method of the form, right? Meaning, I don’t have to name any HTML certain things?

Thanks much.
~TehYoyo

If you type something into the form field named “field1A” and your form conforms to the basic html rules, then that variable is available to the place you post the form to (in this case itself) as part of the POST array which you access with


echo $_POST['field1A'];

You should now make a form which features more sensible variable names in order that when the form is posted back to itself you can do the equivalent of:


// if POST isset etc
echo "Hello there, ". $_POST['first_name'] .  " " . $_POST['last_name'] ;

OK great, thanks.

So the first code you gave me only actually posts an <hr />, right?

Not exactly…

The line below just tells the html to post data back to the server, using the script “process_form.php”.


<form action="process_form.php" method ="POST">

This line tells it to post the value of this input control as a variable named field1A.


<input type="text" name="field1A" />

This line, in “process_form.php”, is used to get the field value, so you can do stuff with it.


$field1A = $_POST['field1A'];

The sample code previously given was offered as simple demo of an html form that posts back to itself. The php block at the top just detects if anything was previously posted, and if so, shows it, followed by an <hr /> to separate it from the form display.

Presumably, though, you want this info stored somewhere for later use? At the moment, the info will be lost once the page is closed.

If you want to store the information for display later, then this is where a CMS is handy. You might need to clarify what you are trying to do to get the answers you need. :slight_smile:

No that is not quite correct.

  1. The action attribute refers to the server side procesing script you want to send the form data to. It could be to another page from the page containing the form. eg <form action=“formProcessor.php” … or it could be to the page containing the form eg <form action=“#”…
  2. form data is sent as name/value pairs for each of the form’s input controls…eg <input type=“text” name=“txtUsername”… or <input type=“checkbox” name=“chkCountry” value=“france”…The entered username is sent as the name txtUsername and if the user checks the checkbox, the string france is sent as the name chkCountry.
  3. The method attribute determines how the form data is sent to your server side script. It can be set to POST or GET. If it is set as a GET then all your form data is appended to the url the action attribute is set to as name value pairs…eg…formProcessor.php?txtUsername=fred&chkCountry=france…and the sent form data can be got in formProcessor.php in $_GET[‘txtUsername’] etc…If method is set to POST then all the form data is sent via the headers in formProcessor.php and the form data can then be got in formProcessor.php with $_POST[‘txtUsername’] etc. Usually you use GET if you want to give your users the opportunity to bookmark database driven web pages. Otherwise you would normally use POST.
  4. You don’t need a CMS to store form data. This looks like a homework or self teaching type exercise, so a CMS is gross overkill if that is the case. You can store the form data is just a plain text file or write a few lines of basic code to store it in a database.

Have a read through this forms tutorial and [URL=“http://www.tizag.com/phpT/examples/formex.php/”]form processing tutorial.