Jump to content

PHP coding help? (using list menu selection to populate a text area)


jgrant2012

Recommended Posts

hi everyone. i was wondering if i could get some help.

 

basically im doing a website based on exercising. so the user will select from the drop downlist an exercise i.e. Chest press. this is dynamically populated by a database. From this selection i would like to then dynamically populate some sort of text area with the description of the exercise. I used php coding within the drop down list menu to take the exercise name (chest press) out of the database and into the list menu to be selected. I would like to know how to take the description of the exercise out of the database Dependent on the exercise choice within the list menu. i.e. if the user chooses Chest press, the description for chest press will be taken from the database and displayed on the page. if a user selects another exercise within the list menu, its description will be taken from the database and so on.

Can anyone help me with this, i am really struggling  :( :'(

 

example of the database

 

id

ex_identify (something to identify the exercise) i.e chest

ex_name (name of the exercise) i.e chest press

ex_description (description of the exercise)

 

heres the code of how i populated the list menu, if ths helps

 

<form id="form1" name="form1" method="post" action="">
      <label>
        <select name="select" class="oneColElsCtrHdr" id="chestExercises">
        <option id="0">---Select Chest Exercise---</option>
        <?php
        require("Getfitconfig.php");
        $getallchest = mysql_query("SELECT ex_name FROM getfit_exercises2 WHERE ex_identify = 'chest'");
        while($viewallchest = mysql_fetch_array($getallchest)){
        ?>
        <option id="<?php echo $viewallchest['id']; ?>"><?php echo $viewallchest['ex_name']; ?></option>
        <?php } ?>
      </select>
  </label>

Link to comment
Share on other sites

Your <option> tags in the form need to be value="" not id=""

 

This will get you the description on the receiving end:

$getDescription =  mysql_query("SELECT ex_description FROM getfit_exercises2 WHERE id = '" . $_POST['select'] . "'");
$row = mysql_fetch_array($getDescription);
echo $row['ex_description'];

Link to comment
Share on other sites

What I think you are envisioning really is AJAX, not php.  AJAX allows you to dynamically change the page based on user interaction by passing data to a backend server (usually php), getting data in response, and then updating an element of the webpage with the returned data (or changing it based on the response, such as logging in, etc).

 

What you're trying to do with php can be done, but it will require multiple steps.  Without AJAX, you'll have to submit the form data each step in order to change the content of other elements in the form.

 

I''ll create an example in the next post, but I'm going to click submit so you know I'm working on this ATM

Link to comment
Share on other sites

The workflow process will be:

Create a form with <select> elements with <options> for each exercise.  The ACTION on that form will go to a php page which will grab the value of the selected OPTION when it was submitted. (Note:  You CAN have the form submit to itself, e.g., the same php page.  In this case, you start by looking for the _POST or _GET variables being set and processing them from the get-go, setting variables as appropriate throughout the rest of the page.  This limits your total number of php pages in your site, but can get VERY messy without careful preplanning. )

 

That php page takes the value submitted by the form and runs the appropriate query, something like (a generic example)

$submittedVariable = $_POST['exercise']; //don't forget to sanitize POST and GET, even though I leave that out for brevity here
$query = "SELECT description FROM excercises WHERE exercise = '".$submittedVariable."' LIMIT 1;
$result = mysql_query($query); //it's safer to use PDO, but that's another discussion entirely.  This is the quick easy way to make the query
if(!$result)
{
     //use this for debugging purposes only--never reveal to users what your database structure is like.  After you get it working properly replace this message with something more generic but specific enough that you know the error occurs in this spot, not at another location in your code     
     echo "<p>Error retrieving the description.  The query used was ".$query."<br/>The returned error was: ".mysql_errno().": ".mysql_error()."</p>";
}
/* now process the returned query and grab the description, settting a variable with the description value.  To make this quick and easy I use a function which I have in an include file:
function db_result_to_array($result)
{
   $res_array = array();

   for ($count=0; $row = @mysql_fetch_array($result); $count++)
     $res_array[$count] = $row;

   return $res_array;
}
This function allows me to reference the specific MySQL column by column name when I process it:
*/
$result = db_result_to_aray($result);
foreach($result as $row)
{
     $desription = $row['description'];
}

/* Now, at this point you COULD give an AJAX feel to your page by setting the description in the SESSION array so the original page can access it: */
$_SESSION['description'] = $description;

/* and then, if everything went smoothly, we haven't had any output to the screen, so that allows us to just redirect back to the original page. */
$referring_page = 'Location: http://'.$_SERVER['HTTP_REFERER']; //you may just want to hardcode in the page that has the original form because HTTP_REFERER is set by the browser and can't always be trusted
header($referring_page);

 

Now back on the page with the select form, at the beginning, you just look to see if $_SESSION['description'] is set.  If it is, then use that text in the appropriate location in the page and then

unset($_SESSION['description']);

after you assign it to a variable.  That way you're keeping it inside the

if(isset($_SESSION['description']))

block.  Once it's assigned to a variable you can use the var anywhere on the page you need to, and although you're using 2 separate PHP pages, the page just seems to refresh itself when the initial select form is submitted, showing the results and giving your page an AJAX feel without really using AJAX.

 

I hope you can follow that...I know I ramble a bit in long explanations  (and there may be a typo or two in the code, so use judgement on missing ) or ;'s etc The keyboard on my laptop isn't as responsive as it used to be and sometimes my fingers go faster than it wants to process :)

Link to comment
Share on other sites

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.