472,961 Members | 2,130 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,961 software developers and data experts.

Form handling with PHP

I have a bunch of pages with long forms, with lots of input
types-text, radios, textareas, and the debugging process has become
overwhelming.
What I need to happen is to make sure that
1. Every field on the page is filled out-if not, a list of incorrect
fields is displayed, and any radio boxes that were checked still are
2. The fields are queried to a table in a database. (The table has the
same column names as the field names)

Here's what I did; can someone tell me why this doesn't work?

<?php
require_once ('../mysql_connect.php');
$page_title = 'Test page';

if (isset($_POST['submit'])) { // Handle the form.

// Create an empty new variable.
$message = NULL;

// Create a function for escaping the data.
function escape_data ($data) {
global $dbc; // Need the connection.
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}
return mysql_real_escape_string($data, $dbc);
}

//Create an array of all formfield names
$required=array('field1','field2','field3');

//Check to see if all field names are filled in
function field_check($required) {
foreach($required as $q) {
if(empty($_POST[$q])) {
$message = 'Error: The following field was not filled in: $q';
$ok=FALSE;
}
else {
$ok;
}
}
}

// If no empty posts are returned
if ($ok) {
//set a variable for the current user
$u = addslashes($_COOKIE['user_id']);

//insert all variable values into the table
foreach($required as $q) {
$query = "INSERT INTO test_table ($q) VALUES ('$'.'$q')";
}

// Run the query
$result = @mysql_query ($query); // Run the query.

// If it ran OK, write the the page with a link to continue.
if ($result) {
include ('templates/header.inc');
$page_title = 'Entry Recorded';
echo "<p><b><a href=\"next_page.php\"
class=\"content\">Continue</a>";
include ('templates/footer.inc');
exit(); // Quit the script.
}
// If it did not run OK, show an error
else {
$message = '<p>Your form could not be recorded due to a system
error. We apologize for any inconvenience.</p><p>' . mysql_error() .
'</p>';
}
}

// Close the database connection
mysql_close($dbc);
}

include ('templates/header.inc');

// Print the error message if there is one.
if (isset($message)) {
echo '<font color="red">', $message, '</font>';
}

// Print the page.
?>

<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<table>
<tr>
<td><input type="radio" name="field1" value="1"</td>
<td><input type="radio" name="field1" value="2"></td>
<td><input type="text" name="field2"></td>
<td><textarea name="field3" rows="20" cols="30">
</textarea></td>
</tr>
</table>
<p>
<input type="submit" name="submit" value="Add to Record">
</p>
</form>
<?
include ('templates/footer.inc'); // Include the HTML footer.
?>
Thanks.
Jul 17 '05 #1
2 2056
I noticed that Message-ID:
<dd**************************@posting.google.com > from Glyphman
contained the following:
Here's what I did; can someone tell me why this doesn't work?


It doesn't work because it isn't coded properly...
What bit doesn't work?

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #2
Hello,

On 02/16/2004 02:15 PM, Glyphman wrote:
I have a bunch of pages with long forms, with lots of input
types-text, radios, textareas, and the debugging process has become
overwhelming.
What I need to happen is to make sure that
1. Every field on the page is filled out-if not, a list of incorrect
fields is displayed, and any radio boxes that were checked still are
2. The fields are queried to a table in a database. (The table has the
same column names as the field names)


You may want to try this class instead of reinventing the wheel. It can
compose and validate HTML forms, either on client side with Javascript
or on the server side with the class code. It supports many common
validation types.

You can compose the forms output with a Smarty plugin that makes it
possible to integrate with the class. The generated output embeds the
necessary Javascript but you do not need to know Javascript at all.

http://www.phpclasses.org/formsgeneration

--

Regards,
Manuel Lemos

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/

PHP Reviews - Reviews of PHP books and other products
http://www.phpclasses.org/reviews/

Metastorage - Data object relational mapping layer generator
http://www.meta-language.net/metastorage.html
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

72
by: Stephen Poley | last post by:
I have quite often (as have probably many of you) come across HTML forms with irritating bits of Javascript attached. The last straw on this particular camel's back was a large form I was asked to...
4
by: Stuart Perryman | last post by:
Hi, I have the following code which works just fine in IE6 but not in Firefox. It is an extract of several table rows each with an individual form. It is generated by php. <form...
4
by: John Fereira | last post by:
So, one of the limitations of multipart-form handling is that when an <input type="file" ..> tag is used it will bring up a window which allows a user to select a file for upload but won't allow...
1
by: google | last post by:
It would seem that when I assign an HTML entity to a form text input using "inline" javascript that it will display properly. But when trying to set it via a function call, the entity text shows...
8
by: swathky | last post by:
I've tried mutiple things but no go -- (sorry this is so long) I'm collecting a 5 digit number in an input box function and all works fine until a number is passed that doesn't exist in the...
15
by: Steve | last post by:
I have a form with about 25 fields. In the BeforeUpdate event of the form, I have code that sets the default value of each field to its current value. For a new record, I can put the focus in any...
16
by: TD | last post by:
This is the code under a command button - Dim ctl As Control For Each ctl In Me.Controls If ctl.BackColor <> RGB(255, 255, 255) Then ctl.BackColor = RGB(255, 255, 255) End If Next ctl
5
by: RAJ | last post by:
hi plz tell me how to know "how window is going to close"... i have to right code for X button of forms... plz telll me thanks bye
6
by: nadeem_far | last post by:
Hello All, I am working on a .Net desktop application and I am having problem displaying a form. I am using C# and version 1.1 of the framework. here is how the code looks likes and I will...
2
by: Esa | last post by:
Hi, I'm having problems with one strange web system where submitting an application and making queries about its handling status require a series of form submits and response parsing - all in...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.