Checkbox "checked=\"checked\"" from url array

Posted on 2012-09-03
Last Modified: 2012-09-03
a search page
The form sends to itself
What I want is that, the checkboxes must be checked as the choice before submit in the result

like this:

<form action="samepage.php" method="get">
<?php do {....
<input <?php if (!(strcmp($_GET['kat'],$row_kategori['kat']))) {echo "checked=\"checked\"";} ?>  name="kat[]" type="checkbox"   value="<?php echo $row_kategori['kat']; ?>" />
<?php } while.....

Anyone have a suggestion :)
Question by:HelgeLind
    LVL 58

    Expert Comment

    <input <?php if (isset($_GET['kat'])) {echo 'checked="checked"';} ?>  name="kat" type="checkbox"   value="<?php echo $row_kategori['kat']; ?>" />

    Author Comment

    sorry, do not work with this url

    LVL 58

    Expert Comment

    A bit confused - you just want to check that all checkboxes are checked before submitting or just one checkbox or...?
    Or are you asking how to set the checkbox to checked if previously checked on form submit

    Author Comment

    I want to show the result of input in the form on submit, while they searched categories will be check t  in the form check boxes. So the user can see what it was I searched for while she sees the results

    Here are most of the code

    <?php virtual('/connection/file.php'); 
    $k2= $_GET['cat'];
    if (isset($_GET['from'])) {$from = $_GET['from'];} else {$from = date('Y-m-d');};
    if (isset($_GET['to'])) {$to = $_GET['to];} else {$to = date('Y-m-d', strtotime("+2 months"));};
    if (isset($_GET['cat']))  {$cat2 = implode(", " ,$k2 );}  else {$cat2 = ' 100,103 ';};
    mysql_select_db($database_filer, $filer);
    $query_schema = "SELECT pv_akt.akt_id, bla bla bla  FROM bla bla bla WHERE pv_day between '".$from."' and '".$to."' and pv_akt.status   IN(".str_replace("'","",$kat2).") ORDER BY pv_handelser.dag ASC, pv_handelser.tid ASC";
    $schema = mysql_query($query_schema, $filer) or die(mysql_error());
    mysql_select_db($database_filer, $filer);
    $query_category = "SELECT * FROM pv_aktstat ORDER BY sort ASC";
    $kategori = mysql_query($query_kategori, $filer) or die(mysql_error());
    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xmlns="">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <div class="noprint">
    <form action="/samepage.php" method="get">
    From   <input name="from" type="text" id="from" value="<?php echo $from; ?>" size="10" maxlength="10" />
    To <input name="to" type="text" id="to" value="<?php echo $to; ?>" size="10" maxlength="10" />
     <?php do { ?><label><?php echo $row_kategori['status']; ?>
       <input name="cat[]" type="checkbox"   value="<?php echo $row_category['cat']; ?>" />
     <?php } while ($row_category = mysql_fetch_assoc($category)); ?>
    <input name="search" type="submit" id="search" value="search" /> 
    Result  here:

    Open in new window

    LVL 58

    Accepted Solution

    Still not sure I'm following what you are trying to achieve but try...

    <input <?php if (in_array($row_kategori['kat'],$_GET['kat'])){echo 'checked="checked"';}?>  name="kat[]" type="checkbox"   value="<?php echo $row_kategori['kat']; ?>" />

    If the value of $row_kategori['kat'] exists in the posted array of kat then the checkbox will be checked.

    Author Closing Comment

    Thanks that works

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Android App Development

    This course will involve creating widgets, customize list view, grid view, spinners, etc. Creating applications using audio, video, and SQLite database. Ending with publication on Google Play.

    A common challenge, or question, when working with two calendar dates is: What is the difference between the specified days? As is frequently the case, the answer is: It depends... What kind of difference do you want? Given two date obje…
    Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now