Help with a Form - Need it to be dynamic

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • siikworm
    New Member
    • Oct 2008
    • 3

    Help with a Form - Need it to be dynamic

    HI, I'm new to PHP/MySQL and having trouble with this. I'd like to select data from my database, use that data to populate a form, and then submit the form based on the user's selection.

    The problem I'm running into is that the form always submits the values of $studio_id and $name as they are when they exit the while loop, instead of using the values of what's been selected.

    How can I set this up so that the form action changes depending on the user's selection? Thanks in advance.

    Code:
    <?php
      error_reporting(E_ALL);
    
      $studio_id = $_GET['studio_id'];
      $name = $_GET['name'];
    
      if (isset($name)) {
        $includeForm = 1;
      }
    ?>
    
    <form method="post" action="add_class2.php?studio_id=<?php echo $studio_id; ?>&name=<?php echo $name; ?>">
    <select>
    
    <?php
    
      //connect to db
      include('config.php');
    
      // Query if the user has entered an explicit time
      $query = "SELECT *
                FROM studios
                ORDER BY name ASC";
    
      // Retrieve all the data from the example table
      $result = mysql_query($query) or die(mysql_error());
    
      // Create loop to fill the form
      while ($row = mysql_fetch_array($result)) {
        $studio_id = $row['studio_id'];
        $name = $row['name'];
        echo "<option value=\"add_class.php?studio_id=$studio_id&name=$name\">$name";
      }
    ?>
    
    </select>
    <input type="submit" value="Go" />
    </form>
    
    <?php
      if (isset($includeForm)) {
        //Display the class submission form
        include('class_form.php');
      } else {
        include('test_footer1.php');
      }
    ?>
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    You aren't really using the <form> element properly.

    The <form> element is mean to pass the user's selection via the various input elements (such as <input>, <textarea> and <select>).

    You don't put the values directly into the URL in the action attribute. You put then into <input> elements.

    For example, this form:
    Code:
    <form action="process.php" method="post">
      <select name="someID">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
      </select>
      <input type="submit" />
    </form>
    And this "process.ph p" file:
    [code=php]
    <?php
    header("Content-Type: text/plain");
    print_r($_POST) ;
    ?>
    [/code]
    Would output:
    [code=php]
    Array (
    [someID] => 2
    )
    [/code]
    ... if you selected "Two", that is.

    My point being; you use the <input> elements to set the values, not the URL string of the action attribute.

    I wrote an article about this a while back. It might help explain this.
    You can find it here.

    Comment

    Working...