By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

Help with a Form - Need it to be dynamic

P: 3
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.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   error_reporting(E_ALL);
  3.  
  4.   $studio_id = $_GET['studio_id'];
  5.   $name = $_GET['name'];
  6.  
  7.   if (isset($name)) {
  8.     $includeForm = 1;
  9.   }
  10. ?>
  11.  
  12. <form method="post" action="add_class2.php?studio_id=<?php echo $studio_id; ?>&name=<?php echo $name; ?>">
  13. <select>
  14.  
  15. <?php
  16.  
  17.   //connect to db
  18.   include('config.php');
  19.  
  20.   // Query if the user has entered an explicit time
  21.   $query = "SELECT *
  22.             FROM studios
  23.             ORDER BY name ASC";
  24.  
  25.   // Retrieve all the data from the example table
  26.   $result = mysql_query($query) or die(mysql_error());
  27.  
  28.   // Create loop to fill the form
  29.   while ($row = mysql_fetch_array($result)) {
  30.     $studio_id = $row['studio_id'];
  31.     $name = $row['name'];
  32.     echo "<option value=\"add_class.php?studio_id=$studio_id&name=$name\">$name";
  33.   }
  34. ?>
  35.  
  36. </select>
  37. <input type="submit" value="Go" />
  38. </form>
  39.  
  40. <?php
  41.   if (isset($includeForm)) {
  42.     //Display the class submission form
  43.     include('class_form.php');
  44.   } else {
  45.     include('test_footer1.php');
  46.   }
  47. ?>
Oct 31 '08 #1
Share this Question
Share on Google+
1 Reply


Atli
Expert 5K+
P: 5,058
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:
Expand|Select|Wrap|Line Numbers
  1. <form action="process.php" method="post">
  2.   <select name="someID">
  3.     <option value="1">One</option>
  4.     <option value="2">Two</option>
  5.     <option value="3">Three</option>
  6.   </select>
  7.   <input type="submit" />
  8. </form>
  9.  
And this "process.php" file:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. header("Content-Type: text/plain");
  3. print_r($_POST);
  4. ?>
  5.  
Would output:
Expand|Select|Wrap|Line Numbers
  1. Array (
  2.   [someID] => 2
  3. )
  4.  
... 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.
Oct 31 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.