|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
auto-populating INPUT boxes on form
Hi, I have a PHP script that is basically a simple HTML form that submits contact information to a MySQL contact database table, once a user has typed it in the INPUT boxes.
Since I have a company table that already has general address and phone information, I would like to use the selected value from the company SELECT box (which already has an array from the company table with all the company names) to query the company table based on the company name and put all the company address, phone, city, state, yada yada information into the INPUT boxes on the form, prior to submission to the contact table. Can this be done? If so, how? --Thread Moved Postalcow-- |
|
#2
|
|||||
|
|||||
|
RE: auto-populating INPUT boxes on form
php Code:
All that you have to do is name the input fields the same var as what you are pulling from the db. Then when the company is selected, and the page is reposted to itself, it will load the info. Hope that is what you meant. |
|
#3
|
|||
|
|||
|
RE: auto-populating INPUT boxes on form
This is kind of what I was trying yesterday...but there doesn't seem to be anything (like a javascript onchange script for the company select box) available to resend the query for the phone number, address, etc., once a company name has been selected from the dropdown box.
Remember, I want to populate the form fields using data from a different table prior to submitting an update query to my contacts table. Any other suggestions? Thanks all. |
|
#4
|
|||
|
|||
|
RE: auto-populating INPUT boxes on form
Oops, I didn't see the bottom of your reply... I guess the "POST" is the event I'm looking for. I'll try this again and see if I can get it to work. Thanks.
|
|
#5
|
|||
|
|||
|
RE: auto-populating INPUT boxes on form
OK, I was trying to avoid this...
It's still not working, doesn't seem to be "refreshing" form data after I select the company name. Here is the code: <FORM METHOD="POST" NAME="addcontactform" ACTION="addcontact.php"> <TABLE cellpadding=0 cellspacing=1 align="left" colspan="3"> <TR align="right"> <TD>CONTACT: <INPUT TYPE="text" SIZE=40 NAME="CONTACT" ></TD> <TD>COMPANY: <SELECT SIZE=1 STYLE="width: 263px" NAME="COMPANY"> <? $dbname = "somedb"; $connection = mysql_connect("localhost", "somename", "somepass") or die ("Couldn't Connect."); $db = mysql_select_db($dbname, $connection); $sql = "SELECT company_id, company_name, company_division, company_address, company_address2, company_phone, company_fax, company_city, company_state, company_zip, company_country, company_website FROM tblCompanies ORDER BY Company_Name"; $result = mysql_query($sql, $connection) or die ("Couldn't Execute Query"); while ($row = mysql_fetch_array($result)) { $id = $row['Company_ID']; $company = $row['company_name']; $division = $row['company_division']; $address = $row['company_address']; $address2 = $row['company_address2']; $phone = $row['company_phone']; $fax = $row['company_fax']; $city = $row['company_city']; $state = $row['company_state']; $zip = $row['company_zip']; $country = $row['company_country']; $website = $row['company_website']; $option_block .="<option value="$company">$company</option>"; } ECHO $option_block; ?> </SELECT> </TD></TR> <? $company_name=$_POST["COMPANY"]; $dbname = "somedb"; $connection = mysql_connect("localhost", "somename", "somepass") or die ("Couldn't Connect."); $db = mysql_select_db($dbname, $connection); $sql = "SELECT company_id, company_name, company_division, company_address, company_address2, company_phone, company_fax, company_city, company_state, company_zip, company_country, company_website FROM tblCompanies WHERE company_name="$company_name""; $result = mysql_query($sql, $connection) or die ("Couldn't Execute Query"); while ($row = mysql_fetch_array($result)) { $id = $row['Company_ID']; $company = $row['company_name']; $division = $row['company_division']; $address = $row['company_address']; $address2 = $row['company_address2']; $phone = $row['company_phone']; $fax = $row['company_fax']; $city = $row['company_city']; $state = $row['company_state']; $zip = $row['company_zip']; $country = $row['company_country']; $website = $row['company_website']; } ?> <TR align="right"> <TD>PHONE:<INPUT TYPE="text" SIZE=40 NAME="PHONE" VALUE="<?echo $phone;?>"> </TD> Is this what you meant? If not, please clarify. |
|
#6
|
|||
|
|||
|
RE: auto-populating INPUT boxes on form
If you want it to change values on the fly you need to use JavaScript
|
|
#7
|
|||
|
|||
|
RE: auto-populating INPUT boxes on form
Thanks. That's what I was beginning to think. Do you have any suggestions about how to do this or what the script should look like? I don't know javascript.
|
|
#8
|
|||
|
|||
|
RE: auto-populating INPUT boxes on form
Check out javascript.com (Do a search for pull down list)
Your probley going to have to make PHP type a bunch of javascript code for you. MB |
|
#9
|
|||
|
|||
|
RE: auto-populating INPUT boxes on form
If you want to change it on the fly like postalcow said you have to use js. I try not to use js unless I'm absolute forced too. I'll always refresh the page in that situation. You can have seperate buttons for updating the page and submitting. I'm probably biased because I hate js so much though.
|
|
#10
|
|||
|
|||
|
RE: RE: auto-populating INPUT boxes on form
Quote:
And why is that? (the hatred I mean) I simply love javascript because it's fast, reliable and with the current DOM object model most things work perfectely in various browsers. However, in this case if you want to use javascript you probably have to sent a whole bunch of code to the browser first. This doesn't seem like the best solution to me. An alternative would be to request the data using xmlHTTP requests to the server from javascript getting the necessary data on the fly, but this is somehow complicated and can only be used in modern browsers (like IE5+ and recent Mozilla/Netscape browsers). You're bound to run into security issues with some browsers as well. Best solution still would be to do a post-back to the server and rewriting the form filling the other fields based upon the selection. You could use javascript to perform the post-back by using the onchange event handler on the select. |
|
#11
|
|||
|
|||
|
RE: auto-populating INPUT boxes on form
My hatred is probably not justified because to tell you truth, because I haven't used it very much. Although I'm sure its rare, there are ways to get around js. If something fails with the js and you don't have error checking in your php code, then functionality is lost. Like I said though, its probably just that I don't like js, but it is nice to have the php backbone behind the script. I don't think many people are going to be with me on this one though.
|
|
#12
|
|||
|
|||
|
RE: RE: auto-populating INPUT boxes on form
Quote:
I'm with you sofar that the functionality of a webpage should not solely rely on javascript. If you do error checking in javascript you should still do it again in PHP. The advantage of doing it in javascript is the quick response to the user (since you do not need the post-back), but javascript can easily be circumvented. It can give your site a little extra for users who have it turned on. I think the combination of a serverside language such as PHP mixed with the possibilities of a client side scripting language maxes a very powerfull combination |
|
#13
|
|||
|
|||
|
RE: auto-populating INPUT boxes on form
Very good point.
I probably should get more into js. So many of my co-workers have turned me off to it because they don't use it the right way. We can't seem to get stuff to validate either, sec 508 and such. All the stuff I do has to work for screen readers (even apps). A javascript front side could make things easier for the user. |
|
#14
|
|||
|
|||
|
RE: auto-populating INPUT boxes on form
I have a question about this response below (taken from early on in this thread).
This is the suggestion:---------------------------------------------------------------------- <? $company_name = $_POST["company_name"]; //connect to db $query = "SELECT * from table WHERE company_name = "$company_name";"; $result = mysql_query($query); $row = mysql_fetch_object($result); //get all the vars ($var = $row->var;) ?> All that you have to do is name the input fields the same var as what you are pulling from the db. Then when the company is selected, and the page is reposted to itself, it will load the info. Hope that is what you meant. ------------------------------------------------------------- I am new to PHP and DB integration. Can I pull data from a table within a DB and insert the pulled data along with new data into a different table within that same DB? It seems I read someplace that you should never write to a DB that you are pulling info from, but I wanted to dould check this with you guys for your input. |
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Client Side Things > auto-populating INPUT boxes on form |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|