0
<select name="shortcut">
<?php
$sql= "SELECT * FROM subject WHERE id_ped = $id_ped";
$vysledek0 = mysqli_query($con, $sql);
$count0 = mysqli_num_rows($vysledek0);
   for($i=0;$i<$count0;$i++){
$row= mysqli_fetch_row($vysledek0);
echo "<option value=\"shortcut\">" . $row[1] . "</option>";"<br>";
   }
?>
</select>

This gives me dropdown list where I can select data from certain table.

But I have trouble accessing the data later - for example like this:

<?php   $shortcut = $_POST['shortcut'];
                    echo $shortcut;
?>

It doesn´t take in the list item but instead it takes the string ´shortcut´.

How do I use the list items as variable from this point?

4
  • 1
    <option value=\"shortcut\">" you gave every option the value shortcut .... think about ,) Commented Dec 2, 2016 at 14:25
  • Is the <select> tag nested inside a <form>? Commented Dec 2, 2016 at 14:25
  • It´s form>table>select... So yes it is Commented Dec 2, 2016 at 14:28
  • stackoverflow.com/a/10009791/5588347 Commented Dec 2, 2016 at 14:32

2 Answers 2

1

You need to set the value of the option for it to be posted:

<select name="shortcut">
<?php
$sql= "SELECT * FROM subject WHERE id_ped = $id_ped";
$vysledek0 = mysqli_query($con, $sql);
$count0 = mysqli_num_rows($vysledek0);
   for($i=0;$i<$count0;$i++){
$row= mysqli_fetch_row($vysledek0);
echo "<option value='" . $row[1] . "'>" . $row[1] . "</option>";"<br>";
   }
?>
</select>

Then on the server side:

<?php   
    $shortcut = $_POST['shortcut'];
    echo $shortcut;
?>

However I personally would prefer writing the above code in the following style. Just in case if you like it:

<?php
    $sql= "SELECT * FROM subject WHERE id_ped = $id_ped";
    $vysledek0 = mysqli_query($con, $sql);
    $count0 = mysqli_num_rows($vysledek0);
    $items = array();

    for($i=0; $i<$count0; $i++) {
        $row = mysqli_fetch_row($vysledek0);
        array_push($items, $row[1]);
    }
?>

<select name="shortcut">
<?php foreach($items as $value): ?>
    <option value="<?php echo $value ?>"><?php echo $value ?></option>
    <br>
<?php endforeach ?>
</select>
Sign up to request clarification or add additional context in comments.

Comments

1

try to replace the \" with '. It should work that way

Comments

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.