Jump to content

PHP Checkboxes Auto Select


NewcastleFan

Recommended Posts

Hey guys, 

 

Not sure if anyone is able to help me out I'm trying to split up some data from a table and select check boxes depending on if the tag exists:

 

Database:

 

Table: Tags

Fields: id, tag

 

PHP so far:

 

$sqlCommand = "SELECT tag FROM tags";
$query2 = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
$tagoptions="";
while ($row = mysqli_fetch_array($query2)) { 
    $tag = $row["tag"];
$string = $posttag;
$array = explode(',', $string);
if ($string == $tag) {
$checked = "checked";
}
else {
$checked = "";
}
$tagoptions.="<input type='checkbox' name='checkboxname[]' value='$tag' $checked>".$tag.'<br>';
}

 

 


The data in the tag's field:

ID - TAG

1 tag1

2 tag2

3 tag3

 

The tag table is used to generate the tag check boxes. Once they are generated (Which works).

 

I then have the data been requested from a different table which could be multiple options e.g.:

 

tag1, tag3

 

However if only one tag is selected then the box is checked, if 2 tags or more are selected then neither work.

 

Anyone have any ideas where I am going wrong here?

Link to comment
https://forums.phpfreaks.com/topic/276529-php-checkboxes-auto-select/
Share on other sites

The posttag is opened up in the array here?

 

$string = $posttag;
	$array = explode(',', $string);
				if ($string == $array) {
							$checked = "checked";
							}
						else {
							$checked = "";
						}

 

I've used explode to separate them but then I'm not sure how to get from that point to checking to check the box?


$sqlCommand = "SELECT tag FROM tags";
$query2 = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
$tagoptions="";
while ($row = mysqli_fetch_array($query2)) {
$tag = $row["tag"];
if (in_array($tag, $postag)) {
$checked = "checked";
}
else {
$checked = "";
}
$tagoptions.="<input type='checkbox' name='checkboxname[]' value='$tag' $checked>".$tag.'<br>';
}
  On 4/4/2013 at 3:02 PM, Technocrat said:

 

$sqlCommand = "SELECT tag FROM tags";
$query2 = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
$tagoptions="";
while ($row = mysqli_fetch_array($query2)) { 
    $tag = $row["tag"];   
    if (in_array($tag, $postag)) {
        $checked = "checked";
    }
    else {
        $checked = "";
    }
    $tagoptions.="<input type='checkbox' name='checkboxname[]' value='$tag' $checked>".$tag.'<br>';
}

 

That doesn't seem to do anything, no boxes are checked no matter the value of $posttag.

 

Not sure if I've explained it correctly I'm not the greatest at explaining sorry.

 

$posttag would come through with a value of "tag1, tag2"

$tag would come through in an array with my while loop "tag1" "tag2" "tag3"


$sqlCommand = "SELECT tag FROM tags";
$query2 = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
$tagoptions="";
$postag = explode(',', str_replace(' ', '', $postag));
while ($row = mysqli_fetch_array($query2)) {
$tag = $row["tag"];
if (in_array($tag, $postag)) {
$checked = "checked";
}
else {
$checked = "";
}
$tagoptions.="<input type='checkbox' name='checkboxname[]' value='$tag' $checked>".$tag.'<br>';
}
  On 4/4/2013 at 4:32 PM, Technocrat said:

 

$sqlCommand = "SELECT tag FROM tags";
$query2 = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
$tagoptions="";
$postag = explode(',', str_replace(' ', '', $postag));
while ($row = mysqli_fetch_array($query2)) { 
    $tag = $row["tag"];   
    if (in_array($tag, $postag)) {
        $checked = "checked";
    }
    else {
        $checked = "";
    }
    $tagoptions.="<input type='checkbox' name='checkboxname[]' value='$tag' $checked>".$tag.'<br>';
}

 

Thanks for the reply, this still unfortunately does nothing :(

 

I've tried to take it back to basics and remove the database calls just to figure out how to get this to work:

 

<?php 
$posttag = "tag1, tag2";
$posttag = explode(',', str_replace(' ', '', $posttag));
$tag = "tag1, tag2, tag3";   
    if (in_array($tag, $posttag)) {
        $checked = "checked";
    }
    else {
        $checked = "";
    }
}

echo $posttag;
  
?>
<br>
 <input type='checkbox' name='checkboxname[1]' value='tag1' <?php echo $checked ?>>Tag1<br>
 
 <input type='checkbox' name='checkboxname[2]' value='tag2' <?php echo $checked ?>>Tag2<br>
 
 <input type='checkbox' name='checkboxname[3]' value='tag3' <?php echo $checked ?>>Tag3<br>

 

However this now isn't even working ha, just returns a blank screen :(

  On 4/4/2013 at 4:32 PM, Technocrat said:

 

$sqlCommand = "SELECT tag FROM tags";
$query2 = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
$tagoptions="";
$postag = explode(',', str_replace(' ', '', $postag));
while ($row = mysqli_fetch_array($query2)) { 
    $tag = $row["tag"];   
    if (in_array($tag, $postag)) {
        $checked = "checked";
    }
    else {
        $checked = "";
    }
    $tagoptions.="<input type='checkbox' name='checkboxname[]' value='$tag' $checked>".$tag.'<br>';
}

 

What was in $postag when you ran the above code?

 

Did you echo $tagoptions at the end?

$tagoptions was echoed yes, it output the correct list of check boxes but none were selected.

 

$posttag was pulled from the database column posttag which is a text field, and had the value of "tag1, tag2" so the check boxes for tag1 and tag2 should be selected.

Not sure how its working now but this is what worked no idea why if anyone else is stuck:

 

$sqlCommand = "SELECT tag FROM tags";
$query2 = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
$tagoptions="";
$posttag = explode(',', str_replace(' ', '', $posttag));
while ($row = mysqli_fetch_array($query2)) { 
    $tag = $row["tag"];   
    if (in_array($tag, $posttag)) {
        $checked = "checked";
    }
    else {
        $checked = "";
    }
    $tagoptions.="<input type='checkbox' name='checkboxname[]' value='$tag' $checked>".$tag.'<br>';
}

Archived

This topic is now archived and is 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.