1

This is my code with a classic html dropdown with large list, and a button that executes the querry with the selected value:

<html>
<body>
<form name="lista" method="post" action="">
<select id="broj" name="name">                      
<option value="0">--Odaberi vrh--</option>
<option value="1">Maglić</option>
<option value="2">Vlasulja</option>
<option value="3">Veliki Oštrikovac</option>
...
<option value="73">Velika Lelija</option>
<option value="74">Bregoč</option>
<option value="75">Kozje strane</option>
<option value="76">Cincar</option>
</select>
<input type="submit" name="submit" value="SUBMIT" />
</form>
<?php
$peak = $_POST['name'];
    if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {
----querry with $peak
}
?>

This is solution from @jamesgarrett where the dropdown menu is populated from the Joomla database:

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('peak_name');
$query->from($db->qn('#__peaks'));
$db->setQuery($query);
$peaks_list = $db->loadColumn();

// you could just echo out as you go if you prefer, but instead here we build up the html as a single string.
$peaks_select  = '<select name="name" id="peaks">';
$peaks_select .= '<option value="">-- Select Peak --</option>';

// here we use the peak_name for the value, whereas usually you set the value to the id of the table row. we're assuming there won't be any double quotes in these names.
foreach($peaks_list as $p){
    $peaks_select .= '<option value="' . $p . '">' . $p . '</option>';  
}
$peaks_select .= '</select>';

echo $peaks_select;

But I never succeeded to create the other part of the code with the button which will execute querry.

2
  • Are you asking how to make an HTML select field and build select options using a loop? If not then you may need to clarify to what ‘the dropdown menu’ refers. Dec 29 '17 at 22:07
  • Yes, I ask how to make an HTML select field and build select options using a loop
    – Midhat
    Dec 29 '17 at 22:10
4
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('peak_name');
$query->from($db->qn('#__peaks'));
$db->setQuery($query);
$peaks_list = $db->loadColumn();

// you could just echo out as you go if you prefer, but instead here we build up the html as a single string.
$peaks_select  = '<select name="name" id="peaks">';
$peaks_select .= '<option value="">-- Select Peak --</option>';

// here we use the peak_name for the value, whereas usually you set the value to the id of the table row. we're assuming there won't be any double quotes in these names.
foreach($peaks_list as $p){
    $peaks_select .= '<option value="' . $p . '">' . $p . '</option>';  
}
$peaks_select .= '</select>';


?><html>
<body>
<form name="lista" method="post" action="">
<?php echo $peaks_select; ?>
<input type="submit" name="submit" value="SUBMIT" />
</form>
<?php
$peak = $_POST['name'];
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    // ----query with $peak
}
4
  • Just one more question. I need to make a variable $peak with the selected value. How? <form name="list" method="post" action=""> <?php $peak = $_POST['name']; ?> But not working
    – Midhat
    Dec 30 '17 at 10:46
  • A submitted field is accessible server side by having a name attribute. I have edited my answer with name="name". Dec 31 '17 at 0:27
  • I'm sorry, but my knowledge in this field is very limited. So far I have accessed the selected value with the POST method. How can I do it now? I did an update to my first question where I explained the situation. Thanks in advance
    – Midhat
    Dec 31 '17 at 20:22
  • I have added the form markup from your edited post around the select. Jan 1 '18 at 4:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.