0

i have 2 radio buttons male and female when i select male i want the list to be loaded with male names from the database also with female the male and female tables have 2 columns {id} and {name}

<?php 
include "config.php";//database conection
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<script type="text/javascript" src="jquery.min.js"></script>
<div>sexe </div>
<form id="myform">
<input type="radio" id="male" name="gender" value="male" checked="checked">
<label for="male">Male</label><br>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label><br>
</form>

<div>nom</div>

<select id="sel_name">

</select>
<script type="text/javascript">
    $(document).ready(function () {
        $('#myform').change(function () {
            var selected =$("input[name=gender]");           
            $.ajax({
                url:'getnames.php',
                type:'post',
                data:{det:selected},
                dataType:'json',
                success: function (response) {
                   var len = response.length;
                   $('#sel_name').empty();
                   for (var i=0;i<len;i++){
                       var id = response[i]['id'];
                       var name = response[i]['name'];
                       $("#sel_name").append("<option value='"+id+"'>"+name+"</option>");
                   }
                }
            })
        })
    })
</script>
</body>
</html>

and here is the code to get dropdown elements the problem is the dropdown list is still empty i still have a problem with the sql query i get Fatal error: Uncaught Error: Call to a member function execute() on boolean

<?php
include "config.php";

if(isset($_POST["gender"]))
{
$choice = $_POST["gender"];
if ($choice == "male") {
   $table = "male";
}
elseif ($choice =="female") {
   $table = "female";
}
}

$sqli = $conn->prepare("SELECT name FROM ".$table);
$sqli->execute();
$result = $sqli->get_result();

   while ($row = $result->fetch_assoc())
   {
    $id = $row['id'];
    $name = $row['name'];
    
    $persons_array[] =array("id"=>$id,"name"=>$name);
   }
echo json_encode($persons_array);

?>

Kousayla
  • 53
  • 8
  • Note you are missing `#` in `$('sel_name').empty();` – Carsten Løvbo Andersen Sep 08 '20 at 10:54
  • @CarstenLøvboAndersen still the main problem in the sql query – Kousayla Sep 08 '20 at 10:57
  • 1
    Does this answer your question? [Call to a member function execute() on boolean in](https://stackoverflow.com/questions/35132451/call-to-a-member-function-execute-on-boolean-in) – M. Eriksson Sep 08 '20 at 10:58
  • @MagnusEriksson no the parameter here is the table name i tried to add sqli->bind_param and still the – Kousayla Sep 08 '20 at 11:10
  • Now I see what happens. When you're making the Ajax request, you're only passing `{det:selected}` but in your PHP, you're trying to use `$_POST['gender']` instead of `$_POST['det']`. That means that the variable `$table` never gets set and your query ends up as `SELECT name FROM` without the table name since you always execute the query regardless if the posted param exists or not. – M. Eriksson Sep 08 '20 at 11:15
  • Does this answer your question? [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – Dharman Sep 08 '20 at 13:51

1 Answers1

-1

Mysqli->prepare can also return FALSE if an error occurred. And did you check if $table has value? Because I think your code has some problems.for example in your AJAX you should use:

var selected =$("input[name=gender]:checked").val();  

To send male or female. In your PHP file you should write

$choice = $_POST["det"]; 

because you send values by this name so you don't have any thing with gender name.

I think simply you can write

$table = $post['det'];

And it'll get table name.

Dharman
  • 30,962
  • 25
  • 85
  • 135