onchange dropdown show checkbox is checked in php - php

code:
<script>
$(document).ready(function(){
$(".menu").click(function(){
ids = $('.menu:checked').map(function() {
return this.id;
}).get().join(',');
console.log(ids);
$("#ids").val(ids);
});
});
</script>
<?php
if(isset($_POST['submit']))
{
$adminid = $_POST['admin'];
$menuids = explode(",", $_POST['ids']);
foreach ($menuids as $idd)
{
$sql = "update menu set admin_id = concat(admin_id,'$adminid',',') where id = '$idd'";
$result = mysqli_query($link,$sql);
}
if($result == true)
{
$msg .= "<p style='color:green'>successfull</p>";
}
else
{
$msg .= "<p style='color:red'>error!</p>";
}
}
?>
<form method="post">
<select name="admin" id="admin">
<option value="">---Select Admin---</option>
<?php
$sql = "select * from admin";
$result = mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($result))
{
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['firstname']?></option>
<?php
}
?>
</select>
<table>
<tr>
<th>Share</th>
<th>Menu Name</th>
</tr>
<?php
$query = "select * from menu";
$results = mysqli_query($link,$query);
while ($fetch = mysqli_fetch_array($results))
{
?>
<tr>
<td>
<input type="checkbox" class="menu" id="<?php echo $fetch['id']; ?>" name="menuid" />
</td>
<td>
<?php echo $fetch['menu_name']; ?>
</td>
</tr>
<?php
}
?>
</table>
<input type="text" name="ids" id="ids" value=""/>
<input type="submit" name="submit" id="submit" />
</form>
In this code I am update a table having name menu in database. Now, I want to check only those checkbox where admin_id like ,1, or ,2, which is update by query. How can I fix this issue ?please please help.
Thank You

while ($fetch = mysqli_fetch_array($results))
{
?>
<tr>
<td>
<input type="checkbox" class="menu" value="<?php if($fetch['id']==1 or
$fetch['id']==2 ) { echo "checked";} else{} ?>" name="menuid" />
</td>
<td>
<?php echo $fetch['menu_name']; ?>
</td>
</tr>
<?php
}
?>

Related

How to check more than one checkbox if value for each checkbox exists in database else unchecked

I have three Table their structure is added down there. i want to show all role on one screen. and when click on one any role it will take you to next screen where (all the permission shown in check boxes) permission that is given to that role checked and other unchecked. and i want to that dynamically, as it can be given and taken by using check boxes. there check boxes should contain role_id and perm_id so it can b use for crud operations.table has many to many relation.
i have the role_id bu using the isset($_GET['role_id']) metthod and perm_id by calling a function that will give me the perm_id
<?php if (isset($_GET['role_id'])) {
$role_id = $_GET['role_id'];
} ?>
user_permission.php page
<?php $row = $role->allPermission($role_id); if(!empty($row)){
?>
<table class="table table-bordered table-striped" id="datatable-editable">
<thead>
<tr>
<th><center>Add</center></th>
<th><center>Update</center></th>
<th><center>Delete</center></th>
<th><center>View</center></th>
</tr>
</thead>
<tbody>
<tr>
<?php $all_permissions = $permission->allPermissions();
while ($row=mysqli_fetch_assoc($all_permissions)) {
$perm_id = $row['perm_id'];
$check_permission = $permission->checkPermission($role_id,$perm_id);
while ($row=mysqli_fetch_assoc($check_permission)){
?>
<td><center><input type="checkbox" perm_id="<?php echo $row['perm_id']?>" value="<?php echo $row['permission']?>"> <?php echo $row['permission']?>
</center>
</td>
<?php
}} ?>
</tr>
<tr>
<?php foreach ($row as $val) {?>
<td>
<?php if ($val['permission'] == 'add' || $val['permission'] == 'update' || $val['permission'] == 'delete' || $val['permission'] == 'view'){ ?>
<input type="checkbox" checked="checked" value="<?php echo $val['id']?>">
<?php } else { ?> <input type="checkbox" value="<?php echo $val['id']?>" > <?php }?>
</td>
<?php } ?>
<!-- foreach ($value as $key => $val) {
// echo $key . ' => ' . $val." / ";
echo $key['prem'];
} -->
<!-- <td>
<?php if (in_array('add', $row)){ ?>
<input type="checkbox" checked="checked" value="<?php echo $id?>">
<?php } else { ?> <input type="checkbox" > <?php }?>
</td>
<td>
<?php if (in_array('update', $row)){ ?>
<input type="checkbox" checked="checked">
<?php } else { ?> <input type="checkbox" > <?php }?>
</td>
<td>
<?php if (in_array('delete', $row)){ ?>
<input type="checkbox" checked="checked">
<?php } else { ?> <input type="checkbox" > <?php }?>
</td>
<td>
<?php if (in_array('view', $row)){ ?>
<input type="checkbox" checked="checked">
<?php } else { ?> <input type="checkbox" > <?php }?>
</td> -->
</tr>
</tbody>
</table>
allPermissions()
public function allPermissions()
{
$query="SELECT * from permission where status = 1";
$conn=$this->Connection();
$result = mysqli_query($conn,$query);
$num_rows = mysqli_num_rows($result);
if ($num_rows>0) {
return $result;
}
}
chechkPermission()
public function checkPermission($role_id,$perm_id)
{
$query = "select * from role_perm rp where role_id = '$role_id' and perm_id = '$perm_id' ";
$conn=$this->Connection();
$result = mysqli_query($conn,$query);
$num_rows = mysqli_num_rows($result);
if ($num_rows>0) {
return $result;
}
}
its been three(now) days i'm stuck here, please help. and sorry for my bad english
Table Structure
This is how i want it
you mean something like this? <input type="checkbox" <?= ($permission >= 3 ? "checked" : "") ?>

I am developing php page for update function

when page is loaded it display error that id, name and address is undefined. These variable are getting on condition of edit, but i dont understand how to place these values in text filed 'value' for edit as this is not working. The main code is like this.*** also the code of update is not updating values but it displays your record is updated
<?php
include 'server.php';
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;
$record = mysqli_query($con, "SELECT * FROM info WHERE id=$id");
if (count($record) == 1) {
$n = mysqli_fetch_array($record);
$name = $n['name'];
$address = $n['address'];
}
}
?>
<!-- Form -->
<!DOCTYPE html>
<html>
<head>
<title>CReate, Update</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form method="post" action="php_code.php" >
<input type="hidden" name="id" value="<?php echo $id; ?>">
<input type="text" name="name" value="<?php echo $name; ?>">
<input type="text" name="address" value="<?php echo $address; ?>">
<div class="input-group">
<?php if ($update == true): ?>
<button class="btn" type="submit" name="update" >Update</button>
<?php else: ?>
<button class="btn" type="submit" name="submit" >save</button>
<?php endif ?>
</div>
</form>
<!-- Display -->
<?php
$i = 1;
$q = mysqli_query($con, "select*from info");
while ($f = mysqli_fetch_array($q)) {
?>
<table>
<th> sr N0</th>
<th> Name</th>
<th> address</th>
<th> Action</th>
<tr>
<td> <?php echo $i; ?> </td>
<td> <?php echo $f['name']; ?> </td>
<td> <?php echo $f['address']; ?> </td>
<td><a href="index.php?edit=<?php echo $f['id']; ?>" class="edit_btn" >Edit</a></td>
</tr>
<?php $i ++;
}
?>
</table>
</body>
</html>
<?php
include 'server.php';
if (isset($_POST['save']))
{
$name= $_POST['name'];
$add= $_POST['address'];
$q = "INSERT into info (name,address) VALUES ('$name', '$add')";
mysqli_query($con, $q);
echo "inserted";
}
if (isset($_POST['update']))
{
$uname= $_POST['name'];
$uaddress= $_POST['address'];
$q = "UPDATE info set name= '$uname', adress= '$uaddress' WHERE id = $id";
mysqli_query($con, $q);
echo "updated";
}
?>
Make
if (count($record) == 1 ) {
to
if (mysqli_num_rows($record) == 1 ) {

how to refresh tabs data when update table?

index.php
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'Engineering')">Engineering</button>
<button class="tablinks" onclick="openCity(event, 'LAW')">LAW</button>
</div>
<div id="Engineering" class="tabcontent">
<table class="items">
<tr>
<th>State</th>
<th>College Name</th>
</tr>
<?php
$query = "select * from college where field = 'engineering'";
$show = mysqli_query($link,$query);
while ($fetch = mysqli_fetch_array($show))
{
?>
<tr>
<td><?php echo $fetch['state']?></td>
<td><?php echo $fetch['college_name']?></td>
<td>
edit
</td>
</tr>
<?php
}
?>
</table>
</div>
<div id="Law" class="tabcontent">
<table class="items">
<tr>
<th>State</th>
<th>College Name</th>
</tr>
<?php
$query = "select * from college where field = 'law'";
$show = mysqli_query($link,$query);
while ($fetch = mysqli_fetch_array($show))
{
?>
<tr>
<td><?php echo $fetch['state']?></td>
<td><?php echo $fetch['college_name']?></td>
<td>
edit
</td>
</tr>
<?php
}
?>
</table>
</div>
edit.php
<?php
if(isset($_POST['update']))
{
$college_name = $_POST['colleges'];
$state = $_POST['state'];
$sqli = "update college set college_name = '$college_name', state = '$state' where id = '$id'";
$results = mysqli_query($link,$sqli);
if($result == true)
{
$msg .= "<p style='color:green;'>Your data update successfully</p>";
}
else
{
$msg .= "<p style='color:red;'>Errror!</p>";
}
}
?>
<form method="POST" enctype="multipart/form-data" >
<select name="state" id="state">
<option value="<?php echo $stateid; ?>"><?php echo $statename; ?></option>
<option value="">Select State</option>
<?php
$sql = "select * from statemaster";
$result = mysqli_query($link,$sql);
while($row = mysqli_fetch_array($result))
{
echo "<option value=".$row['stateid'].">".$row['statename']."</option>";
}
?>
</select>
<select name="colleges" id="colleges">
<option value="<?php echo $college_name; ?>"><?php echo $college_name; ?></option>
<option value="">Select College</option>
</select>
<button type="submit" name='update' id='update'>update</button>
</form>
In this code when I click on edit button then it will go to edit.php page where I get id from url and run update query after updating table college the data will update but when I move from edit page to index.php page the data will remain same but in database update data will be there. So, How can I fix this issue ?
Thank You
Check for caching. It could be that the browser is not going to the server in order to get the contents of index.php, as it thinks it has it.
Try calling index.php with a variable, like:
Home

Insertion error (PHP) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I've been looking for a solution to this problem, but with no luck. Basically when inserting an item in a table it doesn't return a result (doesn't insert anything). I've checked the table fields and all match, I can't seem to find what error I'm doing.
Here's the portion of the code I currently have, where the problem occurs.
<br>
<h3><center><b>Components management</b></center></h3>
<br>
<form name = "registo" method = "POST" action = "">
<center><fieldset>
<legend>Insert component</legend>
<br>
<p><b> Nome: <input type = "text" name = "component_name" required></b> </p><br>
<p> <b>Type:</b> </p>
<div class = "mytable">
<table><tr>
<?php
$queryComp_Type = "select id, name from conc_type order by name";
$resultadoQueryComp_Type = mysqli_query($conn, $queryComp_Type);
foreach ($resultadoQueryComp_Type as $rowComp_Type)
{
?>
<th><?php echo $rowComp_Type['name']; ?></th>
<?php
}
?>
</tr><tr>
<?php
foreach ($resultadoQueryComp_Type as $rowComp_Type)
{
?>
<td><input class= "radio" type= "radio" name= "component_type" value= "<?php echo $rowComp_Type['id']; ?>" required> </td>
<?php
}
?>
</tr></table>
</div><br>
<p> <b>State:</b></p>
<table class="mytable">
<tr><th>Active</th><th>Inactive</th></tr>
<tr>
<td> <input class= "radio" type= "radio" name= "component_state" value= "active" required></td>
<td> <input class= "radio" type= "radio" name= "component_state" value= "inactive"></td>
</tr>
</table>
<br>
<!--Insert button-->
<input type= "hidden" name= "state" value= "inserir">
<input class= "button" type= "submit" value= "Inserir Componente">
<br><br>
</fieldset></center>
</form>
<?php
}
else if($_REQUEST['state'] == "inserir")
{
?>
<h3>Components management - inserçtion</h3>
<?php
$component_name = $_REQUEST['component_name'];
$component_type = $_REQUEST['component_type'];
$component_state = $_REQUEST['component_state'];
$insert = sprintf("INSERT INTO `concept` (`name`,`conc_type_id`,`state`) VALUES ('%s', '$component_type', '%s')", mysqli_real_escape_string($conn, $component_name),
mysqli_real_escape_string($conn, $component_state));
$resultado_insert = mysqli_query($conn, $insert);
if($resultado_insert)
{
mysqli_query($conn,'COMMIT');
?>
<p>Insert successfuly</p>
<p>Click in Continue to advance.</p>
<?php
}
else
{
mysqli_query($conn,'ROLLBACK');
?>
<p>Insertion error.</p>
<?php
}
back();
}
}
?>
If anyone could help I'd be really grateful!
EDIT: Changed $$component_name to $component_name. (Typo)
EDIT: Full page code.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css"href="/custom/css/ag.css">
</head>
<body>
<?php
require_once("custom/php/common.php");
if(!is_user_logged_in() && !current_user_can('manage_components'))
{
echo "You don't have permission to access this page";
}
else
{
if($_REQUEST['state'] == "")
{
$verify_component = "SELECT * FROM concept";
$result_component = mysqli_query($conn, $verify_component);
if(mysqli_num_rows($result_component) == 0)
{
echo "Não há componentes";
}
else
{
?>
<table class="mytable">
<thead><tr>
<th>Type</th>
<th>ID</th>
<th>Name</th>
<th>State</th>
<th>Ação</th>
</tr></thead>
<!--<tbody>-->
<?php
$choice_comp_type = "SELECT * FROM conc_type";
$resultado_comp_type = mysqli_query($conn, $choice_comp_type);
while($type = mysqli_fetch_array($resultado_comp_type))
{
$choice_components = "SELECT component.*
FROM concept,conc_type
WHERE conc_type.id = concept.conc_type_id
AND conc_type_id = '".$type['id']."'";
$result_components = mysqli_query($conn, $choice_components);
$num_rows = mysqli_num_rows($result_components);
if($num_rows > 0)
{
?>
<tr>
<td class= "Name" colspan = "1" rowspan = "<?php echo $num_rows; ?>"> <?php echo $type['name']; ?> </td>
<?php
while($row = mysqli_fetch_array($result_components))
{
?>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<?php
if($row['state'] == "active")
{
?>
<td>active</td>
<td>[edit] [desactivate]</td>
<?php
}
else
{
?>
<td>inactive</td>
<td>[edit] [activate]</td>
<?php
}
?>
</tr>
<?php
}
}
}
?>
<!--</tbody>-->
</table>
<?php
}
?>
<br>
<h3><center><b>Management Compontent - introduction</b></center></h3>
<br>
<form name = "registo" method = "POST" action = "">
<center><fieldset>
<legend>Insert component</legend>
<br>
<p><b> Name: <input type = "text" name = "component_name" required></b> </p><br>
<p> <b>Type:</b> </p>
<div class = "mytable">
<table><tr>
<?php
$queryComp_Type = "select id, name from conc_type order by name";
$resultadoQueryComp_Type = mysqli_query($conn, $queryComp_Type);
foreach ($resultadoQueryComp_Type as $rowComp_Type)
{
?>
<th><?php echo $rowComp_Type['name']; ?></th>
<?php
}
?>
</tr><tr>
<?php
foreach ($resultadoQueryComp_Type as $rowComp_Type)
{
?>
<td><input class= "radio" type= "radio" name= "component_type" value= "<?php echo $rowComp_Type['id']; ?>" required> </td>
<?php
}
?>
</tr></table>
</div><br>
<p> <b>State:</b></p>
<table class="mytable">
<tr><th>active</th><th>Inactive</th></tr>
<tr>
<td> <input class= "radio" type= "radio" name= "component_state" value= "active" required></td>
<td> <input class= "radio" type= "radio" name= "component_state" value= "inactive"></td>
</tr>
</table>
<br>
<!--Botão Inserir-->
<input type= "hidden" name= "state" value= "inserir">
<input class= "button" type= "submit" value= "Inserir Componente">
<br><br>
</fieldset></center>
</form>
<?php
}
else if($_REQUEST['state'] == "inserir")
{
?>
<h3>Management component - insertion</h3>
<?php
$component_name = $_REQUEST['component_name'];
$component_type = $_REQUEST['component_type'];
$component_state = $_REQUEST['component_state'];
$insert = sprintf("INSERT INTO `concept` (`name`,`conc_type_id`,`state`) VALUES ('%s', '%s', '%s')", mysqli_real_escape_string($conn, $component_name), mysqli_real_escape_string($conn, $component_type),
mysqli_real_escape_string($conn, $component_state));
$resultado_insert = mysqli_query($conn, $insert);
if($resultado_insert)
{
mysqli_query($conn,'COMMIT');
?>
<p>Successfull insertion</p>
<p>Click in Continue to advance.</p>
<?php
}
else
{
mysqli_query($conn,'ROLLBACK');
?>
<p>Insertion error</p>
<?php
}
back();
}
}
?>
</body>
</html>
First when you create a table... get a result of query... i think should be like this :
<div class = "mytable">
<?php
$queryComp_Type = "select id, name from conc_type order by name";
$resultadoQueryComp_Type = mysqli_query($conn, $queryComp_Type);
$tableHeader = "";
$tableBody = "";
?>
<table>
<thead>
<?php
while ($row = mysqli_fetch_array($resultadoQueryComp_Type)) {
$tableHeader .= '<tr>
<th>'.$row['name'].';</th>
</tr>';
}
?>
<?=$tableHeader?>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($resultadoQueryComp_Type)) {
$tableBody .= '<tr>
<td>'.$row['id'].'</td>
</tr>';
}
?>
<?=$tableBody?>
</tbody>
</table>
</div>

Checkbox Data Dynamically Save to Database on Click

I need some js/ajax/jquery script saving data to database dynamically when I check the check-box.
the checkboxes at the moment or loaded in next to records and change the variable in the database depending if its checked or not.but i have to reload the page after i select one to save it to the database. i can do everything else but understand how to implement the ajax to this so i don't have to submit the query and refresh the page every time. any help is greatly appreciated.
<form name="form1aa" method="post" action="process.php?fn=<? echo $rows['first']; ?>&class=<?php echo $rows['class']; ?>&last=<?php echo $rows['last']; ?>
&model=<?php echo $rows['model']; ?>&cas=<?php echo $rows['cases']; ?>&upid=<?php echo $id; ?>&group=1" id="form1a" >
<select name="type" onchange=" fill_damage (document.form1aa.type.selectedIndex); ">
<option value="Hardware">Hardware</option>
<option value="Software">Software</option>
</select>
<select name="damage">
</select>
<input type=text name="comment" placeholder="Comments Box">
<input type=text name="cost" placeholder="Cost">
<input type="submit" value="Save" name="Save">
</form>
<?php
//Job Status
if(isset($_POST['checkbox'])){$checkbox = $_POST['checkbox'];
if(isset($_POST['activate'])?$activate = $_POST["activate"]:$deactivate = $_POST["deactivate"])
$id = "('" . implode( "','", $checkbox ) . "');" ;
$sql="UPDATE repairs SET status = '".(isset($activate)?'Completed':'In Progress')."' WHERE id=$id" ;
$result = mysql_query($sql) or die(mysql_error());
}
//End Job Status
//Payment Status
if(isset($_POST['paycheck'])){$paycheck = $_POST['paycheck'];
if(isset($_POST['paid'])?$paid = $_POST["paid"]:$unpaid = $_POST["unpaid"])
$id = "('" . implode( "','", $paycheck ) . "');" ;
$sql="UPDATE repairs SET paid = '".(isset($paid)?'Paid':'Unpaid')."' WHERE id=$id" ;
$result = mysql_query($sql) or die(mysql_error());
}
//End Payment Status
//Return Status
if(isset($_POST['retcheck'])){$retcheck = $_POST['retcheck'];
if(isset($_POST['ret'])?$ret = $_POST["ret"]:$unret = $_POST["unret"])
$id = "('" . implode( "','", $retcheck ) . "');" ;
$sql="UPDATE repairs SET ret = '".(isset($ret)?'Retuned':'In Office')."' WHERE id=$id" ;
$result = mysql_query($sql) or die(mysql_error());
}
//End Return Status
$sql="SELECT * FROM $tbl_name";
if(isset($_POST['all'])){
$sql="SELECT * FROM $tbl_name";
}
if(isset($_POST['tpc'])){
$sql="select * from $tbl_name WHERE class LIKE '1%'";
}
if(isset($_POST['drc'])){
$sql="select * from $tbl_name WHERE class LIKE 'D%'";
}
if(isset($_POST['bsc'])){
$sql="select * from $tbl_name WHERE class LIKE 'B%'";
}
$result=mysql_query($sql);
?>
<form name="frmactive" method="post" action="">
<input name="activate" type="submit" id="activate" value="Complete Job" />
<input name="paid" type="submit" id="Payment" value="Payment Status" />
<input name="ret" type="submit" id="ret" value="Returned 2 Student" />
<br />
<a id="displayText" href="javascript:toggle();">Show Extra</a>
<div id="toggleText" style="display: none">
<br />
<input name="unret" type="submit" id="unret" value="In Office" />
<input name="unpaid" type="submit" id="unpaid" value="Not Paid" />
<input name="deactivate" type="submit" id="deactivate" value="In Progress" /></div>
<table width="1000" border="0" cellpadding="3" cellspacing="1">
<thead>
<th width="67" align="center"><strong>Start Date</strong></th>
<th width="50" align="center"><strong>Cases</strong></th>
<th width="34" align="center"><strong>Type</strong></th>
<th width="120" align="center"><strong>Damage</strong></th>
<th width="31" align="center"><strong>Comment</strong></th>
<th width="31" align="center"><strong>Cost</strong></th>
<th width="90" align="center"><strong>Payment Status</strong></th>
<th width="100" align="center"><strong>Returned 2 Student</strong></th>
<th width="100" align="center"><strong>Job Status</strong></th>
</thead>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['start']; ?></td>
<td><? echo $rows['cases']; ?></td>
<td><? echo $rows['type']; ?></td>
<td width="70"><? echo $rows['damage']; ?></td>
<td width="70"><? echo $rows['comment']; ?></td>
<td><? echo "$"; echo $rows['cost']; ?></td>
<!--Payment Display(Start)-->
<?php
if($rows['paid']=="Paid")
{
?>
<td><input name="paycheck[]" type="checkbox" id="paycheck[]" value="<? echo $rows['id']; ?>">
<? echo $rows['paid'];?>
</td>
<?
}
if($rows['paid']=="Unpaid")
{
?>
<td width="21"><input name="paycheck[]" type="checkbox" id="paycheck[]" value="<? echo $rows['id']; ?>">
<? echo $rows['paid']; ?>
</td>
<?
}
if($rows['ret']==""){
?>
<td width="50">No Data</td>
<?
}
?>
Do it with jQuery, a simple example could be:
HTML:
<input type="checkbox" name="option1" value="Milk">
<input type="checkbox" name="option2" value="Sugar">
<input type="checkbox" name="option3" value="Chocolate">
JS:
$("input[type='checkbox']").on('click', function(){
var checked = $(this).attr('checked');
if(checked){
var value = $(this).val();
$.post('file.php', { value:value }, function(data){
// data = 0 - means that there was an error
// data = 1 - means that everything is ok
if(data == 1){
// Do something or do nothing :-)
alert('Data was saved in db!');
}
});
}
});
PHP: file.php
<?php
if ($_POST && isset($_POST['value'])) {
// db connection
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
// error happened
print(0);
}
mysql_select_db('mydb');
// sanitize the value
$value = mysql_real_escape_string($_POST['value']);
// start the query
$sql = "INSERT INTO table (value) VALUES ('$value')";
// check if the query was executed
if(mysql_query($sql, $link)){
// everything is Ok, the data was inserted
print(1);
} else {
// error happened
print(0);
}
}
?>
Simple put...
$('input:checkbox').click( function() {
clicked = $(this).attr('checked');
if (clicked) {
/* AJAX the server to tell them it was clicked. */ }
else {
/* AJAX the server to tell them it was unclicked. */ } } );
I can make this even simpler. first, you need to ad a checkbox!!
<form name="form1aa" method="post" action="process.php?fn=<? echo $rows['frist']; ?>&class=<?php echo $rows['class']; ?>&last=<?php echo $rows['last']; ?>
&model=<?php echo $rows['model']; ?>&cas=<?php echo $rows['cases']; ?>&upid=<?php echo $id; ?>&group=1" id="form1a" >
<select name="type" onchange="fill_damage(document.form1aa.type.selectedIndex);">
<option value="Hardware">Hardware</option>
<option value="Software">Software</option>
</select>
<select name="damage">
</select>
<input type="text" name="comment" placeholder="Comments Box">
<input type="text" name="cost" placeholder="Cost">
<input type="checkbox" name="somecheck" onchange="if(this.checked)document.form1aa.submit()">Check this to Save.
<input type="submit" value="Save" name="Save">
</form>
<script type="javascript>
//another function that works for onchange="dosubmit(this)"
//IF you put it after the form.
function dosubmit(el) {
if (el.checked) {
document.form1aa.submit();
}
}
</script>
get rid of the spaces in your onchange events where possible.
If you have dynamic checkbox list and you want to dynamically save the clicked one to database or inserting the unchecked one, here how to do that:
Html/PHP
<?php
// $checklists are models that I am getting from db
$checklists = CheckList::getCheckLists(3);
echo '<ul>';
foreach ($checklists as $checklist) {
$isChecked = $checklist->getAnswer($requestID, $checklist->primaryKey);
$checked = $isChecked ? "checked" : "";
echo '<li>';
echo "<input id='{$checklist->primaryKey}'
name='{$checklist->primaryKey}' type='checkbox' {$checked}
value='{$isChecked}' data-request-id='{$requestID}'>
$checklist->check_list_text";
echo '</li>';
}
echo '</ul>';
?>
Jquery
<script>
$("input[type='checkbox']").on('click', function(){
var checkbox = $(this);
var checked = checkbox.prop('checked');
var checklistId = checkbox.attr("id");
$.ajax({
url:"<?= Url::to(['default/add-checklist-answer']) ?>",
// I don't need to write the type here because I am using Yii Framework
// type: 'post',
data: {
checklistId: checklistId,
requestId: checkbox.data('request-id'),
checked: checked
},
success: function(data) {
//alert(data);
console.log(data.firstMessage)
},
error: function(data) {
// alert(data);
}
});
});
</script>
I hope it will work for MVC users.

Categories