Jump to content

PHP Checkboxes Auto Select


NewcastleFan
Go to solution Solved by 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?

Edited by NewcastleFan
Link to comment
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?

Link to comment
Share on other sites


$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>';
}
Link to comment
Share on other sites

 

$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"

Link to comment
Share on other sites


$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>';
}
Link to comment
Share on other sites

 

$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 :(

Link to comment
Share on other sites

 

$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?

Link to comment
Share on other sites

  • Solution

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>';
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.