0

I tried to search google for it,yet i cant really find the condition really apply on mine. what i need is the checkboxes data is from table1 so i can add more area when needed.meanwhile,how could i do to analysis the user had selected which boxes because i need to store into table2.

<label>Area:</label>
 <input type="checkbox" name="area"> Ayer Keroh
 <input type="checkbox" name="area" >Bukit Beruang
 <input type="checkbox" name="area"> MITC
 <input type="checkbox" name="area">Malim Jaya
 <input type="checkbox" name="area">Kota Laksamana<br>
Others:
 <textarea rows="2" cols="50" name="comment" placeholder="Enter if area            is not listed" ></textarea> 
Dlayz
  • 53
  • 7
  • 2
    this is not clear at all. – Unex Dec 18 '17 at 11:12
  • It means on select you need to insert in database ? – TarangP Dec 18 '17 at 11:12
  • PLease be more specific – GYaN Dec 18 '17 at 11:14
  • I need those "ayer keroh,bukit beruang,mitc..." is from table area whereas when submit my form and i need to record which the user had selected and store into database. – Dlayz Dec 18 '17 at 11:16
  • so you want to populate your check boxes with data from database and then then insert user selection to the database? – Cezary Dec 18 '17 at 11:28
  • https://stackoverflow.com/questions/3921573/populate-checkbox-from-database this might help – Cezary Dec 18 '17 at 11:31
  • i saw that too but it doesnt fit to my problem.what i need is the checkboxes data is from table1 so i can add more area when needed.meanwhile,how could i do to analysis the user had selected which boxes because i need to store into table2. – Dlayz Dec 18 '17 at 11:39

1 Answers1

1
    <form action="test.php" method="post">
        <input type="checkbox" name="area[]" value="value 1">value 1
        <input type="checkbox" name="area[]" value="value 2">value 2
        <input type="checkbox" name="area[]" value="value 3">value 3
        <input type="checkbox" name="area[]" value="value 4">value 4
        <input type="checkbox" name="area[]" value="value 5">value 5
        <input type="checkbox" value="Other" name="area[]">Other<br>
        <textarea rows="2" cols="50" name="comment">write here
      </textarea>
        <input type="submit" />
        </form>
        <?php
        if(!empty($_POST['area'])) {
            foreach($_POST['area'] as $check) {
                    echo $check; //echoes the value set in the HTML form for each checked checkbox.
                                 //so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
                                 //in your case, it would echo whatever $row['Report ID'] is equivalent to.
        if($check=='other'){//if the user selected other checkbox
           echo$comment=$_REQUEST['comment'];
        }
            }
        }
        ?>

for more you can reffer to this Get $_POST from multiple checkboxes

how to retrive data from the sql

<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT AreaName FROM Areas";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<input type='checkbox' name='area[]' value='".$row['AreaName']."'>".$row['AreaName'];
    }
} else {
    echo "0 results";
}

$conn->close();

?>
Umar Majeed
  • 313
  • 3
  • 15