Cannot find out what problems,for example at the beginning i add patient details, i selected allergies and asthma and store into database,but i want to edit the details, it only checked asthma (last value)...please help me find out the answer Thank you.
<tr>
<td>Past Medical Records</td>
<td><?php
$DiseaseSplit = $medicalRec['Diseases'];
$array = explode(", ",$DiseaseSplit);
foreach ($array as $item) {
echo "<li>$item</li>";
}
?></td></tr>
<tr><label class="q" for="q1"></label>
<td><input name="q1[]" type="checkbox" value="NONE" <?php if($item == "NONE") { echo 'checked=\"checked\"';}?>>None</td></tr>
<tr>
<td><input name="q1[]" type="checkbox" value="ALLERGIES" <?php if($item == "ALLERGIES") { echo 'checked=\"checked\"';}?>>Allergies</td>
<td align="left"><input name="q1[]" type="checkbox" value="BLOOD DYSCRASIAS" <?php if($item == "BLOOD DYSCRASIAS") { echo 'checked=\"checked\"';}?>>Blood Dyscrasias</td></tr>
You're overwriting $item for every iteration of the loop.
I would try making a hash map to see which values should be shown/hidden:
<tr>
<td>Past Medical Records</td>
<td><?php
$data = array();
$DiseaseSplit = $medicalRec['Diseases'];
$array = explode(", ",$DiseaseSplit);
foreach ($array as $item) {
$data[$item] = true;
echo "<li>$item</li>";
}
?></td></tr>
<tr><label class="q" for="q1"></label>
<td><input name="q1[]" type="checkbox" value="NONE" <?php if(isset($data["NONE"])) { echo 'checked=\"checked\"';}?>>None</td></tr>
<tr>
<td><input name="q1[]" type="checkbox" value="ALLERGIES" <?php if(isset($data["ALLERGIES"])) { echo 'checked=\"checked\"';}?>>Allergies</td>
<td align="left"><input name="q1[]" type="checkbox" value="BLOOD DYSCRASIAS" <?php if(isset($data["BLOOD DYSCRASIAS"])) { echo 'checked=\"checked\"';}?>>Blood Dyscrasias</td></tr>
Related
I have a form in html like this, but it's error for checkbox question, someone please help me solve my problem.
It's multiple checkbox in multiple question
if(empty($tampil_pertanyaan)){
echo "<tr><td colspan=\"6\">Data tidak tersedia</td></tr>";
}else{
$no = 1; //for question
$no2 = 0; //for array
foreach($tampil_pertanyaan as $row)
{
<input type="hidden" name="user[]" id="user[]" value="<?php echo $data["user"]; ?>">
<tr>
<td><input type="checkbox" class="w3-check" name="ket_jawaban[<?php echo $no2;?>]" id="ket_jawaban[<?php echo $no2;?>]" value="<?php echo $row->pil1;?>" oninput="this.className = ''"></td>
<td> <?php echo $row->pil1;?></td>
</tr>
<tr>
<td><input type="checkbox" class="w3-check" name="ket_jawaban[<?php echo $no2;?>]" id="ket_jawaban[<?php echo $no2;?>]" value="<?php echo $row->pil2;?>" oninput="this.className = ''"></td>
<td> <?php echo $row->pil2;?></td>
</tr>
<tr>
<td><input type="checkbox" class="w3-check" name="ket_jawaban[<?php echo $no2;?>]" id="ket_jawaban[<?php echo $no2;?>]" value="<?php echo $row->pil3;?>" oninput="this.className = ''"></td>
<td> <?php echo $row->pil3;?></td>
</tr>
$no++;
$no2++;
}
And i my controller (i use Codeigniter)
public function insert_jawaban(){
// Proses pemvalidasian data yg di input
$this->form_validation->set_rules('ket_jawaban[]', 'ket_jawaban', 'trim|xss_clean');
if ($this->form_validation->run() == FALSE){
echo validation_errors(); // tampilkan apabila ada error
}else{
//Insert ke tabel jawaban
$result = array();
foreach($_POST['ket_jawaban'] AS $key => $val){
$result[] = array(
"id_jawaban" => '',
"user" => $_POST['user'][$key],
"ket_jawaban" => $_POST['ket_jawaban'][$key]
);
}
$res = $this->db->insert_batch('jawaban', $result);
}
why every time it returns one value, even though I checked three.
Firstly I apologize if there is any wrong in my question is because I am newbie here.
Thanks and please advise.
Through checkbox I want to pass & values. how can I do that. I can only pass one value. I am trying but its not working
while($row_tbl = mysqli_fetch_array($query))
{
<tr class="success" style="font-size:12px;">
<td > <input type="checkbox" name="check[]" class="chk_val" value=" <?php echo $row_tbl['Course_ID'] ?> " id="in" onclick="test()" /></td>
<td > <?php echo $row_tbl['Course_ID'] ?> </td>
<td> <?php echo $row_tbl['Course_Title'] ?> </td>
<td> <?php echo $row_tbl['Section'] ?> </td>
<td> <?php echo $row_tbl['Time'] ?> </td>
<td> <?php echo $row_tbl['Day'] ?> </td>
<td> <?php echo $row_tbl['Dept'] ?> </td>
<td> <?php echo $row_tbl['Capacity'] ?> /  0 </td>
<?php
}
To pass multiple values, you need to create multiple checkbox with the same name (as an array)
<input type="checkbox" name="check[]" class="chk_val" value="value1"/>
<input type="checkbox" name="check[]" class="chk_val" value="value2"/>
<input type="checkbox" name="check[]" class="chk_val" value="value3"/>
<input type="checkbox" name="check[]" class="chk_val" value="value4"/>
In your controller,
$values = $request->check; //the array of checked inputs.
Then you can loop through it
foreach($values as $value) {
...
}
Update
Accodring to the chat discussion you want to send 2 different fields per checkbox. In your checkboxes, put both fields like this
#foreach($courses as $course)
<input type="checkbox"
name="check[]"
class="chk_val"
value="{{ $course->id }}-{{ $course->section }}"/>
#endforeach
Pay attention to the value here. I added a delimiter - that will be useful in the controller. In controller now
public function selectedCourses(Request $request)
{
... //whatever you do for validation
//loop through selected courses
foreach( $request->check as $values ) {
$values = explode("-", $values); //split where we added the dash
$id = $values[0]; //the course ID
$section = $values[1]; // the course section
... do what you want here with that information
}
}
And voila!
Hey guys i have created a register form with an image upload too but when i try to update this form i try to get the id but the isset of my image is not working so it just wont run my update query do check it out
this is the updation form where all the values will be displayed for edit now can i run the update function in the isset condition of my submit button and then update the data
<title>Register Update</title>
<?php
//error_reporting(0);
$id=$_GET['id'];
function __autoload($classname)
{
include "$classname.php";
}
$obj = new connect();
$st=$obj->con();
if (isset($_POST['sub']))
{
$upd= new update();
$upd->updatedata($_POST);
}
$qry = "select * from register ";
$run = mysqli_query($st,$qry);
$row = mysqli_fetch_assoc($run);
{
$g = $row['gen'];
$l = $row['lang'];
}
$query=mysqli_query($st,"select * from register where id='$id'");
//echo "<ul>";
while($query2=mysqli_fetch_assoc($query))
{
//print_r($query2);
echo "<form method='POST' action='RegisterRetrieve.php'>";
echo "<table>";
?>
<p><input type="hidden" name="sid" value="<?php echo $query2['id']; ?>"></p>
<tr>
<td>
First Name:
</td>
<td><input type="text" name="uname" value="<?php echo $query2['uname']; ?>"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="pwd" value="<?php echo $query2['pwd']; ?>"></td>
</tr>
<tr>
<td>Email Id:</td>
<td><input type="text" name="emailid" value="<?php echo $query2['emailid']; ?>"
</td>
</tr>
<tr>
<td>Radio Button: Are you male or female?</td>
<?php
if ($g == "male"){
echo "<td><input type='radio' name='gen' value='Male' id='gen' checked> Male <input type='radio' name='gen' value='Female' id='gen'> Female </td>";
}
else
{
echo "<td><input type='radio' name='gen' value='Male' id='gen'> Male <input type='radio' name='gen' value='Female' id='gen' checked> Female </td>";
}
?>
</tr>
<tr>
<td>Check Box: Check the languages you know?</td>
<td><?php
$lang=explode(',',$l);
//print_r($lang);
if(in_array('Cricket', $lang))
echo '<input type="checkbox" name="lang[0]" value="Cricket" checked>Cricket';
else
echo '<input type="checkbox" name="lang[0]" value="Cricket">Cricket';
if(in_array('Basketball', $lang))
echo '<input type="checkbox" name="lang[1]" value="Basketball" checked>Basketball';
else
echo '<input type="checkbox" name="lang[1]" value="Basketball">Basketball';
if(in_array('Hockey', $lang))
echo '<input type="checkbox" name="lang[2]" value="Hockey" checked>Hockey';
else
echo '<input type="checkbox" name="lang[2]" value="Hockey">Hockey'."<br>";
?>
</td>
</tr>
<tr>
<td>Mobile No:</td>
<td><input type="text" name="mobile" value="<?php echo $query2['mobile']; ?>"
</td>
</tr>
<tr>
<td>10th Marks:</td>
<td><input type="text" name="marks_10" value="<?php echo $query2['10marks'];?>"
</td>
</tr>
<tr>
<td>
12th Marks:</td>
<td><input type="text" name="marks_12" value="<?php echo $query2['12marks'];?>"</td>
</tr>
<tr>
<td>
Browse Image:</td>
<td><input type="file" name="file1"></td>
<td><img src='img/<?php echo $query2['name'];?>' width='150px' height='150px'></td>
</tr>
<tr>
<td>
<select name="priority">
<option value="admin">
admin
</option>
<option value="<?php echo $query2['priority']; ?>"><?php echo $query2['priority']; ?>
</option>
<option value="superadmin">
superadmin
</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="submit" name="sub"><br>
</td>
</tr>
<?php
echo "<table>";
echo "</form>";
}
//echo "</ul>";
?>
now my update query which i m using but when i try to isset my image it just wont go in that condition
<?php
class update extends connect
{
function updatedata($rel)
{
$obj= new connect();
$obj->con();
extract($_POST);
$id=$_GET['id'];
$line = implode("," ,$lang);
print_r($_POST);
if(isset($_FILES["file1"]))
{
extract($_POST);
echo "hello";
$name = $_FILES['file1']['name'];
$type = $_FILES['file1']['type'];
$size = $_FILES['file1']['size'];
$tmp_name = $_FILES['file1']['tmp_name'];
$loc = 'img/';
$ext = substr($name,strpos($name,'.')+1);
if($_FILES['file1']['size']>= '10000' || $_FILES['file1']['size']<="23000000")
{
//echo $size;
}
else{
// echo "size is not supported";
}
$val = $_FILES['file1']['size'];
if($ext == 'jpg' || $ext == 'png')
{
//echo $lang;
//print_r($_POST);
//exit;
$val =("update register set uname='$uname',pwd='$pwd',emailid='$emailid',gen='$gen',lang='$line',mobile='$mobile',10marks='$marks_10',12marks='$marks_12' file1='$name' where id=$sid");
//print_r($qry);
$res=mysqli_query($this->con(),$val);
//print_r($run);
if($res)
{
move_uploaded_file($tmp_name,$loc.$name);
//echo "data saved";
//echo "Data inserted";
}
else
{
//echo "Data Not Inserted";
}
}
}
}
//print_r($val);
// return $res;
}
?>
Your html is broken:
</tr>
<?php
echo "<table>"; <--shouldn't this be </table>?
echo "</form>";
}
I have a form with multiple checkboxes that can be selected. If a user wanted they could go in and change the value in the checkbox. I want to know if there is a way I can not accept the form or display an error if the value submitted was not one of the original choices displayed. Here is my code:
HTML Code:
<form action="" method="post">
<table>
<tr>
<td>
<input type="checkbox" name="checkbox[]" value="1">
</td>
<td>
<input type="checkbox" name="checkbox[]" value="2">
</td>
<td>
<input type="checkbox" name="checkbox[]" value="3">
</td>
<td>
<input type="checkbox" name="checkbox[]" value="4">
</td>
<td>
<input type="checkbox" name="checkbox[]" value="5">
</td>
<td>
<input type="checkbox" name="checkbox[]" value="6">
</td>
</tr>
</table>
<input type="submit" name="submit" value="submit">
</form>
PHP Code:
<?php
if(!empty($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $check) {
echo $check;
}
}
If you want, since html is static (which I dont know why), you could initialize some original values, then compare it. Consider this example:
<?php
$original_values = array(1, 2, 3, 4, 5, 6);
if(isset($_POST['submit'], $_POST['checkbox'])) {
$submitted_values = $_POST['checkbox'];
foreach($submitted_values as $value) {
if(!in_array($value, $original_values)) {
// this particular submitted value does not belong to the original values that is set
echo 'not good';
exit;
}
}
// all good if it goes down here
echo 'all good';
}
?>
<form method="POST" action="">
<table border="0" cellpadding="10">
<tr>
<?php foreach($original_values as $value): ?>
<td><input type="checkbox" name="checkbox[]" value="<?php echo $value; ?>" /></td>
<?php endforeach; ?>
</tr>
<tr><td colspan="6"><input type="submit" name="submit" value="submit" /></td></tr>
</table>
</form>
Note: To test, try to change the values thru the browser [most likely F12], change the dom values. Example: Try to change a checkbox value attribute to 7 or 100, check it the submit.
Also another way:
$original_values = array(1, 2, 3, 4, 5, 6);
if(isset($_POST['submit'], $_POST['checkbox'])) {
$submitted_values = $_POST['checkbox'];
$differences = array_diff($submitted_values, $original_values);
if(count($differences) > 0) {
echo 'not good';
exit;
}
// all good if it goes down here
echo 'all good';
}
Print all the values inside a table:
if(isset($_POST['submit'], $_POST['checkbox'])) {
$submitted_values = $_POST['checkbox'];
$count = count($submitted_values);
echo "<table border='1' cellpadding='10'>";
echo "<tr><td colspan='$count'>Values</td></tr>";
echo "<tr>";
foreach($submitted_values as $value) {
echo "<td>$value</td>";
}
echo "</tr>";
echo "</table>";
}
Store values in a separate PHP file, checkbox_values.php:
<?php
$checkbox_values = array(1, 2, 3, 4, 5, 6);
?>
And your form, another PHP file, main.php:
<?php
require 'checkbox_values.php';
?>
<form method="POST" action="checkbox_process.php">
<table border="0" cellpadding="10">
<tr>
<?php foreach ( $checkbox_values as $check ) { ?>
<td><input type="checkbox" name="checkbox[]" value="<?php echo $check; ?>" /></td>
<?php } ?>
</tr>
<tr><td colspan="6"><input type="submit" name="submit" value="submit" /></td></tr>
</table>
</form>
And checkbox_process.php:
<?php
require 'checkbox_values.php';
if(!empty($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $check) {
if ( in_array($check, $checkbox_values) ) { echo $check; }
}
}
?>
I am trying to build a list where user can select any checkbox and it can send all select values for the row to the next page.
Here is what I have:
first page:
<table border="0">
<tr>
<td>Check All | Uncheck All</td>
<td>Item Name</td>
<td>Item Description</td>
</tr>
<?php
if ($num_rows == 0) {
return "No Data Found";
}else{
while ($row = dbFetchAssoc($result)) {
$item_id = $row['item_id'];
$item_name = $row['item_name'];
$item_desc = $row['item_desc'];
$item_qty = $row['item_qty'];
$item_upc = $row['item_upc'];
$item_price = $row['item_price'];
$vendor_id = $row['vendor_id'];
?>
<tr>
<td> <input type="checkbox" name="area[]" value="<?=$item_id?>" /></td>
<td><input type="text" name="item_name[]" value="<?=$item_name?>"></td>
<td><input type="text" name="item_desc[]" value="<?=$item_desc?>"></td>
</tr>
<?php
}
}
?>
<tr><td colspan="3"><input type="submit"></td></tr>
</table>
</form>
Next Page:
<table>
<tr><td colspan="3"><?php "Total Item(s) selected: "; echo count($_POST['area']); ?></td></tr>
<?php
if(!empty($_POST['area'])) {
foreach($_POST['area'] as $check) {
echo "<tr><td>".$check."</td><td>".$_POST['item_name']."</td><td>".$_POST['item_name']."</td></tr>";
}
}
?>
</table>
I need to read the item_name and item_desc value as well. How do I get it?
You need the key:
if(!empty($_POST['area'])) {
foreach($_POST['area'] as $key => $check) {
echo "<tr><td>".$check."</td><td>".$_POST['item_name'][$key]."</td><td>".$_POST['item_desc'][$key]."</td></tr>";
}
}
As Marc said in his answer it would probably be better to set key's in your loop creating the input.
<?php
if ($num_rows == 0) {
return "No Data Found";
}else{
$counter = 0;
while ($row = dbFetchAssoc($result)) {
$counter++;
$item_id = $row['item_id'];
$item_name = $row['item_name'];
$item_desc = $row['item_desc'];
$item_qty = $row['item_qty'];
$item_upc = $row['item_upc'];
$item_price = $row['item_price'];
$vendor_id = $row['vendor_id'];
?>
<tr>
<td> <input type="checkbox" name="area[<?=$counter?>]" value="<?=$item_id?>" /></td>
<td><input type="text" name="item_name[<?=$counter?>]" value="<?=$item_name?>"></td>
<td><input type="text" name="item_desc[<?=$counter?>]" value="<?=$item_desc?>"></td>
</tr>
<?php
}
}
?>
foreach($_POST['area'] as $key => $check) {
echo $check . $_POST['item_name'][$key];
}
assuming that there's an exact 1:1 correspondence between your three submitted arrays. Remember that checkboxes which are NOT checked do NOT get submitted with the form, so this is most likely NOT true and this code will not work as written.