Jump to content

Checkbox instead of option value


mkr365

Recommended Posts

Hello,

 

I have a site where you can select for example a "categorie", after the selection (option value) the information is from my database.

 

Next problem: At the moment i'm using option value's but i really want to use checkboxes. I'v tried to changed my code so that the checkboxes are active, but they aren't working.

 

Is it difficult to change this? Or can somebody see something is wrong in my code?

 

The original code with option value (which is working fine):

 

[color=red]<tr><td>Rubriek:</td><td><select name="categorien"><option value="">Selecteer...</option>';
$sql = "SELECT * FROM rubrieken INNER JOIN groepen ON rubrieken.GroepID = groepen.GroepID WHERE Groepnaam = 'Architect' ORDER BY Rubrieknaam ";
$result = mysql_query($sql) or die(mysql_error()); 
while($query = mysql_fetch_array($result))
{
	if ($_GET['categorien'] == $query['RubriekID'])
  			echo '<option value="'.$query['RubriekID'].'" selected>'.$query['Rubrieknaam'].'</option>';

The code which i tried to change:

[color=red]<tr><td>Rubriek:</td><td><select name="categorien"><option value="">Selecteer...</option>';
$sql = "SELECT * FROM rubrieken INNER JOIN groepen ON rubrieken.GroepID = groepen.GroepID WHERE Groepnaam = 'Architect' ORDER BY Rubrieknaam ";
$result = mysql_query($sql) or die(mysql_error()); 
while($query = mysql_fetch_array($result))
{
	if ($_GET['categorien'] == $query['RubriekID'])
  			echo '<input type="checkbox"'.$query['RubriekID'].'" selected>'.$query['Rubrieknaam'].'';
	else
		echo '<input type="checkbox"'.$query['RubriekID'].'" >'.$query['Rubrieknaam'].'';
}
echo '</select></td></tr>
<tr><td colspan="2"><input type="submit" value="zoeken"><input type="hidden" name="zoek" value="true"></td></tr>
	else
		echo '<option value="'.$query['RubriekID'].'" >'.$query['Rubrieknaam'].'</option>';
}
echo '</select></td></tr>
<tr><td colspan="2"><input type="submit" value="zoeken"><input type="hidden" name="zoek" value="true"></td></tr>[/color][/color]

Link to comment
Share on other sites

You need to brush-up on your HTML

 

checkboxes need  name and value attributes

echo '<input type="checkbox" name="categorien[]" value="{$query['RubriekID']}" selected>'.$query['Rubrieknaam'].'';

 

checkboxes don't have select.../select tags around them

 

Only checked checkbox values are posted.

Link to comment
Share on other sites

Cool, that helped me a lot further! It's almost working :)

 

The search works just fine now but I can only search for 1 checkbox, if i select multiple checkboxes than I see only the results from the last one selected. Is there some way to show the results for multiple selected checkoboxes?

 

<tr><td>Rubriek:</td><td>';

$sql = "SELECT * FROM rubrieken INNER JOIN groepen ON rubrieken.GroepID = groepen.GroepID WHERE Groepnaam = 'Architect' ORDER BY Rubrieknaam ";

$result = mysql_query($sql) or die(mysql_error());

while($query = mysql_fetch_array($result))

{

if ($_GET['categorien'] == $query['RubriekID'])

  echo '<input type="checkbox" name="categorien" value="'.$query['RubriekID'].'" checked>'.$query['Rubrieknaam'].'';

else

echo '<input type="checkbox" name="categorien" value="'.$query['RubriekID'].'">'.$query['Rubrieknaam'].'';

}

echo '</select></td></tr>

<tr><td colspan="2"><input type="submit" value="zoeken"><input type="hidden" name="zoek" value="true"></td></tr>

 

 

 

Link to comment
Share on other sites

Sorry for asking, but do i have to insert the next code:

 

foreach ($_POST['catogorien'] as $cat

{

    // process

    echo $cat, '<br/>';

}

 

Or do i only have to change name="categorien" to name="categorien[]" ? (this last thing isn't working)

 

 

Link to comment
Share on other sites

Sorry for asking, but do i have to insert the next code:

 

foreach ($_POST['catogorien'] as $cat

{

    // process

    echo $cat, '<br/>';

}

 

Or do i only have to change name="categorien" to name="categorien[]" ? (this last thing isn't working)

 

 

Did you try both?

Link to comment
Share on other sites

Well if i change it to name="categorien[]" i get the following error:

 

Unknown column 'Array' in 'where clause'

 

code:

 

<tr><td>Rubriek:</td><td>';

$sql = "SELECT * FROM rubrieken INNER JOIN groepen ON rubrieken.GroepID = groepen.GroepID WHERE Groepnaam = 'Architect' ORDER BY Rubrieknaam ";

    $result = mysql_query($sql) or die(mysql_error());

    while($query = mysql_fetch_array($result))

    {

        if ($_GET['categorien'] == $query['RubriekID'])

              echo '<input type="checkbox" name="categorien[]" value="'.$query['RubriekID'].'" checked>'.$query['Rubrieknaam'].'';

        else

            echo '<input type="checkbox" name="categorien[]" value="'.$query['RubriekID'].'">'.$query['Rubrieknaam'].'';

    }

echo '</select></td></tr>

<tr><td colspan="2"><input type="submit" value="zoeken"><input type="hidden" name="zoek" value="true"></td></tr>

Link to comment
Share on other sites

You mean there something wrong with the where clause?

 

<tr><td>Rubriek:</td><td>';

$sql = "SELECT * FROM rubrieken INNER JOIN groepen ON rubrieken.GroepID = groepen.GroepID WHERE Groepnaam = 'Architect' ORDER BY Rubrieknaam ";

   

Link to comment
Share on other sites

Hello,

 

Well i have several errors:

 

Undefined index:  bedrijfsnaam in C:\\webroot\\Nieuw Bouwref\\headerarchitect.php on line 232

Undefined index:  categorien in C:\\webroot\\Nieuw Bouwref\\headerarchitect.php on line 241

 

line 232 : <tr><td>Bedrijfsnaam:</td><td><input type="text" name="bedrijfsnaam" value="'.$_GET['bedrijfsnaam'].'"input SIZE="30"></td></tr>

 

line 241: if ($_GET['categorien'] == $query['RubriekID'])

 

Seems like "bedrijfsnaam" and "categorien" are unknown.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.