Note that there are some explanatory texts on larger screens.

plurals
  1. POcan't get JQUERY .autocomplete to work right with .attr
    primarykey
    data
    text
    <p>I'm going around and around in circles on this. I'm new to JQUERY and so far it's awesome! I've looked over tons of stuff on the internet and here in the answered questions but I have not been able to figure out the right answer. Right now I'm killing myself trying to figure out what I'm doing wrong on it. The .autocomplete is working fine with the basic stuff like giving me back a string from the MYSQL database and inputing it into form how I want. But the problem I'm having is bringing back a boolean value and marking a check box with that value. What I'm getting right now is all the boxes checked even if the value if 0.</p> <h2>Here's my .autocomplete code.</h2> <pre><code>&lt;?php require_once('../connection.php'); ?&gt; &lt;?php include '../_includes/jq.inc.php';?&gt; &lt;html&gt; &lt;head&gt; &lt;script type="text/javascript"&gt; $(function() { $('#Company_Name').val(""); $('#Manager').val(""); $('#Phone').val(""); $('#Contacted').val(""); $('#Pitched').val(""); $('#Interested').val(""); $("#autoSearch").autocomplete({ source: "Test.QUERY.php", minLength: 2, select: function(event, ui) { $('#Company_Name').val(ui.item.Company_Name); $('#Manager').val(ui.item.Manager); $('#Phone').val(ui.item.Phone); $('#Contacted').attr("checked", true); $('#Pitched').attr("checked", true); $('#Interested').attr("checked", true); } }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form action="&lt;?php echo $PHP_SELF;?&gt;" method="post"&gt;SEARCH: &lt;input type="text" id="autoSearch" size="60" onClick="this.form.reset()"/&gt; &lt;/form&gt; &lt;form method="POST" name="form"&gt; &lt;input type="text" name="Company_Name" id="Company_Name"&gt;Company Name&lt;br&gt; &lt;input type="text" name="Manager" id="Manager"&gt;Manager&lt;br&gt; &lt;input type="text" name="Phone" id="Phone"&gt;Phone&lt;br&gt; Contacted&lt;input type="checkbox" name="Contacted" id="Contacted"&gt;&lt;br&gt; Pitched&lt;input type="checkbox" name="Pitched" id="Pitched"&gt;&lt;br&gt; Interested&lt;input type="checkbox" name="Interested" id="Interested"&gt; &lt;br&gt; &lt;input type="submit" name="button" value="UPDATE"&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <h2>Here's how the MYSQL DB is set for the next QUERY</h2> <pre><code>Company_Name varchar(45) Manager varchar(45) Phone varchar(45) Contacted tinyint(1) Pitched tinyint(1) Interested tinyint(1) </code></pre> <h2>Here's the Test.QUERY.php Page</h2> <pre><code>&lt;?php include_once '../conn.php';?&gt; &lt;?php try { $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); } catch(PDOException $e) { echo $e-&gt;getMessage(); } $return_arr = array(); if ($conn) { $ac_term = "%".$_GET['term']."%"; $query = "SELECT Company_Name, Manager, Phone, Contacted, Pitched, Interested, CONCAT_WS(' ', Company_Name, Manager, Phone) AS autoNameShow FROM CustomerList WHERE `Company_Name` LIKE :term OR `Manager` LIKE :term OR `Phone` LIKE :term "; $result = $conn-&gt;prepare($query); $result-&gt;bindValue(":term",$ac_term); $result-&gt;execute(); while ($row = $result-&gt;fetch(PDO::FETCH_ASSOC)) { $row_array['value'] = $row['autoNameShow']; $row_array['Company_Name'] = $row['Company_Name']; $row_array['Manager'] = $row['Manager']; $row_array['Phone'] = $row['Phone']; $row_array['Contacted'] = $row['Contacted']; $row_array['Pitched'] = $row['Pitched']; $row_array['Interested'] = $row['Interested']; array_push($return_arr,$row_array); } } $conn = null; echo json_encode($return_arr); ?&gt; </code></pre> <p>So, the million dollar question is..... What did I do wrong &amp; how should I fix it?</p> <h2>EDIT from Suggestion.</h2> <h2>QUERY PAGE</h2> <pre><code>&lt;?php include_once '../conn.php';?&gt; &lt;?php try { $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); } catch(PDOException $e) { echo $e-&gt;getMessage(); } $return_arr = array(); if ($conn) { $ac_term = "%".$_GET['term']."%"; $query = "SELECT Company_Name, Manager, Phone, Contacted, Pitched, Interested, CONCAT_WS(' ', Company_Name, Manager, Phone) AS autoNameShow FROM CustomerList WHERE `Company_Name` LIKE :term OR `Manager` LIKE :term OR `Phone` LIKE :term "; $result = $conn-&gt;prepare($query); $result-&gt;bindValue(":term",$ac_term); $result-&gt;execute(); while ($row = $result-&gt;fetch(PDO::FETCH_ASSOC)) { $return_arr[] = array( 'value' =&gt; $row['autoNameShow'], 'Company_Name' =&gt; $row['Company_Name'], 'Manager' =&gt; $row['Manager'], 'Phone' =&gt; $row['Phone'], 'Contacted' =&gt; $row['Contacted'], 'Pitched' =&gt; $row['Pitched'], 'Interested' =&gt; $row['Interested'], ); }} $conn = null; echo json_encode($return_arr); ?&gt; </code></pre> <h2>autocomplete page</h2> <pre><code>&lt;?php include '../_includes/jq.inc.php';?&gt; &lt;html&gt; &lt;head&gt; &lt;script type="text/javascript"&gt; var $form = $('#my_form'); $(function() { $('#Company_Name').val(""); $('#Manager').val(""); $('#Phone').val(""); $('#Contacted').val(""); $('#Pitched').val(""); $('#Interested').val(""); $('#autoSearch').autocomplete({ source: 'Test.QUERY.php', minLength: 2, select: function(event, ui) { $form.find('input').val(''); $form.find('input[type="checkbox"]').attr('checked', ''); $('#Company_Name').val(ui.item.Company_Name); $('#Manager').val(ui.item.Manager); $('#Phone').val(ui.item.Phone); if (ui.item.Contacted) { $('#Contacted').attr('checked', 'checked'); } if (ui.item.Pitched) { $('#Pitched').attr('checked', 'checked'); } if (ui.item.Interested) { $('#Interested').attr('checked', 'checked'); } } }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form action="&lt;?php echo $PHP_SELF;?&gt;" method="post"&gt;SEARCH: &lt;input type="text" id="autoSearch" size="60" onClick="this.form.reset()"/&gt; &lt;/form&gt; &lt;form method="POST" name="form" id="my_form"&gt; &lt;input type="text" name="Company_Name" id="Company_Name"&gt;Company Name&lt;br&gt; &lt;input type="text" name="Manager" id="Manager"&gt;Manager&lt;br&gt; &lt;input type="text" name="Phone" id="Phone"&gt;Phone&lt;br&gt; Contacted&lt;input type="checkbox" name="Contacted" id="Contacted"&gt;&lt;br&gt; Pitched&lt;input type="checkbox" name="Pitched" id="Pitched"&gt;&lt;br&gt; Interested&lt;input type="checkbox" name="Interested" id="Interested"&gt; &lt;br&gt; &lt;input type="submit" name="button" value="UPDATE"&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <h2>Still getting all the boxes checked.</h2> <p>What am I doing wrong?</p> <h2>Also posting the JSON return data.</h2> <pre><code>[{"value":"Company Test 1 Manager Test 1 111-111-1111","Company_Name":"Company Test 1","Manager":"Manager Test 1","Phone":"111-111-1111","Contacted":"0","Pitched":"0","Interested":"0"},{"value":"Company Test 2 Manager Test 2 222-222-2222","Company_Name":"Company Test 2","Manager":"Manager Test 2","Phone":"222-222-2222","Contacted":"0","Pitched":"1","Interested":"1"},{"value":"Company Test 3 Manager Test 3 333-333-3333","Company_Name":"Company Test 3","Manager":"Manager Test 3","Phone":"333-333-3333","Contacted":"0","Pitched":"0","Interested":"1"},{"value":"Company Test 4 Manager Test 4 444-444-4444","Company_Name":"Company Test 4","Manager":"Manager Test 4","Phone":"444-444-4444","Contacted":"1","Pitched":"0","Interested":"1"},{"value":"Company Test 5 Manager Test 5 555-555-5555","Company_Name":"Company Test 5","Manager":"Manager Test 5","Phone":"555-555-5555","Contacted":"0","Pitched":"1","Interested":"1"},{"value":"Company Test 6 Manager Test 6 666-666-6666","Company_Name":"Company Test 6","Manager":"Manager Test 6","Phone":"666-666-6666","Contacted":"1","Pitched":"0","Interested":"1"},{"value":"Company Test 7 Manager Test 7 777-777-7777","Company_Name":"Company Test 7","Manager":"Manager Test 7","Phone":"777-777-7777","Contacted":"0","Pitched":"1","Interested":"1"},{"value":"Company Test 8 Manager Test 8 888-888-8888","Company_Name":"Company Test 8","Manager":"Manager Test 8","Phone":"888-888-8888","Contacted":"1","Pitched":"0","Interested":"1"},{"value":"Company Test 9 Manager Test 9 999-999-9999","Company_Name":"Company Test 9","Manager":"Manager Test 9","Phone":"999-999-9999","Contacted":"0","Pitched":"1","Interested":"0"},{"value":"Company Test 10 Manager Test 10 000-000-0000","Company_Name":"Company Test 10","Manager":"Manager Test 10","Phone":"000-000-0000","Contacted":"1","Pitched":"0","Interested":"1"}] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload