Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the checkboxes in the backend and file admin/ban-country-ip.php

CONTENT HERE IS GETTING THE COUNTRY list with checkbox in the admin backend

<?php
    $countryiso = mysql_query("SELECT distinct country_name as country_name FROM location_country");
    echo '<table>';
    echo '<th>Country</th><th> Add/Remove </th>';
    while ($row = mysql_fetch_assoc($countryiso)) {
        echo '<tr>';
        echo '<td>'. $row['country_name'] . '</td>';
        echo '<td><input type="checkbox"></td>';
        echo '</tr>';
    }
    echo '</table>';
?>

AND in the frond end registration it ECHO's them in registration.php.

$ctrstr="";
$res=mysql_query("select *  from location_country where code NOT IN('A1','A2','AP','EU') order by name");
$ctrstr.="<select name=\"country\" id=\"country\" onChange=\"loadState();\"  >
        <option value=\"\" selected=\"selected\">-Select Country-</option>";
while($row=mysql_fetch_row($res))
{
    if($country==$row[0])
    $ctrstr.="<option value=\"$row[0]\" selected>$row[1]</option>";
    else
    $ctrstr.="<option value=\"$row[0]\">$row[1]</option>";
}
$ctrstr.="</select>";

I would like to know how can i hide a value from a select list using checkbox. When i un-check in the back-end a country, it should also be removed in the front end in the select field. If i check it again, should be showing again. I want to mention that the countries are stocked in mysql in a column.

MYSQL - TABLE STRUCTURE

#   Name    Type            Collation    Attributes Null    Default Extra
1   code    varchar(2)    utf8_unicode_ci       No      
2   name    varchar(255)    utf8_unicode_ci     No      
share|improve this question
When you remove any country from back-end you save the list of removed countries ? show the logic how you are saving the removed or added country list in the back-end – dianuj Sep 21 at 15:35
I was thinking somehow on adding another column and in the column to have values 1 for checked countries and 0 for not checked countries. But there are lots of things to do that i could not find and don't know: In this case i will need to update the value from mysql when i check and un-check and after add somehow an if condition like: if value of the selected country is 0 then hide country from select field in front end. Sorry i am a bit of a noob in this. – Adrian Sep 21 at 15:42
1  
Yes you can do it its simple just save the countries with 1 that are checked from back-end and on front end just query them like select * from location_country where code NOT IN('A1','A2','AP','EU') AND your_new_column =1 order by name thats all for the front end only you have to build the update/add functionality with your_new_column – dianuj Sep 21 at 15:51
   
@dianuj ah, great idea, one thing how do i update values in mysql with checkbox? To change value in the your_new_column when i check and uncheck. – Adrian Sep 21 at 15:55
1  
Let me give you example but its not complete make a form put the checkbox with name countries[] array <input type="checkbox" name="countries[]"> get all the posted values from the form first update all the country's your_new_column to 0 then loop through the posted values and update your_new_column to 1 hope you got my idea – dianuj Sep 21 at 16:04
show 2 more comments

1 Answer

using jQuery you can do that to hide option from list:

Add ID attribute to checkbox :

<input type="checkbox" id='hide'>

jQuery :

<script type="text/javascript">
        $(document).ready(function() {

          $("#hide").click(function(){ 
          if( $("#hide:checked").length == 1 ) {
            $("#country option[value='1']").hide();
           }  else {
              $("#country option[value='1']").show();
           }
         });
      });

</script>

And if you want to print this script using PHP :

$script = '<script type="text/javascript">
        $(document).ready(function() {

        $("#hide").click(function(){ 
        if( $("#hide:checked").length == 1 ) {     alert($("#hide:checked").length);
$("#country option[value=1]").hide();
       } else {
           $("#country option[value=1]").show();
       }
    });
  });

    </script>';
echo $script;
share|improve this answer
Are this answer help you to solving your problem? – jetawe Sep 21 at 15:47
And exactly where and how do i add this? – Adrian Sep 21 at 15:50
But it's not possible to integrate it in my php. Don't see how. – Adrian Sep 21 at 16:06
Do you mean how to print this script using php ???? – jetawe Sep 21 at 16:17
yes, where do i add the print? – Adrian Sep 21 at 16:22
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.