javascript function check box values without submit button in php - javascript

I want to insert the checkbox values without submit button but i cant get my checkbox values on my jquery code please get me over this problem which jquery function i have to use to get my values i'm new this script
<?php
date_default_timezone_set('Asia/Kolkata');
$a=date('H');
$b=8;$c=15;
if($b<$a&&$a<=$c)
{
$status='check="unchecked"';
}
else
{
$status = 'disabled="disabled"';
}
?>
<form >
<div align="center">
<table>
<tr>
<td>
<p>S.NO</p>
</td>
<td>
<p>Check Box</p>
</td>
<td>
<p>Activity Name</p>
</td>
</tr>
<tr>
<td>
1
</td>
<td>
<input type="checkbox" class="get_value" value="Bod1" <?php echo $status ?> />
</td>
<td>
<label for="Bod1">BOD1 </label>
</td>
</tr>
<tr>
<td>
2
</td>
<td>
<input type="checkbox" class="get_value"value="Bod2"<?php echo $status ?> />
</td>
<td>
<label for="Bod2">BOD2 </label>
</td>
</tr>
<tr>
<td>
3
</td>
<td>
<input type="checkbox" class="get_value" value="Bod3"<?php echo $status ?>/>
</td>
<td>
<label for="Bod3">BOD3</label>
</td>
</tr>
<tr>
<td>
4
</td>
<td>
<input type="checkbox" class="get_value" value="Bod4" <?php echo $status ?>/>
</td>
<td>
<label for="Bod4">BOD4 </label>
</td>
</tr>
<tr>
<td>
5
</td>
<td>
<input type="checkbox" class="get_value" value="Bod5" <?php echo $status ?> />
</td>
<td>
<label for="Bod5">BOD5 </label>
</td>
</tr>
</div>
</div>
</form>
<script>
$(document).ready( function () {
var languages=[];
//$.each($("input[name='checklist[]']:checked"), function(){
/ alert($(this).val());
// $("input[name='checklist[]']").each( function () {
var checkedvalue=$('.check_list:checked').val();
var checkedvalue=null;
var inputelements=document.getelementbyclassname('check_list');
for(var i=0;inputelements[i];i++){
if(inputelements[i].checked){
checkedvalue=inputelements[i].value;
});
checkedvalue=checkedvalue.toString();
$.ajax({
url:"dummyeodcheckbox.php";
type:"post"
data:{languages:languages};
success:function(data){
var result=result.data;
alert("function called successfully");
}
});
});
});
</script>

check this , hope it will help you. this is your html code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" name="form1">
<input type="checkbox" name="checkboxList[]" value="1">value1</input>
<input type="checkbox" name="checkboxList[]" value="2">value2</input>
<input type="checkbox" name="checkboxList[]" value="3">value3</input>
</form>
<script>
$("input[type=checkbox]").on("change", function () {
var ids = [];
$('input[type=checkbox]:checked').each(function () {
ids.push($(this).val());
});
$.ajax({
url: "test.php",
type: "POST",
async: true,
cache: false,
data: ({value: ids}),
dataType: "text",
success: function (data) {
alert(data)
}
});
});
</script>
then create test.php file
<?php
print_r($_POST['value']);
?>
in test.php file make your action.

Related

Get data-id from url using javascript ajax

i've got stuck to get the data-id value from url. After user click the edit link from the url, i want to show the data in the modal dialog. I use the ajax script, but it doesnt work clearly. The modal dialog show an error warning.
This is my class_view.php
<script>
$(document).ready(function(){
$(".edit").on('click', function(e){
var id = $(e.relatedTarget).data('id');
$.ajax({
type: 'post',
url: "class/edit_class_form.php",
data: 'id=' + id,
success: function(data){
$('.show_class').html(data);
}
});
});
});
</script>
<table width="40%">
<thead>
<tr>
<th>No.</th>
<th>Kode</th>
<th>Name</th>
<th>Capacity</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$query = $db->prepare("SELECT * FROM class ORDER BY name ASC");
$query->execute();
for($no=1; $data = $query->fetch();){
?>
<tr>
<td><?php echo $no++;?></td>
<td><?php echo $data['code'];?></td>
<td><?php echo $data['type']." ".$data['name'];?></td>
<td><?php echo number_format($data['capacity'], 2, '.', ',');?> Ton</td>
<td>edit
</td>
</tr>
<?php };?>
</tbody>
</table>
</div>
<?php
include 'setting/class/edit_class_form.php';
?>
I use pure css modal. This is the code of my edit_class_form.php
<div id="edit_form" class="modal">
<div class="md-medium">
X
<h2>Edit Class</h2>
<form method="post" action="setting/class/edit_class_update.php" id="form" autocomplete="off">
<div class="md-content">
<table class="show_class">
<?php
require '../../inc/koneksi.php';
if($_POST['id']){
$id=$_POST['id'];
$stmt = $db->prepare("SELECT * FROM class WHERE id=:id");
$stmt->execute(array(':id' => $id));
?>
<?php while($row=$stmt->fetch(PDO::FETCH_ASSOC)){?>
<tr>
<td>Class Code</td>
<td><input type="text" name="code" size="5" maxlength="3" value="<?php echo $row['code'];?>" required></td>
</tr>
<tr>
<td>Class Type</td>
<td><input type="text" name="type" size="10" maxlength="5" value="<?php echo $row['type'];?>" required></td>
</tr>
<tr>
<td>Class Name</td>
<td><input type="text" name="name" size="40" maxlength="35" value="<?php echo $row['name'];?>" required></td>
</tr>
<tr>
<td>Capacity</td>
<td><input type="text" name="capacity" size="15" value="<?php echo $row['capacity];?>"maxlength="11" required> Persons</td>
</tr>
</table>
<?php }}?>
</div>
<div class="md-footer">
Cancel
<input type="submit" value="insert"">
</div>
</form>
</div>
</div>
Can you help me?
You can get the data-id of an element in this way then you can add the value to the url for your ajax call and your data should be an object
data: {id: id}
You can also var_dump($_POST) to check what are you receiving in PHP
$(document).ready(function() {
$(".edit").on('click', function(e) {
$this = $(this);
const id = $this.data("id")
$.ajax({
type: 'post',
url: "class/edit_class_form.php",
data: {
id: id
},
success: function(data) {
$('.show_class').html(data);
}
});
});
});
.edit {
cursor: pointer;
background: green;
width: 90px;
padding: 20px;
text-align: center;
color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="edit" data-id="5445454">Click me </div>

php notice database array error

If I press on my post button the data of my form will be send to the database but I get 2 Array to string error's. I don't know what I have to change to fix these errors.
Here is also a picture of the data in my database where it is showing arrays.
https://gyazo.com/f7463825b6ae2e44812d5149d55e9011
I hope someone can help me! I am learning :P
Here is my code example.
<html>
<title>HOTEL</title>
<center>
<head>
<h2>
<?php echo "Welkom terug " . $_SESSION["username"] . ""?>
</br>
<a href='logout.php'>Log uit</a></br>
</h2>
<hr>
<h1>Film toevoegen</h1>
<hr>
</head>
<body>
<form method="POST">
<table>
<tr>
<td>Film:</td>
<td><input type="text" name="txtFilm"></td>
</tr>
<tr>
<td>Genre:</td>
<td>
<select name="Genre">
<option value="Actie">Actie</option>
<option value="Fantasie">Fantasie</option>
<option value="Horror">Horror</option>
<option value="Avontuur">Avontuur</option>
<option value="Komedie">Komedie</option>
<option value="Romantiek">Romantiek</option>
<option value="Historisch">Historisch</option>
</select>
</td>
</tr>
<tr>
<td>Film omschrijving:</td>
<td><input type="text" name="txtFilmomschrijving"></td>
</tr>
<tr>
<td>Datum van uitkomst: </td>
<td><input type="date" name="uitkomstdate">
</td>
</tr>
<tr>
<td>Datum uit bioscoop: </td>
<td><input type="date" name="BiosDate">
</td>
</tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr>
<td>Film draai dagen: </td>
<td>
<input type="checkbox" value="Maandag" name="draaidag[]">Maandag
<input type="checkbox" value="Dinsdag" name="draaidag[]">Dinsdag
<input type="checkbox" value="Woensdag" name="draaidag[]">Woensdag
<input type="checkbox" value="Donderdag" name="draaidag[]">Donderdag
<input type="checkbox" value="Vrijdag" name="draaidag[]">Vrijdag
<input type="checkbox" value="Zaterdag" name="draaidag[]">Zaterdag
<input type="checkbox" value="Zondag" name="draaidag[]">Zondag
</td>
</tr>
<tr>
<td>Film tijd dagen: </td>
<td>
<input type="checkbox" value="12uur" name="tijddagen[]">12:00
<input type="checkbox" value="14uur" name="tijddagen[]">14:00
<input type="checkbox" value="16uur" name="tijddagen[]">16:00
<input type="checkbox" value="18uur" name="tijddagen[]">18:00
<input type="checkbox" value="20uur" name="tijddagen[]">20:00
<input type="checkbox" value="22uur" name="tijddagen[]">22:00
<input type="checkbox" value="24uur" name="tijddagen[]">24:00
</td>
</tr>
<tr>
<td>Zaal: </td>
<td>
<select name="zaal">
<option value="normaal">Normaal</option>
<option value="groot">Groot</option>
</select>
</td>
</tr>
<tr></tr>
<tr></tr>
<tr></tr>
<td></td>
<td>
<input type="submit" value="Film toevoegen" name="btnHuur" />
</td>
</tr>
</table>
</form>
<hr>
<?php
if(isset($_POST['btnHuur']))
{?>
<table>
<th>
<h2>Toegevoegde film:</h2>
</th>
<?php
if(isset($_POST['btnHuur']))
{
?>
<tr>
<td>
<?php echo 'Film: '; ?>
</td>
<td>
<?php echo $_POST['txtFilm']; ?>
</td>
</tr>
<tr>
<td>
<?php echo 'Genre: '; ?>
</td>
<td>
<?php echo $_POST['Genre']; ?>
</td>
</tr>
<tr>
<td>
<?php echo 'Film omschrijving: '; ?>
</td>
<td>
<?php echo $_POST['txtFilmomschrijving'];?>
</td>
</tr>
<tr>
<td>
<?php echo 'Datum van uitkomst: ';?>
</td>
<td>
<?php echo $_POST['uitkomstdate']; ?>
</td>
</tr>
<tr>
<td>
<?php echo 'Datum uit bioscoop: ';?>
</td>
<td>
<?php echo $_POST['BiosDate']; ?>
</td>
</tr>
<tr>
<td>
<?php echo 'Film draai dagen: ';?>
</td>
<td>
<?php
if(!empty($_POST['draaidag']))
{
foreach($_POST['draaidag']as$check)
{
echo " ".$check;
$draaidag = " ".$check;
}
?>
</td>
</tr>
<tr>
<td>
<?php echo 'Film tijd dagen: ';?>
</td>
<td>
<?php
if(!empty($_POST['tijddagen']))
{
foreach($_POST['tijddagen']as$check)
{
echo " ".$check;
$tijddagen = " ".$check;
}
}
?>
</td>
</tr>
<tr>
<td>
<?php echo 'zaal: '; ?>
</td>
<td>
<?php echo $_POST['zaal']; ?>
</td>
</tr>
<?php
}else
{
echo "Geen";
}
include("loginDB.php");
?>
</br>
</br>
</br>
<?php
$film = $_POST['txtFilm'];
$genre = $_POST['Genre'];
$Filmomschrijving = $_POST['txtFilmomschrijving'];
$Datumvanuitkomst = $_POST['uitkomstdate'];//$_POST['selDag'] + '-' + $_POST['selMaand'] + '-' + $_POST['selJaar'];
$Datumuitbioscoop = $_POST['BiosDate'];//$_POST['selDag'] + '-' + $_POST['selMaand'] + '-' + $_POST['selJaar'];
$Filmdraaidagen = $_POST['draaidag'];
$Filmtijddragen = $_POST['tijddagen'];
$Zaal = $_POST['zaal'];
?>
<tr></tr>
<tr>
<td>
<?php
if($film && $genre && $Filmomschrijving && $Datumvanuitkomst && $Filmdraaidagen && $Filmtijddragen && $Zaal)
{
$query = "INSERT INTO films VALUES (0, '$film', '$genre', '$Filmomschrijving', '$Datumvanuitkomst', '$Datumuitbioscoop', '$Filmdraaidagen', '$Filmtijddragen', '$Zaal')";
mysqli_query($db, $query);
echo "Bestelling voltooid!";
}
}
?>
</td>
</tr>
</table>
<hr>
<?php } ?>
</body>
</center>
</html>
There is some mistake in your Php Code,
Html You had define name as Array (draaidag[] && tijddagen[])
<tr>
<td>Film draai dagen: </td>
<td>
<input type="checkbox" value="Maandag" name="draaidag[]">Maandag
<input type="checkbox" value="Dinsdag" name="draaidag[]">Dinsdag
<input type="checkbox" value="Woensdag" name="draaidag[]">Woensdag
<input type="checkbox" value="Donderdag" name="draaidag[]">Donderdag
<input type="checkbox" value="Vrijdag" name="draaidag[]">Vrijdag
<input type="checkbox" value="Zaterdag" name="draaidag[]">Zaterdag
<input type="checkbox" value="Zondag" name="draaidag[]">Zondag
</td>
</tr>
<tr>
<td>Film tijd dagen: </td>
<td>
<input type="checkbox" value="12uur" name="tijddagen[]">12:00
<input type="checkbox" value="14uur" name="tijddagen[]">14:00
<input type="checkbox" value="16uur" name="tijddagen[]">16:00
<input type="checkbox" value="18uur" name="tijddagen[]">18:00
<input type="checkbox" value="20uur" name="tijddagen[]">20:00
<input type="checkbox" value="22uur" name="tijddagen[]">22:00
<input type="checkbox" value="24uur" name="tijddagen[]">24:00
</td>
</tr>
You need to use implode() while accessing from $_POST. to convert it into string.
$Filmdraaidagen = implode("," ,$_POST['draaidag']);
$Filmtijddragen = implode(",",$_POST['tijddagen']);

Need to implement ajax to change value of input field on change in dropdown

I'm populating row of inputs in my form. Its changes only one row. Since ajax uses id to display the php file output. But I need to change the value on each row...
An early response will be highly appreciated.
function getAttendanceSub(categoryName) {
$.ajax({
type: "GET",
url: "getAttendanceSub.php",
data: "CategoryName=" + categoryName,
success: function(result) {
$("#subCategory").html(result);
}
});
};
html
$sql_category1 = "SELECT id, unit FROM unitmaster WHERE deleted = 'no' ";
// echo $sql_batch;
$res_category1 = mysql_query($sql_category1);
while ($row_category1 = mysql_fetch_assoc($res_category1)){
$vall = $row_category1["id"];
$captionn = $row_category1["unit"];
?>
<option value="<?php echo $captionn ?>"><?php echo $captionn ?></option>
<?php } ?>
</select>
</TD>
<TD width="10%">
<input type="text" name="txtbox2[]" />
</TD>
<TD width="10%"> <INPUT type="text" name="txtbox3[]" /> </TD>
<TD width="10%"> <INPUT type="text" name="txtbox4[]" /> </TD>
<!--<TD stylestyle="width:20px;"="width:50px;"> <INPUT type="checkbox" name="chkbox[]"/></TD>-->
</TR>
<?php
$i++;
?>
</TABLE>
<br/>
<INPUT type="button" value="Add" class="button" onClick="addRow('dataTable')" />
<!--<INPUT type="button" value="Delete" onClick="deleteRow('dataTable')" />-->
</div>
</div>
<br/>
</div><br/><br/>
<tr>
<td align="center"><input type="submit" value="submit" name="submit"/></td>
</tr>
</div>
</div>
</div>

Dynamically increasing radio button validation

I have some radio buttons that dynamically increase based on data. What I want is to check if all radio button are selected or not. If they're not, I want to show an error message on that particular row as I am using form substitution to send data to ajax.
$(document).ready(function() {
// radio buttons
$('#mySpan4').submit(function(e) {
e.preventDefault();
$.ajax({
url: 'ajax2.php',
data: $(this).serialize(), // reads the data ...
success: function(data) {
alert("data Updated Successfully");
window.location = 'login.html';
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="mySpan4">
<table>
<tr>
<td>
Name : abc
</td>
<td>
<input type="radio" name="game[0]" value="Mother">Mother
</td>
<td>
<input type="radio" name="game[0]" value="Father">Father
</td>
</tr>
<tr>
<td>
Name : def
</td>
<td>
<input type="radio" name="game[1]" value="Mother">Mother
</td>
<td>
<input type="radio" name="game[1]" value="Father">Father
</td>
</tr>
<tr>
<td>
Name : ghi
</td>
<td>
<input type="radio" name="game[2]" value="Mother">Mother
</td>
<td>
<input type="radio" name="game[2]" value="Father">Father
</td>
</tr>
</table>
<input name="submit" type="submit" id="submit" value="submit">
</form>
You may try using :checked
var all = $('input[type=radio][name^=game]').length;
var chk = $('input[type=radio][name^=game]:checked').length;
var exp = all / 2;// in a group two radios each, any one will be selected
if(chk === exp) {
//all checked
} else {
// some goup is not checked
}
$(document).ready(function() {
// radio buttons
$('#mySpan4').submit(function(e) {
e.preventDefault();
var all = $('input[type=radio][name^=game]').length;
var chk = $('input[type=radio][name^=game]:checked').length;
var exp = all / 2; // in a group two radios each, any one will be selected
if (chk !== exp) {
alert('Error, select all !');
return false;
}
$.ajax({
url: 'ajax2.php',
data: $(this).serialize(), // reads the data ...
success: function(data) {
alert("data Updated Successfully");
window.location = 'login.html';
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="mySpan4">
<table>
<tr>
<td>
Name : abc
</td>
<td>
<input type="radio" name="game[0]" value="Mother">Mother
</td>
<td>
<input type="radio" name="game[0]" value="Father">Father
</td>
</tr>
<tr>
<td>
Name : def
</td>
<td>
<input type="radio" name="game[1]" value="Mother">Mother
</td>
<td>
<input type="radio" name="game[1]" value="Father">Father
</td>
</tr>
<tr>
<td>
Name : ghi
</td>
<td>
<input type="radio" name="game[2]" value="Mother">Mother
</td>
<td>
<input type="radio" name="game[2]" value="Father">Father
</td>
</tr>
</table>
<input name="submit" type="submit" id="submit" value="submit">
</form>
Loop through the radio buttons and check weather its checked.
$(input[type="radio"]).each(function(val, i){
if($(val.is('checked'))){
//all checked
}else{
//Not all checked
}
})

how to insert multiple rows from array values with input

I have my table named i_table with columns:
id name req_qty req_date rcv_qty rec_date
1 metal 1 2014-03-04
2 spring 5 2014-03-04
in my html/php:
<form action="insert.php" method="post">
<?php $resource2=mysql_query("SELECT * FROM i_table",$con);
while($result2=mysql_fetch_array($resource2))
{
?>
<table>
<tr>
<td>
<input type="text" name="id" value="<?php echo $result2['id'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="name" value="<?php echo $result2['name'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="rcv[]" value="" />
</td>
</tr>
<tr>
<td>
<input type="date" name="rcv_date[]" value="" />
</td>
</tr>
</table>
<?php };?>
</form>
how can I insert in multiple arrays from an input according to their unique ID from db? pls help me here... huhu
In your form, add the id as the key -
<form action="insert.php" method="post">
<?php $resource2=mysql_query("SELECT * FROM i_table",$con);
while($result2=mysql_fetch_array($resource2))
{
?>
<table>
<tr>
<td>
<input type="text" name="id[<?php echo $result2['id'];?>]" value="<?php echo $result2['id'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="name[<?php echo $result2['id'];?>]" value="<?php echo $result2['name'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="rcv[<?php echo $result2['id'];?>]" value="" />
</td>
</tr>
<tr>
<td>
<input type="date" name="rcv_date[<?php echo $result2['id'];?>]" value="" />
</td>
</tr>
</table>
<?php };?>
</form>
Then on your insert.php where you post your form, get the id in your foreach -
<?php
if(isset($_POST['id'])){
foreach($_POST['id'] as $id=>$value){
$sql = "UPDATE `i_table` SET `name` = '".$_POST['name'][$id]."', `rcv_qty` = '".$_POST['rcv'][$id]."', `rec_date` = '".$_POST['name'][$id]."' WHERE `id` = ".$id."";
mysql_query($sql,$con);
}
}
?>
note the foreach() is just an example. You will want to sanitize your data before inserting/updating to prevent sql injection. see How can I prevent SQL injection in PHP?

Categories