简体   繁体   中英

JQuery autocomplete return a checkbox checked

After calling the jquery function how do you make a checkbox selected from a Boolean value in a Mysql database? Here's what is have, but I'm not sure how to get the check box selected and where to put it in the jquery. (FreeShip & BizlicSent) are the Boolean values that need to go into the check boxes. Any help would be greatly appreciated.

$(function() {
 $('#Customer_ID').val("");
 $('#Name').val("");
 $('#BillingEmail').val("");
 $('#FreeShip').val("");
 $('#BizlicSent').val("");
    $("#searchterm").autocomplete({
                source: "Customer.QUERY.php",
                minLength: 3,
                autoFocus: true,
                select: function(event, ui) {
                        $('#Customer_ID').val(ui.item.Customer_ID);
                        $('#FirstName').val(ui.item.FirstName);
                        $('#BillingEmail').val(ui.item.BillingEmail);
                        $('#FreeShip').val(ui.item.FreeShip);
                        $('#BizlicSent').val(ui.item.BizlicSent);
                }
            });
     }); 

New Edit with more data on the pages

This is the page that have the javascript/jquery info

<?php include '../JQ/jquery.php';?>
<html>
<head>
<script type="text/javascript">
$(function() {
 $('#Customer_ID').val("");
 $('#Name').val("");
 $('#BillingEmail').val("");
 $('#FreeShip').val("");
 $('#BizlicSent').val("");
    $("#searchterm").autocomplete({
                source: "Customer.QUERY.php",
                minLength: 3,
                autoFocus: true,
                select: function(event, ui) {
                    $('#Customer_ID').val(ui.item.Customer_ID);
                    $('#FirstName').val(ui.item.FirstName);
                    $('#BillingEmail').val(ui.item.BillingEmail);
                    $('#FreeShip').attr("checked", ui.item.FreeShip);
                    $('#BizlicSent').attr("checked", ui.item.BizlicSent);
            }

            });
     }); 
</script>
</head>
<body>
<form action="<?php echo $PHP_SELF;?>"  method="post">SEARCH:
<input type="text" id="searchterm" size="60" onClick="this.form.reset()"/>
</form>
<form method="POST" name="form" id="my_form">
<input type="text" name="Customer_ID" id="Customer_ID">
Customer ID<br>
<input type="text" name="FirstName" id="FirstName">
First Name<br>
<input type="text"  name="BillingEmail" id="BillingEmail">
Billing Email<br>
FreeShip<input type="checkbox" name="FreeShip" id="FreeShip"><br>
BizlicSent<input type="checkbox" name="BizlicSent" id="BizlicSent"><br>
<br>
<input type="submit" name="button" value="UPDATE"> 
</form>
</body>
</html>

This is the QUERY page.

<?php include_once '../connection.php';?>
<?php

try {
  $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
}
catch(PDOException $e) {
    echo $e->getMessage();
}

$return_arr = array();

if ($conn)
{
    $ac_term = "%".$_GET['term']."%"; 
    $query = "SELECT
Customer_ID, FirstName, BillingEmail, FreeShip, BizlicSent,

CONCAT_WS(' ', Customer_ID, FirstName, BillingEmail) AS displayName
FROM CustomerListNew
WHERE `Customer_ID` LIKE :term
    OR `FirstName` LIKE :term
    OR `BillingEmail` LIKE :term
";

    $result = $conn->prepare($query);
    $result->bindValue(":term",$ac_term);
    $result->execute(); 

    while ($row = $result->fetch(PDO::FETCH_ASSOC)) {

        $row_array['value'] = $row['displayName'];

        $row_array['Customer_ID'] = $row['Customer_ID'];
        $row_array['FirstName'] = $row['FirstName'];
        $row_array['BillingEmail'] = $row['BillingEmail'];  
        $row_array['FreeShip'] = $row['FreeShip'];
        $row_array['BizlicSent'] = $row['BizlicSent'];

        array_push($return_arr,$row_array);
    }

}   
$conn = null;
echo json_encode($return_arr);
?>

Here's the JSON response.

[{"value":"asdf asdf asdf","Customer_ID":"asdf","FirstName":"asdf","BillingEmail":"asdf","FreeShip":"1","BizlicSent":"0"},{"value":"qwer qwer qwer","Customer_ID":"qwer","FirstName":"qwer","BillingEmail":"qwer","FreeShip":"0","BizlicSent":"1"},{"value":"aaaa aaaa aaaa","Customer_ID":"aaaa","FirstName":"aaaa","BillingEmail":"aaaa","FreeShip":"1","BizlicSent":"0"},{"value":"bbbb bbbb bbbb","Customer_ID":"bbbb","FirstName":"bbbb","BillingEmail":"bbbb","FreeShip":"0","BizlicSent":"1"},{"value":"cccc cccc cccc","Customer_ID":"cccc","FirstName":"cccc","BillingEmail":"cccc","FreeShip":"1","BizlicSent":"1"},{"value":"dddd dddd dddd","Customer_ID":"dddd","FirstName":"dddd","BillingEmail":"dddd","FreeShip":"0","BizlicSent":"0"}]

The sign = is missing.

You have to use:

$('#FreeShip').prop('checked', ui.item.FreeShip === "1");

Okay! here's the solution that's working now....

select: function(event, ui) {
  $('#Customer_ID').val(ui.item.Customer_ID);
  $('#FirstName').val(ui.item.FirstName);
  $('#BillingEmail').val(ui.item.BillingEmail);

    if (ui.item.FreeShip == "1") {
      $('#FreeShip').prop('checked', true);
      } else {
      $('#FreeShip').prop('checked', false);
    }

    if (ui.item.BizlicSent == "1") {
      $('#BizlicSent').prop('checked', true);
      } else {
      $('#BizlicSent').prop('checked', false);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM