Jump to content

HTML Form to PHP File - Help Requested


vigiw

Recommended Posts

Hi everyone,

I have a fairly small knowledge of PHP scripting, but I know what I want to do here.

I want to take an HTML form and post the data to a PHP file. I will explain:

I am working on a whole new part of my site and it would be a great addition to it; anyway... I would like to have an HTML file with an HTML form in it. This form would contain several fields that would then need to be transported to a PHP file.


I would like for the PHP file to show the data entered into the form. And I am not talking about showing the user who entered the information in, I'm talking about posting the data to the public on the page (the page will be in my site design using frames or something similar).

Is there a way to keep that data shown to everyone (the public)?

And if so, is there a way to erase the data after a given number of hours. This is going to be a user-defined weather alert post page. The user can set expiration times/dates with a specificed number of hours.

Could someone please help me with the scripting of the PHP file, and also anything to connect the HTML form to the PHP file, such as "<form action" which I will need to know.

Please help me out, this would be a great addition and I can't wait to inform everyone of important weather information (it will be collaborative, that is why I'm using forms).

Please reply as soon as possible, any help is truly appreciated!! Thanks in advance! ;D
Link to comment
Share on other sites

The best thing for you to do is use a database here. MySQL, Mssql, whatever. If you host the site then you can see if you have it. If not, you could also make the PHP page that it's sent to save the information as a file.
It'd be best to read some tutorials around here to learn how to use PHP and MySQL together and theres also a few on files too I believe. :) Just check out the tutorial section, it helps ALOT.
Link to comment
Share on other sites

I have a MySQL database and most of my site is PHP ironically  ;) (http://www.vigilantweather.com)... I am doing to branch this in a different directory and not put it in my main site as seen with URL above.  But as mentioned, I am a n00b at PHP and MySQL, my site is phpBB/Integramod.

I will look around this site.  Thanks for the (quick) response!  Any ideas of how to integrate this?  I would have never thought to include a DB in this situation!
Link to comment
Share on other sites

Hehe sorry it took me longer than i expected... I got a little side tracked...

any ways...
db.php
[code=php:0]
<?
$db_host = "localhost"; //your mysql host
$db_user = "root"; //mysql username
$db_pass = "root"; //mysql password
$db_name = "db1"; //name of the database youre using
$table_name = "data";

mysql_connect($db_host, $db_user, $db_pass) or die (mysql_error()); //trys to connect to the database and if it fails displays the error mysql out puts
mysql_select_db($db_name) or die (mysql_error()); //trys to select your database and returns the error if it is unsuccessful
?>
[/code]

sumbit.php
[code=php:0]
<?
include("db.php");

$field1 = $_POST['field1'];
$field2 = $_POST['field2'];
$field3 = $_POST['field3'];
if($_POST['sub'] == "yes") {
if($field1 == "") { $error .= "You did not put any information for field1<BR>"; } //checks to see if the user filled in field1, or if they put in too much information, and defines the variable error if they did
if(strlen($field1) > 30) { $error .= "Field1 is too long.<BR>"; }

if($field2 == "") { $error .= "You did not put any information for field2<BR>"; } //checks to see if the user filled in field1, or if they put in too much information, and defines the variable error if they did
if(strlen($field2) > 256) { $error .= "Field2 is too long.<BR>"; }

if($field3 == "") { $error .= "You did not put any information for field3<BR>"; } //checks to see if the user filled in field1, or if they put in too much information, and defines the variable error if they did
if(strlen($field3) > 256) { $error .= "Field3 is too long<BR>."; }
}
if($_GET['success'] != "yes") { ?>
<form action="submit.php" method=POST>
Field1: <input type="text" maxlength="30" name="field1" <? if(isset($field1)) { echo "value=\"" . $field1 . "\""; } ?>><BR>
Field2: <input type="text" maxlength="256" name="field2" <? if(isset($field2)) { echo "value=\"" . $field2 . "\""; } ?>><BR>
Field3: <input type="text" maxlength="256" name="field3" <? if(isset($field3)) { echo "value=\"" . $field3 . "\""; } ?>><BR>
<input type="hidden" name="sub" value="yes">
<input type="submit" value="Submit!">
</form>
<?
}
/*
note: .= defines a variable or adds onto it if it already exists... for example
$var1 = "hello";
$var1 .= " my name is corbin";
echo $var1;
would out put hello my name is corbin
*/
if(isset($error)) {
echo "There was one or more error!<BR>";
echo $error;
}
else {
if(($_GET['success'] != "yes") && ($_POST['sub'] == "yes")) { // checks to see if $error is set of if the GET variable of success does not equal yes
$field1 = htmlspecialchars($field1);// if $field does not return an error it replaces any thing that could be interpetted as a browser as html
$field2 = htmlspecialchars($field2);
$field3 = htmlspecialchars($field3);
$q = mysql_query("INSERT INTO `" . $table_name . "` (`field1` , `field2` , `field3`) VALUES ('" . $field1 . "', '" . $field2 . "', '" . $field3 ."')");
if($q) { //what to do if the mysql query is successful
$error = NULL;
?>
<meta http-equiv="refresh" content="0;url=<? echo $_SERVER['HTTP_SELF'] . "?success=yes"; ?>">
<?
//by forwarding to its self the page will clear its post values so if a user hits refresh its not inserted into the database again...
}  //the meta tag has it forward it to this page cept with the $_GET['success'] tag set
}
elseif($_GET['success'] == "yes") { //what to do if $_GET['success'] does equal yes
echo "Information successfully put in the database!<BR> View it <a href=\"view.php\">Here</a>.";
}
}
mysql_close(); //closes the link to mysql}
?>
[/code]

view.php

[code=php:0]
<?
include("db.php");
$q = mysql_query("SELECT * from `" . $table_name . "`");
while($row = mysql_fetch_assoc($q)) {
echo "Field1: " . $row['field1'] . ", Field2: " . $row['field2'] . ", Field3: " . $row['field3'] . "<BR>";
}
if(mysql_num_rows($q) < 1) { echo "No data has been put into the database yet"; }

mysql_close(); //closes the link to mysql
?>
[/code]

The layout of the table I was using was 3 columns... field1 varchar 30 characters long field2 varchar 256 chars long field3 varchar 256 chars long...

to create that table make a php file named
create_table.php

and put
[code=php:0]
include("db.php");
mysql_query("
CREATE TABLE IF NOT EXISTS `" . $table_name . "` (
  `field1` varchar(30) NOT NULL default '',
  `field2` text NOT NULL,
  `field3` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
");
[/code]
Link to comment
Share on other sites

Thank you!! I cannot thank you enough!!! It took me a bit to reply to this because I was saving the files, uploading, and trying them out.  Great work; but there may be something or possibly a few things wrong, I'm not sure...

1) I opened submit.php and typed in random words for each field.  When I clicked submit, a 404 error came up.  My address bar only said http://www.vigilantweather.com/forecast/submit.php which is the location of the file where the form is. (??Hmm??)

There was no data entered into the Database.  I clicked view and it gave me PHP errors, one of them saying that there was no data in the DB.  In phpMyAdmin I saw that there was no data in the DB, and no tables either.  Am I supposed to add tables?

I'm a bit in the dark on how to edit this and correctly send it on over to the DB for viewing on view.php.

Please get back to me as soon as you can.  Thanks again, I really appreciate your help!!!!
Link to comment
Share on other sites

hii,
    just try out this simple and modified code of CORBIN... , hope this solve ur problem...

submit.php
<?
//include("db.php");

if($_POST['s1'])
{

$field1 = $_POST['field1'];
$field2 = $_POST['field2'];
$field3 = $_POST['field3'];


if(empty($field1) | empty($field2) | empty($field3))
{
  echo " PLEASE FILL ALL INFO IN THE FORM";
  form();
}
else
{
$q = mysql_query("INSERT INTO `" . $table_name . "(field1 , field2 , field3) VALUES ('$field1', '$field2', '$field3')");
echo "<BR>YOUR INFORMATION IS SUCCESSFULLY SAVED..<BR>";
form();
}

}
else
{
form();
}
function form()
{
?>
<form action="<? $PHP_SELF ; ?>" method=POST>

Field1: <input type="text" maxlength="30" name="field1" value=<?= $field1 ;?> ><BR>
Field2: <input type="text" maxlength="256" name="field2" value=<?= $field2 ;?> ><BR>
Field3: <input type="text" maxlength="256" name="field3" value=<?= $field3 ;?> ><BR>
<input type="submit" value="Submit!" name="s1">

</form>
<?
}
?>
Link to comment
Share on other sites

hii,
    just try out this simple and modified code of CORBIN... , hope this solve ur problem...

submit.php
<?
include("db.php");

if($_POST['s1'])
{

$field1 = $_POST['field1'];
$field2 = $_POST['field2'];
$field3 = $_POST['field3'];


if(empty($field1) | empty($field2) | empty($field3))
{
  echo " PLEASE FILL ALL INFO IN THE FORM";
  form();
}
else
{
$q = mysql_query("INSERT INTO `" . $table_name . "(field1 , field2 , field3) VALUES ('$field1', '$field2', '$field3')");
echo "<BR>YOUR INFORMATION IS SUCCESSFULLY SAVED..<BR>";
form();
}

}
else
{
form();
}
function form()
{
?>
<form action="<? $PHP_SELF ; ?>" method=POST>

Field1: <input type="text" maxlength="30" name="field1" value=<?= $field1 ;?> ><BR>
Field2: <input type="text" maxlength="256" name="field2" value=<?= $field2 ;?> ><BR>
Field3: <input type="text" maxlength="256" name="field3" value=<?= $field3 ;?> ><BR>
<input type="submit" value="Submit!" name="s1">

</form>
<?
}
?>
Link to comment
Share on other sites

:o ;DSorry about that!! Now submit.php ... now when i do it, all it does is load (i see the green bar for a bit) quickly and then it does nothing, same page, same form, same data.  Then I go to view.php and still the same errors (no records in DB)

Thank you for all of your help... any info? Thanks again!
Link to comment
Share on other sites

Hi Corbin,

Sorry for the late reply.  I already edited db.php... will create_table.php create my table for me?  I entered http://vigilantweather.com/forecast/create_table.php in my browser and it is blank and nothing happened.  Do you have the SQL to run (execute) in phpMyAdmin?  I realize it should be something like in the code of create_table.php, but I don't know how to convert the PHP version of how to create tables (,etc.) and the MySQL way to run the scripts.

Thanks!
Link to comment
Share on other sites

Ok... here's the deal now.

The DB is hooked up so forget my last message!  ;D

Another thing, and this is something pretty large in the project:

Like I said in post 1, can I have a field in the form allowing the user to specify a number of hours for data to last, and then clear out the data from the database after the user-specificed amount of hours for the thing to expire.  This will be a weather alert type thing, and I don't want to display old stuff after it expires in hours or whatever the user (form filler, rather) specifies in the form.  It would be very tedious to do this manually each and every time.

Please help me out with this, looks like I'm almost there!  Thanks!! ;D
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.