Page 1 of 1

Reading From A Form

Posted: Wed Mar 31, 2004 3:23 am
by eoinmick
Hello; was wondering exactly the in's and outs of inputing data from a from into mysql table, I developed the input page in html and presume that I need an insert function; do I put it on the same form or on another form such as check.php.
Here is the input texts I have put.

Code: Select all

<html>

<head><title>Submit Flight Details</title></head>

<body text="blue" bgcolor="">
<HR SIZE=4 style="background-color: blue">

<body>
<h2>Flights</h2>
<form method="post"action="">
<table cellpadding=2 cellspacing=0 border=0>

<td>Flight Id    :</td><td>     <input type="text" size="10" maxlength="4"  name="flightId"  value=""></td><tr>

<td>Fly Day:</td><td><input type="text" size="30" maxlength="10" name="flyday" value=""></td></tr>

<td>Time:</td><td> <input type="decimal" size="10"    name="time"value=" "></td></tr>

<td>Destination From</td><td>	<input type="text" size="30" maxlength="15" name="destinationfrom" value=""></td></tr>

<td>Destination Till</td><td> <input type="text" size="30" maxlength="15" name="destinationtill" value=""> </td><tr>

</table>
<input type="Submit" name="flights" value="Enter Flight Details">
</form>

Posted: Wed Mar 31, 2004 3:33 am
by Pozor
Hello,

in generall i say it is a good idea to separate the steps into files. I hadn't do this in the past, and it is not very good, when you want to do a review or make some changes...

It is a good beginning to check the formulardata in the same skript, and when the data is valid give it to a second skript that do the further processing with the data.

first point to the same file (with $PHP_SELF or $_SERVER['PHP_SELF']):

Code: Select all

<form method="post"action="same_file.php">
when all data is valid point to the next file:

Code: Select all

<form method="post"action="next_file.php">
The data you get with the post action is accessable with $_POST['name_of_formular_object']

greez Pozor

Posted: Wed Mar 31, 2004 3:39 am
by markl999
I prefere to always post the form back to the same page. You can still put the form processing and html in different files if you like (i do ;))
Eg

Code: Select all

<?php
if(!empty($_POST['whatever'])){
    //process the form here
}
require_once 'theform.php';
This way it's easier to prefill in the form values with the ones they just submitted. If there are 10 fields to fill in, and only one of them is invalid (eg they forgot to fill in their last name) then rather than have them fill it all up again you can prefill the values they previously entered.

Posted: Wed Mar 31, 2004 3:44 am
by eoinmick
Do not really understand whet either of you are saying or where exactly to code it...Sorry only new to php.

Posted: Wed Mar 31, 2004 3:46 am
by Pozor
Hello

you can do this in this way, yes. But prefilling work with both possibiltites fine... :?:

you can controll the flow in this way (i do this when i work with this idea)

Code: Select all

<?php
if(1 == $_POST['sent'])
{
  //do the second step
}elseif(2 == $_POST['sent'])
{
  //do the third step  
}else
{
  //this is the first (and the fourth if you mad a loop, sometimes very useful)
}
?>
greez Pozor

Posted: Wed Mar 31, 2004 4:09 am
by idnoble
I normally separate my PHP scripts from my HTML form, but whatever you do before you can insert data into Mysql you need to be able open the for use the fopen() function and call the fields in your form using $_POST ['Fieldname']

Posted: Wed Mar 31, 2004 9:06 am
by amithn12
Since u r new to php, i would recomend , inserting the values into database using another
page to insert the values.

u have this form
<form method="post"action="insert.php">

create a insert.php file.

here is how u can get the values entered in ur input page to the insert.php page

to get the flight id entered in input page u do this

$flightid = $_POST['flightId'];

so the value entered in input page for flight id is now stored in $flightid.

like this get all the values.

when u have all the values. Insert it into the databse

$query = "INSERT into table *** (flightid,..,..) Values ($flightid,..,..)
$result = mysql_query($query);

Posted: Wed Mar 31, 2004 9:32 am
by Steveo31
eoinmick wrote:Do not really understand whet either of you are saying or where exactly to code it...Sorry only new to php.
By "uploading" the variables with the Submit button to the same page, you save the user the hassle of re-entering the values if one is unvlaid or something.

So for example, you have a form.php:

Code: Select all

<?php
//set up the checkers to make sure the values are all there:
if(!empty($_POST['username'])){
    //this page here does all the inserting and whatnot with MySQL:
    include('next_page.php');
}
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="text" name="username">
    <input type="submit">
</form>
Now, I haven't done it like this before, but I guess it works from what the others have said.

Posted: Wed Mar 31, 2004 9:47 am
by magicrobotmonkey
Yea but you have to fill in the for fields manually:

Code: Select all

<input type="text" name="username" <?php if(isset($_POST['username']) echo "value="$_POST[username]"";?>>