-3

I need to display checkbox that binds dynamically using ajax and php .

my code is

<?php

include 'dbconnect.php';
$result = mysqli_query($link, "SELECT * FROM city where district_id='$dist' ");

while($city_row=mysqli_fetch_array($result)){
    ?>

    <input type="checkbox" value="<?php echo $city_row['id']; ?>"/><?php echo $city_row['city_name']; ?>
<?php
}
}

?>

ajax script

$.ajax({

type: "POST",
cache: false,
url: "get_locality.php",
    data:{districts:hi },
success: function(response){
    alert(response);
    $('#city').html(response);

}
});

html div

<div class="checkbox" ><span id="city">

                            </span>

                        </div>

but it only displays the values . not displaying the checkboxes

3
  • $dist where is that defined? Commented Apr 10, 2018 at 11:49
  • districts:hi where are those defined? Commented Apr 10, 2018 at 11:49
  • Seems like I've fallen onto deaf ears. Commented Apr 10, 2018 at 11:56

1 Answer 1

-1

The issue is you print only the value of checkbox and not the checkbox html. So store all code inside one variable and print that variable.

<?php
include 'dbconnect.php';
$result = mysqli_query($link, "SELECT * FROM city where district_id='$dist' ");
$str = '';
while($city_row=mysqli_fetch_array($result)){
$str .= '<input type="checkbox" value="'.$city_row['id'].'"/>'.$city_row['city_name'];
}
echo $str;
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Why should the OP "use below code"? A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.
Yes @JayBlanchard , You are right. I add description of the issue. Thank you for correcting me :)
Another solution with double quotes (more readable in my opinion), which avoid the multiple concatenations: $str .= "<input type='checkbox' value='{$city_row['id']}'/>{$city_row['city_name']}";

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.