-2

this code is inside while after I submit the form instead of retaining what I checked it checked all after submitting.

I just want to happen after submitting the only check box i check is checked.

What should i do ?

 <input  type="checkbox" title ="<?php echo $sym ?>"<?php if(isset($_POST['poscon'])) echo "checked='checked'"; ?> name="poscon[]" value ="<?php echo $pc?>"><?php echo $pc?>
Marcus
  • 6,697
  • 11
  • 46
  • 89

2 Answers2

1

$_POST['poscon'] is a array. Run my script and see how it works.

<?php
/**
 * File test.php
 */


// Store checked Values in Array $arrChecked
$arrChecked=array();
if(isset($_POST) && $_POST['poscon']) {

    // Debug: Show all POST Vars
    echo "<pre>"; print_r($_POST); echo "</pre>";


    foreach($_POST['poscon'] AS $checkboxValue) {
        // fill array with checked values
        // e.g. arrChecked['foo'] = true;       
        $arrChecked[$checkboxValue] = true;
    }
}

/** 
 * Note for HTML-Block:
 * shorthand php if/else used
 * http://stackoverflow.com/questions/4567292/overview-of-php-shorthand
 */
?>

<form action="test.php" method="post">
    <input type="checkbox" name="poscon[]" value="foo" <?php echo (isset($arrChecked) && $arrChecked['foo'] ? 'checked="checked"' : '');?>> foo

    <input type="checkbox" name="poscon[]" value="bar" <?php echo (isset($arrChecked) && $arrChecked['bar'] ? 'checked="checked"' : '');?>> bar

    <input type="submit" value="go">
</form>
Henry
  • 597
  • 4
  • 12
  • that is correct but after my radio button theres a function if(isset($_GET["poscon"])){ $_SESSION["poscon"] = $_GET["poscon"]; $dr=$_SESSION['poscon']; if(isset($_POST['submit'])) { if(!empty($_GET['poscon'])) $_SESSION['poscon'] = $_POST['poscon']; $part=$_GET["poscon"]; } – Maricar Manlulu Aug 10 '15 at 18:16
  • $poscon=mysqli_real_escape_string($link,$_GET['poscon']); $p = mysqli_query($link,"select distinct PossibleCondition,Symptoms from healthguide where Subpart like '%$poscon%' and PossibleCondition REGEXP '^[N-Z].*$' Order by PossibleCondition "); while($r=mysqli_fetch_array($p)){ $pc=$r["PossibleCondition"]; $sym=$r["Symptoms"]; ?>
    – Maricar Manlulu Aug 10 '15 at 18:16
1

refer to in_array

<?php
if(isset($_GET["poscon"])) { 

    $_SESSION["poscon"] = $_GET["poscon"]; 
    $dr=$_SESSION['poscon']; 

    if(isset($_POST['submit'])) { 
        if(!empty($_GET['poscon'])) 
            $_SESSION['poscon'] = $_POST['poscon']; 
        $part=$_GET["poscon"]; 
    }

    $poscon=mysqli_real_escape_string($link,$_GET['poscon']); 

    $p = mysqli_query($link,"select distinct PossibleCondition,Symptoms from healthguide where Subpart like '%$poscon%' and PossibleCondition REGEXP '^[N-Z].*$' Order by PossibleCondition "); 

    while($r=mysqli_fetch_array($p)) { 
        $pc=$r["PossibleCondition"]; 
        $sym=$r["Symptoms"]; 

        if(isset($_POST) && isset($_POST['poscon']) && in_array($pc,$_POST['poscon']))
            $strIsChecked='checked="checked"';
        else
            $strIsChecked=null;

        echo '<tr>';    
        echo '<td><input type="checkbox" '.$strIsChecked.' title ="'.$sym.'" name="poscon[]" value ="'.$pc.'"></td>';
        echo '<td>'.$pc.'</td>';
        echo '</tr>';   

    } 

} 
?>
Henry
  • 597
  • 4
  • 12