473,669 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Autofill fields in a form

12 New Member
Ok please forgive me, as I am really new to the whole PHP programming...

What I am attempting to do is on a form page create a drop down list that is populated from information in a database. For example a list of company names, then with this that would populate one more drop down list with the department they belong to if any.

Once both of those are chosen and the user hits submit it would populate text fields below. For example Name, company, phone number, email so on and so forth.

At this point the first drop down list box shows up but is empty. Here is the code I have for that:

Expand|Select|Wrap|Line Numbers
  1. <select name="comapny_list" size="1" id="company_list"> 
  2. <?php 
  3. include "config.php";
  4.  
  5. //connect to the mysql server
  6. $link = mysql_connect($server, $db_user, $db_pass) or die("Could not connect to MySQL");
  7.  
  8. //select the database
  9. mysql_select_db($database) or die ("Could not select database");
  10.  
  11. // Filling up the selection box
  12.  
  13. $query = mysql_query ("SELECT * FROM " . $table . " WHERE 'Active' = '1'", $link) or die("<center>There was an error and we could load the Client list</center>"); 
  14. $result = mysql_result($query); 
  15. while ($row = mysql_fetch_array($result)){ 
  16. $comp_name = $row2['company'];  
  17.  
  18. echo "<option value='$comp_name'>{$comp_name}</option>"; 
  19. ?> 
  20. <option value=""></option> 
  21. </select> 
Any suggestions would be much appreciated! Thanks!

*Edit here is the error form the error log sorry about that...
Wrong parameter count for mysql_result() in /htdocs/forms/orderform2.php on line 29
Expand|Select|Wrap|Line Numbers
  1. PHP Warning:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /htdocs/forms/orderform2.php
  2.  on line 30
  3.  
Nov 2 '07 #1
12 2660
Atli
5,058 Recognized Expert Expert
Hi Merlin. Welcome to TSDN!

Try printing the query before you execute it, just to see that it looks as you expect it to. It's so easy to make a typo in there somewhere and it helps to see it all combined.

It is always a good idea to check whether your mysql result is valid to avoid the kind of warning you posted above. This can be done like so:
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query(...);
  2. if($result) {
  3.   # Rest of the code.
  4. }
  5. else {
  6.   # Print error.
  7. }
  8.  
Note that your use of the mysql_result() function is not correct in your code. I would drop that and use the original result from the mysql_query() function.

I noticed that in your query you match a column, 'Active', against a number, '1'.
The field name itself should not be quoted, not unless you use back-ticks.

If the number is in fact the number 1, rather than the character 1, it should not be quoted either. I do suspect, however, that the 'Active' field is a CHAR(1) field, in which case your approach is the right one.

I'd recommend changing "SELECT * FROM ..." into "SELECT company FROM ..." because you are only using that one column. No need to fetch a bunch of columns you will never use. That only serves to slow down your server.

I'd also recommend changing from the mysql_fetch_arr ay() function to the mysql_fetch_ass oc() function to increase efficiency. The latter only creates an associative array, where the former creates a combination of both associative and indexed arrays.
Nov 2 '07 #2
Modern Merlin
12 New Member
I tried some of the things you suggested by starting with the printing the query to the screen. I also changed my query to company as per your suggestion.

What it returns is Resource id #4. This isnt anywhere in my database.

So currently all I am trying to do is select the company field and put it in a drop down list to select from, but I cant even get that to print to the screen.
Nov 3 '07 #3
Atli
5,058 Recognized Expert Expert
I tried some of the things you suggested by starting with the printing the query to the screen. I also changed my query to company as per your suggestion.

What it returns is Resource id #4. This isnt anywhere in my database.

So currently all I am trying to do is select the company field and put it in a drop down list to select from, but I cant even get that to print to the screen.
What you are printing is the result ID you get from your MySQL server. What I meant is, print the actual query and the error. Something like:
Expand|Select|Wrap|Line Numbers
  1. # Create and execute query
  2. $sql = "SELECT col from tbl"
  3. $result = mysql_query($sql);
  4.  
  5. # Print query for debugging purposes
  6. echo "<pre>$sql</pre>";
  7.  
  8. # Check result
  9. if($result) {
  10.   # Print all rows
  11.   $rowno = 0;
  12.   while($row = mysql_fetch_assoc($result) {
  13.     $rowno++;
  14.     echo "Row $rowno:<br />"
  15.     foreach($row as $_k => $_v) {
  16.       echo " - $_k = $_v<br />";
  17.     }
  18.   }
  19. else {
  20.   # Print error
  21.   echo "<b>Query failed:</b> ". mysql_error();
  22. }
  23. echo "</pre>";
  24.  
Nov 3 '07 #4
Modern Merlin
12 New Member
I feel like an idiot LOL

I tried what you have there and I get a parse error....

Here is the code:

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. include "config.php";
  3.  
  4. //connect to the mysql server
  5. $link = mysql_connect($server, $db_user, $db_pass) or die("Could not connect to MySQL");
  6.  
  7. //select the database
  8. mysql_select_db($database) or die ("Could not select database");
  9.  
  10. // Selecting form the database
  11.  
  12. $query = mysql_query ("SELECT Company FROM Order_Form" ) or die("<center>There was an error and we could not load the Client list</center>"); 
  13.  
  14. //echo $query;
  15. $result = mysql_query ($query, $link);
  16.  
  17. echo "<pre>$query</pre>";
  18.  
  19. # Check result
  20. if($result) {
  21.   # Print all rows
  22.   $rowno = 0;
  23.   while($row = mysql_fetch_assoc($result)  {
  24.     $rowno++;
  25.     echo "Row $rowno:<br />"
  26.     foreach($row as $_k => $_v) {
  27.       echo " - $_k = $_v<br />";
  28.     }
  29.   }
  30. else {
  31.   # Print error
  32.   echo "<b>Query failed:</b> ". mysql_error();
  33. }
  34. echo "</pre>";
PHP Parse error: parse error, unexpected '{' in /htdocs/forms/orderform2.php on line 23

I changed the the line number to the correct one it is given me an error on. I am going over some of the lessons I took to see if I can figure it out, but any help would be much appreciated...

Modern Merlin
Nov 3 '07 #5
post
17 New Member
[php]
<?php

include "config.php ";

//connect to the mysql server
$link = mysql_connect($ server, $db_user, $db_pass) or die("Could not connect to MySQL");

//select the database
mysql_select_db ($database) or die ("Could not select database");

// Selecting form the database
$query = mysql_query ("SELECT Company FROM Order_Form" ) or die("<center>Th ere was an error and we could not load the Client list</center>");
// Put results in array
$result = mysql_fetch_arr ay($query);

if ($result) {
$rowno = 0;

echo("<select name=\"comapny_ list\" size=\"1\" id=\"company_li st\">");

while ($row = mysql_fetch_ass oc($query)) {

$rowno++;

foreach($row as $_k=>$_v) {

echo("<option value=\"" .$_v. "\">" .$_v. "");

}

}
echo("</select>");
}
else {
# Print error
echo "<b>Query failed:</b> ". mysql_error();
}
?>
[/php]

You forgot to add a ) at line 27.

But I fixed your code to do what I think you were asking take a look at it.
Nov 3 '07 #6
Modern Merlin
12 New Member
WOW that worked perfectly! THANK YOU!!!!

Now my question is, is there a way to select company as a field in one table if a field in that same table is active or in active? I know in MySQL I can select * from the table that are active by doing the statement: SELECT * FROM `Order_Form` WHERE `Active` = '1' This will pull up all active records.

What I am ultimately trying to do is to fill one drop down box with all the companies that are active and within that if the same company but different department places an order I need to pull up their record information. So I need a second drop down box to change the the dept if there is one. Then the text fields below will be filled in as per both of those selections. Then the rest of the form is filled up and written back to the database (which that part I have qworking fine.

Im thinking at this time I may need to create more than one table.. :(

Still working on this myself to see if I can figure it out but any nudge in the right direction would be awesome!

Modern Merlin
Nov 5 '07 #7
pbmods
5,821 Recognized Expert Expert
Heya, Merlin.

So what you're saying is that you have a table of companies and a table of departments, and you want the values in the second drop down to be all departments for whatever company is selected in the first drop down. Is that right?
Nov 5 '07 #8
Modern Merlin
12 New Member
No I have one table.. With fields..

Name
Company
Dept
Phone
Then some choices made by checkboxes

Which the customer will fill out. Then the sales associate will pull up another page that will have a drop down box for companies (which now populates) and some companies will have different departments that order from them. So I will also need a dept drop down the populates as per the company chosen. Once that is done the text boxes below with name company dept, phone plus the checkboxes already marked by the customer will populate as per the company and dept fields above and then the sales associate will be able to choose more check boxes and text fields and that will write back to the database (which I need to over-write what is already in there so that it doesnt create another row in the table but replaces whats already in there).

I already have the part for writing back to the datbase figured out for the customer portion so the table will populate. I can now pull the company names into the drop down box and if I want to list ALL the departments I can list those too. Whichever way I do it, the text fields and and the check boxes have to populate by the company name and dept chosen. If there isnt a dept then the dept field is listed as none, which Im not so sure is a good idea.

Should I have a seperate table to house the departments? I have no problems starting this over because as I explained in the beginning I am new to this and havent really touched any programming in almost 4+ years LOL. Believe me I have no problem admitting when I need help :)
Nov 5 '07 #9
pbmods
5,821 Recognized Expert Expert
Heya, Merlin.

How does your script know that there are multiple departments for a given company? Does it pull this info from a separate table, or does it search for all departments in the one table that match the selected company name?
Nov 6 '07 #10

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

Similar topics

0
3471
by: Chris Sharman | last post by:
I'd like to design my pages to work cooperatively with browser autofill features. I've loked around, but can't find any good documentation on supported/unsupported field names (http://toolbar.google.com/autofill_help.html gives some details, but doesn't make it clear to me how to name my form fields; rfc3106 only seems to cover ecommerce specific data). I'd like a delivery name & address (primary); a contact name & address (if...
1
18526
by: shortbackandsides.no | last post by:
I'm having a lot of difficulty trying to persuade the Google toolbar autofill to act consistently, for example ======================= <html><head> <title>autofill test</title> </head><body> <form method="POST" action=""> 1 email <input type="text" name="email"><br> 2 name <input type="text" name="name"> <br>
0
4257
by: David Portabella | last post by:
Hello, I am a doing a survey about Autofill Web Forms softwares. Usually, they all collect information of the user once during the set-up phase (user knowledge base). Then, when the user navigates to a web form, the autofill software can automatically partially fill the form. The solution can be mostly differenciated by its intelligence algorithm for matching between the
0
2298
by: Ray Holtz | last post by:
Is it possible to autofill a field based on what is entered into another field in a form? My form has an employee field, and department field. In an Items Table, I have fields FldEmployee, and FldDepartment. FldEmployee is looked up from an TblEmployee Table which also has fields FldEmployee and FldDepartment. This FldDepartment field is looked up from a TblDepartment table that just has FldDepartments. I hope I explained that OK The...
4
15843
by: HTS | last post by:
I have written a membership database application (PHP + mySQL) and need to prevent autofill from filling in fields in a member record edit form. I know I can turn off autoFill in my browsers, I want a away to prevent it within the form itself. Is there any standard way to "tell" a browser not to autofill the fields on a particular form?
6
9369
by: bloukopkoggelmander | last post by:
All I am new to this site and hopefully would have a good time here. I am new to Microsoft Access and VBA and am currently involved in a project to create a new database for one of our company departments. The problem I am having is as follows( ...I have spent about 2 weeks on this now and is about 5 seconds away from going insane....) : I have created a new form with a combo box that looks up Last Name and First Name from a employee...
3
1395
by: StuckAndNoClue | last post by:
I've searched through some of the answers on the forum regarding autofill but I can't work out how to do it for my database. I have two tables 1. Members info, containing the following fields: a. Member ID (primary) b. Division that the member belongs in c. Address d. Names 2. Division Night info containing the following fields:
8
2364
by: tess | last post by:
I have: table 1 - tblLeadInfo which includes a salesman ID field table 2 - tbllkpSalesman with all zips in the state and a Salesman assigned to that area. I have a form based on table #1 When I enter the zip code in the form, I'd like the Salesman ID field in tblLeadInfo to fill in accordingly and also show the salesman name on the form. I'm sure this is simple, I just can't seem to get it straight in my
13
6220
by: BASSPU03 | last post by:
Hello, folks. This is my first post and I only began to work extensively with Access about 3 weeks ago. I'm running Access 2003 on Windows XP. I'd like a textbox in subform2 to reflect the value chosen from a combobox in subform1. FYI: Both of these subforms are linked to a main form with five fields (AutoNumber, Quantity, Resource, Type, and General Description; "AutoNumber" is the primary key). My main form lists a resource (e.g....
1
3149
by: Nik Coughlin | last post by:
I'm doing some ajax validation on form fields, using the form change event. If I click Autofill in the Google Toolbar, fields are filled out but the change event is never fired, so the validation doesn't update. I could user a timer but that just smells wrong to me. Any other way to reliably tell if a form has changed or even if I could detect that autofill has been used somehow
0
8462
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8382
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7405
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1787
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.