473,320 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

HTML forms and PHP

3
I'm somewhat of a newbie to PHP, however i would appreciate if someone would be kind enough and help me with my problem.
To give you a better understanding of what my problem is, here is what i'm trying to do.
I have a webpage (A.php) which contains an html form (formA) which has a number of text fields and a submit button. When i click the submit button i want to be able to get the value in the text field assigned to a PHP variable so that i can use those vars to create a query, however it seems that i cannot get the values of the text fields. I have tried a few different ways, but it seems that i jsut cannot get it to work.
Could there be a problem with PHP 5.1.6. I am also using mySQL 4.1 and Apache 2.0.59.

Thank you!!!

The following in the actual code:

Being a newbie is no excuse for not following the Posting guidelines!
Since this is your first entry, I have enclosed your code within html tags.
Read the Posting Guidelines before you post anything in a thread.
Especially the part about putting code within code, php or html tags! - Ronald


test.php:
[html]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>OGAY</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">


<?php

$query1 = "INSERT INTO clients (custID,companyName,contactName,address,phone,fax, email)
VALUES (null,'$companyName','$name','$address','$phone',' $fax','$email');";
print ($query1);

?>
</head>

<body>

<span style="font-size:12pt">Please fill in all fields.</span><br/><br/>
<form name="addClient" method="get" action="test.php ">
<table>
<tr>
<td>Company Name:*</td>
<td><input type="text" name="companyName" size="30" maxlength="255" /></td>
</tr>
<tr>
<td>Contact Name:*</td>
<td><input type="text" name="name" size="30" maxlength="255" /></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="address" size="30" maxlength="255" /></td>
</tr>
<tr>
<td>Phone:*</td>
<td><input type="text" name="phone" size="30" maxlength="10" /></td>
</tr>
<tr>
<td>Fax:*</td>
<td><input type="text" name="fax" size="30" maxlength="10" /></td>
</tr>
<tr>
<td>Email Address:</td>
<td><input type="text" name="email" size="30" maxlength="255" /></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="addClient" value="Add Client" />
&nbsp;&nbsp;<input type="reset" name="clear" value="Clear" /></td>
</tr>

</table>
</form>

</body>
</html>
[/html]
Oct 26 '06 #1
5 1624
You don't need the column ids and then the values. Your query should look like this:

[PHP]$query1 = "INSERT INTO clients VALUES(null,'$companyName','$name','$address','$ph one',' $fax','$email')";

print ($query1);[/PHP]
Oct 27 '06 #2
ronverdonk
4,258 Expert 4TB
I absolutely disagree! The database should never be closely connected to the performing code. I always require that programmers define the column names in their code. Reason:

When a column is added to the table or the column sequence is changed, the program MUST be changed at the same time, otherwise errors will result.

Database admin and programming often are separate functions, as in many companies. The dbadministrator must always have the freedom to change the table setup (adding columns, changing sequences, etc.) without great risk to the already running applications. In the worst case some columns will not be updated, but production can go on.

Ronald :cool:
Oct 27 '06 #3
ronverdonk
4,258 Expert 4TB
Here is some code for your form. I changed/added the following:
  • chang the GET method to the POST method
  • start the code with verification of the submitted form
  • add minor error checking for each POSTed field (just existence and max length)
  • accumulate and print any field errors found
  • when errors: re-display the form with already filled in fields displayed
  • assign POSTed variables to PHP variables after some cleansing
You'll have to add your own field checking (content, email address validity, etc.) and the MySQL code
[php]
<?php
$errors = array();
// -----------------------------------
// Verify that a form is submitted
// -----------------------------------
if (isset($_POST['addClient'])) {
// -------------------------------------------------------------------
// when submitted check the input fields (just checking lengths here)
// -------------------------------------------------------------------
if (!isset($_POST['companyName']) OR strlen($_POST['companyName']) < 5)
$errors[] = "Error in companyname";
if (!isset($_POST['name']) OR strlen($_POST['name']) < 5)
$errors[] = "Error in name";
if (!isset($_POST['address']) OR strlen($_POST['address']) < 8)
$errors[] = "Error in address";
if (!isset($_POST['phone']) OR strlen($_POST['phone']) < 10)
$errors[] = "Error in phone";
if (!isset($_POST['fax']) OR strlen($_POST['fax']) < 10)
$errors[] = "Error in fax";
if (!isset($_POST['email']) OR strlen($_POST['email']) < 5)
$errors[] = "Error in email";

// ----------------------------------------------------------
// checking done, see if there are any errors and print them
//-----------------------------------------------------------
if ($errors > "") {
print '<span style="color:red"><ul><li><b>';
print implode('</b></li><li><b>',$errors);
print '</b></li></ul></span>';
} // End IF

// ---------------------------------------
// When no errors found, process the form
// ---------------------------------------
else {
// --------------------------------------
// Assign POSTed values to PHP variables
// --------------------------------------
$companyName = strip_chars(trim($_POST['companyName']));
$name = strip_chars(trim($_POST['name']));
$address = strip_chars(trim($_POST['address']));
$phone = strip_chars(trim($_POST['phone']));
$fax = strip_chars(trim($_POST['fax']));
$email = strip_chars(trim($_POST['email']));

// --------------------------------------------------------------
// do some more checking like fields content and email validity
// --------------------------------------------------------------
//
// you can do that yourself
//

// ---------------------------------------------------------------------
// Construct and issue the mySQL command and perform actions on result
// ---------------------------------------------------------------------
// ---- connect to the MySQL server
// ---- connect to the data base
// ---- construct your INSERT statement

$query1 = "INSERT INTO clients
(custID,companyName,contactName,address,phone,fax, email)
VALUES (null,'$companyName','$name','$address','$phone',' $fax','$email');";

// ---- execute the SELECT
// ---- process result (error)
print ($query1);

} // End ELSE

} // End If submitted
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>OGAY</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<span style="font-size:12pt">Please fill in all fields.</span><br/><br/>
<form name="addClient" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<table>
<tr>
<td>Company Name:*</td>
<td><input type="text" name="companyName" size="30" maxlength="255" value="<?php echo (isset($_POST['companyName'])) ? $_POST['companyName'] : ""; ?>" /><br /></td>
</tr>
<tr>
<td>Contact Name:*</td>
<td><input type="text" name="name" size="30" maxlength="255" value="<?php echo (isset($_POST['name'])) ? $_POST['name'] : ""; ?>" /></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="address" size="30" maxlength="255" value="<?php echo (isset($_POST['address'])) ? $_POST['address'] : ""; ?>" /></td>
</tr>
<tr>
<td>Phone:*</td>
<td><input type="text" name="phone" size="30" maxlength="10" value="<?php echo (isset($_POST['phone'])) ? $_POST['phone'] : ""; ?>" /></td>
</tr>
<tr>
<td>Fax:*</td>
<td><input type="text" name="fax" size="30" maxlength="10" value="<?php echo (isset($_POST['fax'])) ? $_POST['fax'] : ""; ?>" /></td>
</tr>
<tr>
<td>Email Address:</td>
<td><input type="text" name="email" size="30" maxlength="255" value="<?php echo (isset($_POST['email'])) ? $_POST['email'] : ""; ?>" /></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="addClient" value="Add Client" />
&nbsp;&nbsp;<input type="reset" name="clear" value="Clear" /></td>
</tr>

</table>
</form>

</body>
</html>
[/php]
It is tested (crudely), so run it and see what it is doing.
Good luck!

Ronald :cool:
Oct 27 '06 #4
joker
3
Ronald, Thank you very much. I just got around now to try your code, it works great after i got rid of the else block. For some reason after it executes the error checking "if" block, it doesn't go into the "else" block, ......strange but its ok.
Thanks once again.
Nov 7 '06 #5
ronverdonk
4,258 Expert 4TB
I would be disapppointed with any programming/script language when it would go into an ELSE block after executing the IF block.

Here the reason for those two blocks are that you want (IF block) to display the errors and then re-display the form again until all fields are correctly filled in. When they are correctly filled in, the IF block will not be executed (because $errors is empty) but go straight into the ELSE block where you perform your actual data handling.

Or is that process unclear?

Ronald :cool:
Nov 7 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Bob Nolty | last post by:
Hi all -- I'm trying to create a hidden form in the document using only Javascript -- I don't know what form I want until the user clicks a button -- I tried the following code, which does...
4
by: VK | last post by:
09/30/03 Phil Powell posted his "Radio buttons do not appear checked" question. This question led to a long discussion about the naming rules applying to variables, objects, methods and properties...
22
by: Luke | last post by:
Elements with name attribute: form, input, textarea, a, frame, iframe, button, select, map, meta, applet, object, param, img (if you know more reply...) Methods of addresing html elements:...
2
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when...
6
by: William F. Zachmann | last post by:
We've got a project going that involves moving an old web site with a massive dll written in C++ that produces most of the output from a SQL 7.0 data base on NT4 onto IIS on Windows 2003 Server...
4
by: Sathyaish | last post by:
I am no JavaScript progammer, and unfortunately am having to babysit an old code base that has a lot of JavaScript in it. I have two questions: (1) Can two HTML controls have the same name? It...
5
by: nick | last post by:
I need to create a simple asp.net application that use password protect some html pages. The html page provider doesn't know asp.net. And the host doesn't allow me to create user accounts. ...
7
by: thersitz | last post by:
I can't seem to get my html form to submit properly from within a web form. Here's my form tag syntax and some delivery hidden fields. <form id="myForm"...
19
Atli
by: Atli | last post by:
Introduction At some point, all web developers will need to collect data from their users. In a dynamic web page, everything revolves around the users input, so knowing how to ask for and collect...
1
by: since | last post by:
I figured I would post my solution to the following. Resizable column tables. Search and replace values in a table. (IE only) Scrollable tables. Sortable tables. It is based on a lot...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.