Posting Form data to HTML page

Status
Not open for further replies.

kizzeith

In Runtime
Messages
159
Location
Minneapolis, MN
I'm trying to figure out how to POST data from an HTML form to an HTML. Basically, I want to create an easy way for multiple, non-coders to add information to a webpage.

The HTML form will contain a few fields and when it is submitted, I want the data to be added to the existing information on a seperate html page which can be viewed publicly.

My webhosting service allows php-scripting and MySQL, but I don't know how to use MySQL.

Any help would be greatly appreciated!
 
So, when your form is submitted you want it to create its own HTML page, containing only what they entered into the form?

There should be a place in your hosting somewhere to setup a MySQL database. Search your webhost's FAQ and you should find out how.

Post back with what exactly you want and I'll be happy to help.
 
Basically,

I want to have a page for weather-related cancellations that contains information entered into a POST form.

I want the info to collectively gather as it is entered.
 
Ok, if this isn't what you wanted let me know and I'll try to fix it.

mysql.php
This file is to set the host name, user name, password, and database name for your MySQL database (which you setup in your hosting somewhere).

PHP:
<?php

// Edit the content of these variables as needed
$host = 'localhost'; // This is the host, and is normally localhost
$user = 'root'; // This is the database username
$pass = 'root'; // This is the database password
$db   = 'dbname'; // This is the database name itself

$connect = @mysql_connect ($host, $user, $pass) or die ('<b>ERROR:</b> Could not connect to database!');
$select  = @mysql_select_db ($db) or die ('<b>ERROR:</b> Could not select database!');

?>

enter.php
This file will enter the information from the form to the database

PHP:
<?php

require_once ('mysql.php');

if (isset ($_POST['submit'])) {
        // These next three variables get the information from the text fields
        // below
	$field1 = $_POST['field1'];
	$field2 = $_POST['field2'];
	$field3 = $_POST['field3'];

	if (!empty ($field1) && !empty ($field2) && !empty ($field3)) {
		$sql = "INSERT INTO table_name (field_id, field1, field2, field3) VALUES (0, '".$field1."', '".$field2."', '".$field3."')";
		if ($r = mysql_query ($sql)) {
			echo 'Your info has been entered successfully!';
		}
	} else {
		echo 'You didn\'t fill out all the required fields, please try again!';
	}
} else {
	echo '<form action="enter.php" method="post">
	Field 1 <input type="text" name="field1" />
	<br />
	Field 2 <input type="text" name="field2" />
	<br />
	Field 3 <input type="text" name="field3" />
	<br />
	<input type="submit" name="submit" value="Submit" />
	</form>';
}

?>

view.php
This file will view the information in the database, ordered by newest to oldest

PHP:
<?php

require_once ('mysql.php');

$sql = "SELECT * FROM table_name ORDER BY field_id DESC";
if ($r = mysql_query ($sql)) {
	$num = mysql_num_rows ($r);

	if ($num > 0) {
		echo '<table border="0" width="500px">
		  <tr>
		    <td align="center"><b>Field 1</b></td>
			<td align="center"><b>Field 2</b></td>
			<td align="center"><b>Field 3</b></td>
		  </tr>';

		  while ($row = mysql_fetch_array ($r)) {
			  echo '<tr>
			    <td align="center">'.$row['field1'].'</td>
				<td align="center">'.$row['field2'].'</td>
				<td align="center">'.$row['field3'].'</td>
			  </tr>';
		  }

		  echo '</table>';
	} else {
		print 'There is no information yet.';
	}
}

?>

You will have to change "table_name" in both enter.php and in view.php to whatever you call your database table name.

I recommend taking a small, basic tutorial on MySQL so you are familiar with the terms and how everything works. It's not very complicated and can be learned quickly.

If this isn't what you wanted, I will try to change it so it is what you want. Let me know if you have any problems.
 
I definitely appreciate all of your help.

I am confused about how to upload this information on my server. I use dreamhost as my hosting service and haven't been able to figure out how to use the admin tool.

Can I just FTP the files?
 
You have to make the files with the name I put above the code, and then put the code into the file. Of course, that is a very basic script because I didn't have specifics. If you want it more customized etc let me know.

Yes, you can just upload via FTP.

As for the PHPMyAdmin, sometimes you can't make actualyl create a database with it. Some hosts are different. There should be a support section with a FAQ or something showing how to make a MySQL database.

EDIT: Also make sure you make the necessary changes that I explained.
 
I created a db with a table and entered the host/login info to the mysql.php file. Also, I created fields in the table matching the field1, field 2, field 3 ID that is in the php script. I replaced table_name in both view/edit.php with my table name.

When I enter the info into edit.php, it gives me a blank white screen and no data is POSTed to the DB.

Any ideas why?
 
Still can't get this thing to function properly, I think my table may be set up incorrectly.

I created a Database which the form seems to be able to access. I can tell this because when I enter an incorrect password in the mysql.php file it alerts me that it cannot access the db.

When I enter/submit the fields, nothing happens. The browser just blanks.

I have added the table name as: " INSERT INTO 'severeweather'.'cancel' but still no result. I have also tried just entering it as 'cancel' (without the 'severeweather'.)

There are 4 fields in the table, field_id, field1, field2, field 3.

Any idea where I should look? i know it's getting close, but it's not quite there.

I appreciate everything you've done thus far Crazed.

Thanks.
 
Status
Not open for further replies.
Back
Top Bottom