title: ajax checkbox php
body: <p>I have a php-ajax page that where user writes <strong>search string in  search.php</strong>, and the <strong>list is echoed in getsearch.php</strong>. and the <strong>result of getsearch.php is show in search.php in  using ajax</strong> But there is check box in every row.</p>
  
  <pre><code>getsearch.php 
  
  $q = $_GET["q"];
  
  $sql = "select hobbyid,hobby from hobbies where hobby LIKE '%".$q."%' ";
  
  if($result=mysql_query($sql))
  {
      while($row=mysql_fetch_assoc($result))
      { 
           $v = $row['hobbyid'];
           echo $row['hobby'] . '&lt;input type="checkbox" name="hobby[]" value='.$v.'&gt;';
      }
  }
  </code></pre>
  
  <p>And you can see in following code , this sql is fired for every change(onkeyup). I have kept a check box to get array of selected hobbies. These are selected values are <strong>passed to updatehobby.php.</strong></p>
  
  <pre><code>search.php
  
  echo '&lt;form action="updatehobby.php" method="POST"&gt;
  echo 'Search:&lt;input type="text"  onkeyup="showSearch(this.value)" /&gt;
  &lt;div id="txtSearch" &gt;HERE THE RESULTS FROM getsearch.php are shown&lt;/div&gt;
  &lt;input type="submit&gt;&lt;/form&gt;';
  </code></pre>
  
  <p><strong>So when the user writes a new search string, the list gets changed and so the previous check boxes are deselected. So how can i retain the previous check boxes and display them even if the search changes?</strong></p>
  
  <p><strong>Ultimately after 1 day trying, I successfully did this way!</strong></p>
  
  <p>See user enters search string in search.php and the query is passed to getsearch.php and from there the results are echoed and this results are shown in search.php</p>
  
  <p>So instead of using check box, i inserted a button. So every row now has a button instead of 
  checkbox.    </p>
  
  <pre><code>&lt;button type="button" onClick="submitform(&lt;?php echo $row['hobbyid']; ?     &gt;);"&gt;Add hobby&lt;/button&gt;
  </code></pre>
  
  <p>Now in search.php i again used another ajax function (submitform) which handled the onclick call of button.
  When the button add hobby is pressed, ajax function submitform is called and the hobby is inserted.</p>
  
  <p><strong>So basically i used two ajax functions in search.php , one ajax for searching and populating results and another for updating it to database.</strong></p>