Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database generated dropdown giving more variables a value from the same database when selected

I hope the title wasn't too cryptic, and I am sure there will be other threads like this one but cannot seem to put my finger on the correct terminology.

I have a form that has 3 dropdowns. Let's call them 'sales consultant', sales email & sales team.

What I am looking to do is give a value to 'sales email' & 'sales team' when the sales consultant is chosen from the dropdown 'sales consultant'

The catch, I am using mysql to pull the data for each dropdown. See below:

<div id="sc">
    Sales Consultant
    <input class="" type="text" list="salesconsultant" name="salesconsultant" placeholder="Start typing consultants name" required />
    <datalist id="salesconsultant">
        <?php
            $sql = "SELECT consultant, id FROM salesconsultants";

            $result = $conn->query($sql);

            if (!$result) die($conn->error);

            if($result->num_rows > 0 ) {
                while($row=$result->fetch_assoc()) {
                    echo "<option value=\"".$row["id"]."\">".$row["consultant </option>";
                }

                echo "</datalist>";
            } else {
                echo"record 0";
            }
        ?>
</div>

The other rows I have in the database are 'salesemail', 'id', 'consultant' and 'teamid'.

So when sales consultant 1 is selected it will give the variable $salesemail the value of consultant 1's 'salesemail' and so on.

Is this possible?

like image 658
Connor Finan Avatar asked Jan 28 '26 21:01

Connor Finan


1 Answers

Just change below line to :

echo '<option value="'.$row["id"].'".','."'.$row["salesemail"].'">'.$row["consultant"].'</option>'; 

In above code you are separating both value by , And you can get these values like this :

        $result = $_POST['yourselectbox'];
        $r1 = explode(',', $result);
        echo "id: ". $r1[0]."<br />";
        echo "email: ". $r1[1]."<br />";

Hope this will work!

like image 89
Swati Avatar answered Jan 30 '26 09:01

Swati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!