Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/85404-solved-dynamic-form-help/
Share on other sites

Well Microsoft's Visual Studio would take care of that for you :D (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.

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.

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.

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 } ?>

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.

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?

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.