Client Side Things
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesClient Side Things

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old April 15th, 2003, 01:04 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
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--

Reply With Quote
  #2  
Old April 15th, 2003, 04:04 PM
ResNet9 ResNet9 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 101 ResNet9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to ResNet9
RE: auto-populating INPUT boxes on form

php Code:
Original - php Code
  1.  
  2. <?
  3. $company_name = $_POST["company_name"];
  4.  
  5. //connect to db
  6.  
  7. $query = "SELECT * from table WHERE company_name = "$company_name";";
  8.  
  9. $result = mysql_query($query);
  10. $row = mysql_fetch_object($result);
  11. //get all the vars ($var = $row->var;)
  12. ?>


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.

Reply With Quote
  #3  
Old April 16th, 2003, 01:20 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
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.

Reply With Quote
  #4  
Old April 16th, 2003, 01:40 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
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.

Reply With Quote
  #5  
Old April 16th, 2003, 02:14 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
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.

Reply With Quote
  #6  
Old April 16th, 2003, 02:22 PM
postalcow postalcow is offline
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Ford CIty, PA USA
Posts: 1,267 postalcow User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
Send a message via Yahoo to postalcow
RE: auto-populating INPUT boxes on form

If you want it to change values on the fly you need to use JavaScript



Reply With Quote
  #7  
Old April 16th, 2003, 03:17 PM
laroux laroux is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Austin, TX, USA
Posts: 1 laroux User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to laroux
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.

Reply With Quote
  #8  
Old April 16th, 2003, 03:43 PM
mobeamer mobeamer is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Charlotte, NC, USA
Posts: 10 mobeamer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #9  
Old April 16th, 2003, 05:13 PM
ResNet9 ResNet9 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 101 ResNet9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to ResNet9
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.

Reply With Quote
  #10  
Old April 16th, 2003, 07:24 PM
crisp crisp is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Holland
Posts: 336 crisp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: RE: auto-populating INPUT boxes on form

Quote:
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.

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.

Reply With Quote
  #11  
Old April 17th, 2003, 12:38 AM
ResNet9 ResNet9 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 101 ResNet9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to ResNet9
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.

Reply With Quote
  #12  
Old April 17th, 2003, 05:52 AM
crisp crisp is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Holland
Posts: 336 crisp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: RE: auto-populating INPUT boxes on form

Quote:
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.

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

Reply With Quote
  #13  
Old April 17th, 2003, 02:51 PM
ResNet9 ResNet9 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 101 ResNet9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to ResNet9
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.

Reply With Quote
  #14  
Old August 5th, 2003, 07:29 PM
cmcdonld cmcdonld is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 14 cmcdonld User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.





Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesClient Side Things > auto-populating INPUT boxes on form


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump