PHP MySQL复选框从表中提取数据

时间:2011-12-02 20:24:12

标签: php jquery mysql ajax checkbox

问题:对复选框进行编码以获得所需结果的最有效方法是什么?

好,

所以我有一张包含名单和各种特征的表格。在ENUM的Y和N的特征下。因此,如果名称具有特定的特征,那么如果不是显而易见的话,它们将显示Y.名称可以且确实具有多个特征。

话虽如此,我想使用PHP / MySQL / jQuery / Ajax以及复选框来匹配特征。当通过复选框选择每个特征时,匹配的名称列表将显示或消失。

我完成了相当多的编码工作。挑战是复选框。

    <table width="50%" border="0">
      <tr>
        <td> <label>
          <input type="checkbox" name="oneonone" value="oneonone"/>
          One-on-one</label>
          <br />
        </td>
        <td>  <input type="checkbox" name="smallgroup" value="smallgroup"/>
          Small Group</label></td>

      </tr>
      <tr>
        <td><label>
          <input type="checkbox" name="prek12" onChange="checkmark(prek12)" />
          Educational: Prek - 12</label></td>
        <td><label>
          <input type="checkbox" name="largegroup" onChange="checkmark(largegroup)"/>
          Large Group</label></td>
      </tr>
      <tr>
        <td><label>
          <input type="checkbox" name="postsecondary" onChange="checkmark(postsecondary)" />
          Educational:  Post-secondary</label></td>
        <td><label>
          <input type="checkbox" name="platform" onChange="checkmark(platform)"/>
          Platform</label></td>
      </tr>
      <tr>
        <td> <label>
          <input type="checkbox" name="business" onChange="checkmark(business)"/>
          Business</label></td>
        <td> <label>
          <input type="checkbox" name="community" onChange="checkmark(community)"/>
          Community</label></td>
      </tr>
      <tr>
        <td> <label>
          <input type="checkbox" name="legal" onChange="checkmark(legal)"/>
          Legal</label></td>
        <td><label>
          <input type="checkbox" name="government" onChange="checkmark(government)"/>
          Government</label></td>
      </tr>
      <tr>
        <td><label>
          <input type="checkbox" name="mentalhealth" onChange="checkmark(mentalhealth)" />
          Mental Health</label></td>
        <td><label>
          <input type="checkbox" name="performingarts" onChange="checkmark(performingarts)" />
          Performing Arts</label></td>
      </tr>
      <tr>
        <td> <label>
          <input type="checkbox" name="medical" onChange="checkmark(medical)" />
          Medical</label></td>
        <td> <label>
          <input type="checkbox" name="oral" onChange="checkmark(oral)"/>
          Oral Interpreting</label></td>
      </tr>
      <tr>
        <td> <label>
          <input type="checkbox" name="deafblind" onChange="checkmark(deafblind)"/>
          Deaf-blind</label></td>
        <td> <label>
          <input type="checkbox" name="religious" onChange="checkmark(religious)"   />
          Religious</label><input name="ReligiousType" type="text"  size="25" maxlength="25" />
          <br />
          </label></td>
      </tr>
      <tr>
        <td><label>
          <input type="checkbox" name="mls" onChange="checkmark(mls)"  />
          Minimal Language</label></td>
        <td><input type="checkbox" name="otherspecial" value="Y" onChange="checkmark(otherspecial)"  />
          Other</label><input name="OtherType"  value=" " type="text" size="25" maxlength="25" /></td>
      </tr>

  <br /><br />

</table>







 <?php
include("connect.php");

if (isset($_POST['name_search_term_php']) OR isset($_POST['loc_search_term_php']) OR isset($_POST['spec_search_term_php'])){

//Info received from jQuery

$location=$_POST['loc_search_term_php'];

$specialty=$_POST['spec_search_term_php'];

$name_search_term=mysql_real_escape_string(htmlentities($_POST['name_search_term_php']));

if (!empty($location) OR !empty($specialty)){

//attaches variable of field name
$loc_search="sl.".$location;
$spec_search="sp.".$specialty;



//Does work WITHOUT 'OR' but not With :(

$sql="SELECT * FROM service_location sl 
INNER JOIN contact c ON sl.id=c.id
INNER JOIN specialty sp ON sl.id=sp.id 
WHERE  ".$spec_search."='Y'  OR ".  $loc_search." ='Y')";


//echo "<br>".$sql;

$search=mysql_query($sql);

$result_count=mysql_num_rows($search);

$suffix=($result_count!=1) ? 's':'';

if($result_count>=1)
{

echo '<br><br><center>Your search of <strong>', $name_search_term,'', $location ,'</strong> returned ', $result_count ,' name',$suffix,'</center>';
}

while($results_row=mysql_fetch_assoc($search)){

echo'<br><br>',$results_row['firstname'],' ', $results_row['lastname'] ,'';

mysql_close($connect);
}
}
}

?>










    $(document).ready(function(){ 
$('#name_search').keyup(function() {

var search_term_js=$('#name_search').val();


$.post('search.php', {name_search_term_php: search_term_js}, function(data){

$('#search_results').html(data);


});
});
$('#certified').change(function(){
var cert_search_term_js=$('#certified').val();


$.post('search.php', {cert_search_term_php: cert_search_term_js}, function(data){

$('#search_results').html(data);

});
});

$('#location').change(function(){
var loc_search_term_js=$('#location').val();


$.post('search.php', {loc_search_term_php: loc_search_term_js}, function(data){

$('#search_results').html(data);

});
});

$(':checkbox').click(function(){
if($(this).is(':checked'))
{
var spec_search_term_js=$(this).attr("name");


$.post('search.php', {spec_search_term_php: spec_search_term_js}, function(data){



$('#search_results').html(data);
});
}
});

});

0 个答案:

没有答案