-1

I have this PHP code that is displaying multiple radio buttons:

<?php
foreach ($_SESSION["resellers"] as $reseller) {
    $sql2="SELECT * from reseller where sequence = '".$reseller."' ";
    $rs2=mysql_query($sql2,$conn) or die(mysql_error());
    $result2=mysql_fetch_array($rs2);
    echo '<input type="radio" name="reseller" value="'.$reseller.'" /> '.$result2["company"].'<br>';
}
?>

i want to run another SQL query a little lower down the page based on the checked radio button:

<select name="reseller_customer">
<option value="">Please Choose</option>
<?php
$sql2="SELECT * from customer where resellerid = '".$reseller."' ";
$rs2=mysql_query($sql2,$conn) or die(mysql_error());
$result2=mysql_fetch_array($rs2);
echo '<option value="'.$reseller.'">'.$result2["company"].'</option>';
?>
</select>

how can i do the where resellerid = "CHECKED RADIO BUTTON"

user2710234
  • 3,177
  • 13
  • 36
  • 54

3 Answers3

1

For this you should use some ajax call or something, since php has already been executed when the user selected the radio button.

To get the selected radio button you can use jquery:

<script>
    var id = $("#myform input[type='radio']:checked").val();
</sciprt>

Then, you can send an ajax request with $id to execute the desired sql.

You can also check this: How can I know which radio button is selected via jQuery?

And the ajax request should look like:

function myAjaxRequest(id) {
    $.ajax({
        url: 'http://path/to/file/with/the/sql/function',
        type: 'POST',
        data: {id: id},
        success: function (response) {
            alert ("Everything went fine!)";
        }
    });
}

In the file with the Sql function, you can get the selected id with:

$id = $_POST["id"];
Community
  • 1
  • 1
0

Since "CHECKED RADIO BUTTON" is not value itself, the same query you previously did should do what you need. When a radio button is checked, only the value of the checked radio button is passed to the database.

user3186527
  • 103
  • 1
  • 5
0
    <script>
        $(document).ready(function() {
            $("input[name='reseller']").change(function() {
                var resellerid = $(this).val();

                $.ajax({
                    type: "POST",
                    url: "company.php",
                    data: {resellerid: resellerid}
                })
                        .done(function(company) {
                            $("select[name='reseller_customer'] option[value='']").next().remove();
                            $("select[name='reseller_customer']").append("<option value='" + resellerid + "'>" + company + "</option>");
                        });
            });
        });
    </script>