dfowler Posted January 10, 2008 Share Posted January 10, 2008 Hey guys, here is my problem. I have two tables in a database (table A and table B). I'm trying to create a form where values from table A dynamically populate a select list (I can do this). Then depending on what they pick the rest of the form loads up based on values from table B. For example: The user selects "lunch" from the drop down list (populated by table A); then the rest of the form populates with options from table B that correspond to "lunch". I can do this easily by making the form 2 pages, but I am hoping to do it on one page (or at least look like one page). Thanks for any help! Quote Link to comment Share on other sites More sharing options...
nikefido Posted January 10, 2008 Share Posted January 10, 2008 Well Microsoft's Visual Studio would take care of that for you (i forgot about that handy thing with MS) unfortunately it becomes more complicated with PHP... What you need to do is use some JavaScript - in fact, this is a great usage for AJAX (actually, AJAX might be the only way to really do it - although you can do it synchronously instead of asynchronously) By "AJAX" (useless buzz word) i mean using the XMLHTTPRequest object in JS... I'm sure this isn't the answer you would have liked to hear - perhaps other people have better/other solutions. Quote Link to comment Share on other sites More sharing options...
revraz Posted January 10, 2008 Share Posted January 10, 2008 You can do it with PHP, but you need to Submit the form in order for it to happen. If you want it dynamic, use Java as mentioned. Quote Link to comment Share on other sites More sharing options...
dfowler Posted January 10, 2008 Author Share Posted January 10, 2008 I figured it would need some Javascript or something else. I'm pretty good with PHP and MySQL, but my Javascript ability is limited. How would I do this? Quote Link to comment Share on other sites More sharing options...
cunoodle2 Posted January 10, 2008 Share Posted January 10, 2008 Google is your friend on this one. I just did a search for ... "creating a dynamic html form with javascript" Here is an example of a dynamic form in live operation. You can simply view source to see how the coding works... http://developer.apple.com/internet/webcontent/examples/rental_form.html Read up...integrate into your site and then get back to us if you have any more questions. Good luck. I too am a wiz at php but my javascript is quite lacking. Quote Link to comment Share on other sites More sharing options...
dfowler Posted January 10, 2008 Author Share Posted January 10, 2008 That helps with switching with data, but what about loading? I'll get a little more in detail. Here are how the tables are set up Table A [/td] Table B IDID NameName DescriptionDescription ActiveA_ID Active Now let's say that the option I choose in the drop down has an ID of 2, then I want to only populate the rest of the form with data from Table B that has an A_ID of 2. Quote Link to comment Share on other sites More sharing options...
revraz Posted January 10, 2008 Share Posted January 10, 2008 This is done on the MySQL side SELECT tableb.* from tableb JOIN tablea ON (tableb.ID = tablea.ID) WHERE tabla.ID = $ID Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 10, 2008 Share Posted January 10, 2008 How many rows are in Table B? If there aren't a whole lot, you can load all of the data into a JSON object (dynamically created with PHP). Then pull the needed data from that using JavaScript. Quote Link to comment Share on other sites More sharing options...
dfowler Posted January 10, 2008 Author Share Posted January 10, 2008 That's the problem. I'm not going to know how many rows are in each table. Users are going to be able to add/delete rows from both tables at any time. So I'm hoping to have something automated so that no matter how many rows are in each table it will still work. Combining the tables on load I don't know how this will solve the problem of only displaying records depending on what option the user picks with the drop down list. Here is how I plan on populating the first list: <?php $query = "select * from tablea where active='1'"; $tablea = array(); $result=mysql_query($query); while (($row = mysql_fetch_assoc($result)) !== false) { $tabla[] = $row; } ?> <?php foreach($tabla as $a) { ?><option value="<?php echo $a['id']; ?>"><?php echo $a['name']; ?></option><?php } ?> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 10, 2008 Share Posted January 10, 2008 The reason I was asking, was because of memory allocation. If the table was going to have under 20 rows, it would make sense to load the data into a JSON object (this is basically an associative array for javascript) when the page loads, and get data from it when they select something from the list. If it's going to be more then that, you should load the values dynamically with AJAX. This is all under the assumption that you can't submit the page. If this is true, you should move this topic over to JavaScript. Quote Link to comment Share on other sites More sharing options...
dfowler Posted January 10, 2008 Author Share Posted January 10, 2008 This is all under the assumption that you can't submit the page. If this is true, you should move this topic over to JavaScript. Yeah, I just looked and see there is a Javascript and an AJAX board. I think after a couple of responses this would fit more into one of those. How do I move the thread? Quote Link to comment Share on other sites More sharing options...
revraz Posted January 10, 2008 Share Posted January 10, 2008 Ask a Mod or just start a new topic there and mark this solved. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted January 10, 2008 Share Posted January 10, 2008 Well first you need to get the db data into the JSON object so keep it here Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 10, 2008 Share Posted January 10, 2008 This is a function I use all the time to convert a PHP array into the code for a JSON object: <?php /** * $arr - The array to convert * $trim - True will trim whitespace off the values * $r - True will add whitespace to the output to make it readable * $l - Ignore this variable */ function php_json_encode ( $arr, $trim = false, $r = false, $l = 0 ) { $json_str = ""; if(is_array($arr)){ $pure_array = true; for($i=0;$i < count($arr);$i++){ if(!isset($arr[$i])){ $pure_array = false; break; } } if($pure_array){ $json_str = "[\n".(($r)?str_repeat(' ',$l + 1):""); $temp = array(); for($i=0;$i< count($arr);$i++) $temp[] = sprintf("%s", php_json_encode($arr[$i],$trim,$r,$l + 1)); $json_str .= implode(",\n".(($r)?str_repeat(' ',$l + 1):""),$temp); $json_str .= (($r)?"\n".str_repeat(' ',$l):"")."]"; }else{ $json_str = "{\n".(($r)?str_repeat(' ',$l + 1):""); $temp = array(); foreach($arr as $key => $value) $temp[] = sprintf("\"%s\":%s", $key, php_json_encode($value,$trim,$r,$l + 1)); $json_str .= implode(",\n".(($r)?str_repeat(' ',$l + 1):""),$temp); $json_str .= (($r)?"\n".str_repeat(' ',$l):"")."}"; } }elseif(is_int($arr)){ $json_str = $arr; }elseif($arr === true){ $json_str = 'true'; }elseif($arr === false){ $json_str = 'false'; }else{ mb_internal_encoding("UTF-8"); $convmap = array(0x80, 0xFFFF, 0, 0xFFFF); if($trim) $arr = trim($arr); $str = ""; for($i=mb_strlen($arr)-1;$i >= 0;$i--){ $mb_char = mb_substr($arr, $i, 1); $str = (mb_ereg("&#(\\d+);", mb_encode_numericentity($mb_char, $convmap, "UTF-8"), $match)) ? sprintf("\\u%04x", $match[1]) . $str : $mb_char . $str; } $str = addslashes($str); $str = str_replace("\n", '\n', $str); $str = str_replace("\r", '\r', $str); $str = str_replace("\t", '\t', $str); $str = str_replace("&", '\&', $str); $str = preg_replace('{(</)(script)}i', "$1'+'$2", $str); $json_str = "'{$str}'"; } return $json_str; } $my_arr = array( 'foo' => 'bar', 'my_num' => 123 ); print '<script type="text/javascript"> myJSON = '.php_json_encode($my_arr).';</script>'; ?> Hope this helps Quote Link to comment Share on other sites More sharing options...
emehrkay Posted January 10, 2008 Share Posted January 10, 2008 if you're using php 5.2+ json_encode will do it http://us.php.net/json_encode Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.