0

can someone tell me how to insert the selected text not the value of a dropdown list (select box) into a mysql database when user hits the button enroll. The code right now inserts the id of the row i am rendering but I need to insert the value. Sorry if I am not that explanatory.. Much help is appreciated. !!!

Updated: Can Someone tell me how to redirect after succesfulling inserting to a confirmation page ?????

$db = &JFactory::getDBO();

$query = "
INSERT INTO
`jos_jquarks_persontraining`
(

course_name,

courseDate,

user_id,
employeeNumber,
department,
name,
timeStamp
)
VALUES
(

'{$courseTitle}',
'{$varcourseDate}',

'{$id}',
'{$username}',
'{$department}',
'{$name}',
'{$acknowledge}',
'{$vardate}'
)";

$db->setQuery($query);

$db->query();

if($db->getErrorNum()) { 
JError::raiseError( 500, $db->stderr()); 
} 

}

?>

 <form name="quiz_info" method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">

<?php

echo JText::_('Please select the date:');

$database= &JFactory::getDBO();

$database->setQuery('SELECT training_id,CONCAT(trainingDate,"-",trainingHour) AS trainingDate FROM training WHERE openSeats > 0');


$result = $database->loadObjectList();



echo '<select name="dateSelectBox">';
foreach ($result as $row) {

echo '<option value="'.$row->training_id.'">'.$row->trainingDate.'</option>';
                        }
echo '</select>';

3 Answers 3

1

You have two ways to do that, the first one requires you to change the VALUE of your OPTIONs. But that is usually not the right way to go. What i'd do is simply reload the list of values that you can print, loop it until you find the right one that matches value for id. Then, take that LABEL that you want to insert and save it to database by any means possible.

In Joomla anyway, you should always load your data from the view and assign it to your template. Since the tutorials and best pratice in joomla also tells you that you should cache your retrived data from your model, you could simply use your controller to preload the data in the model, use it to find your "label" to insert into the database and then go on to your view again for result...

Sign up to request clarification or add additional context in comments.

Comments

0

Just use

echo '<option value="'.$row->trainingDate.'">'.$row->trainingDate.'</option>';

on the third last line, but the whole method looks quite fishy to me.

  1. You should really use ID's of the training dates instead of the dates themselves, it's better design
  2. You are not reducing the number of open seats in the code. Anyway, try designing your DB to include "capacity" column, you can then check for open seats in more consistent way.

3 Comments

Cool stuff, yes I am trying to create a trigger to reduce the seats in the mysql table, but for this other table I am building I would like to insert both the id of the training, and the date for reporting purposes
Can you please help , I need to redirect after succesfully inserting
Redirecting is described for exemple here: plus2net.com/php_tutorial/php_redirect.php
0

I think your best bet is to have a table in your database that has an id to value and then when trying to figure out what the id means, do a join to that table.

When you submit a form you can do $_POST and that will show you everything that was sent from the form. If you want to go the extra step you will need to create hidden fields in the form that will hold the value, something along the lines of when the users submits you will need to match that option id to the hidden id.

3 Comments

yes that seems to be the best idea, but I would think there is a way to refer the value of a select box using post: echo '<option value="'.$row->training_id.'" text="'.$row->trainingDate.'">'.$row->trainingDate.'</option>';
When you submit a form you can do $_POST and that will show you everything that was sent from the form. If you want to go the extra step you will need to create hidden fields in the form that will hold the value, something along the lines of <input type='hidden' name='id' value='whatever value you want'> when the users submits you will need to match that option id to the hidden id.
See my updated question, do you know how can I redirect to an specific page once the insertion went trough?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.