there is array in while loop please check with code below
<?php
$addItemSql = "SELECT * from tbl_invoice_services where col_cust_id='$getID' ORDER BY col_service_id";
$addItemResult = mysqli_query($db, $addItemSql);
while ($addItemRow = mysqli_fetch_assoc($addItemResult)) {
?>
<tr>
<td>
<input type="hidden" id="col_service_product1" name="col_service_product1[]" value="<?php echo $addItemRow['col_service_product']; ?>">
</td>
</tr>
<?php} ?>
and want to post those array value to php
ajax code
$('.saveInvoice').on('click', function (ev) {
$.ajax({
url: "saveInvoce.php",
type: "POST",
data: {
col_service_product1:$('#col_service_product1').val()
},
success: function (data) { }
});
});
try this demo code
file1
<?php
$i=0;
while ($i<5) {
?>
<tr>
<td>
<input type="hidden" id="col_service_product1" name="col_service_product1" value="<?php echo $i; ?>">
</td>
</tr>
<?php $i++; } ?>
<button value="Save Invoice" class="saveInvoice">Save Invoice</button>
<script type="text/javascript" src="../library/q.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.saveInvoice').click(function () {
var arr=[];
var data=document.getElementsByName("col_service_product1");
for(var i=0;i<data.length;i++){
arr[i]=data[i].value;
}
$.ajax({
url: "saveInvoce.php",
type: "post",
data: {col_service_product1:arr},
success: function (data) {
alert(data);
}
});
});
});
</script>
file2
<?php
$data=$_POST['col_service_product1'];
//code for save data in table
foreach($data as $value){
echo $value; //query here
}
Related
I want to display pdf icon only when the field (WO_doc) is not empty. I don't know what I am doing wrong while writing the if statement. Please help.
<script type="text/javascript">
$( "select[name='PEF_id']").change(function () {
var Pefid = $(this).val();
if(Pefid) {
$.ajax({
url: "<?php echo site_url('Admin/showwo');?>",
dataType: 'Json',
data: {'PEF_id': Pefid},
success: function(response){
$(".displayNone").removeClass("displayNone").addClass("displayHidden");
$.each(response.wo,function(key, value) {
$('.wodata').append('<tr>\
<td>'+value['WO_no']+'</td>\
<td>'+value['WO_date']+'</td>\
<td>'+value['Agency_name']+'</td>\
<td>'+value['WO_amount']+'</td>\
<?php if("?>'+value['WO_doc']+'<?php" != null) { ?>\ ***this if condition is not working***
<td></i></td> <?php } else { ?> <td></td> <?php } ?>\
<td><button type="submit" name="submit" class="btn btn-success">Update</button></td>\
<td><button type="submit" name="submit" value="'+value['WO_no']+'" class="button-del btn btn-danger">Close this WO</button></td>\
</tr>');
});
}
});
}
});
</script>
This is my ajax call code..
<script type="text/javascript">
$(document).ready(function(){
$('#itemcode').blur(function(){
var itemcode = $(this).val();
$.ajax({
type: "POST",
url:'ajax.php',
data: {'itemcode' : itemcode} ,
cache: false,
success: function(data) {
alert(data)
$("#desc").val(data);
$("#bal").val(data);
}
});
});
});
</script>
include "db.php";
$itemcode=$_POST['itemcode'];
$sql="select * from itemmaster where Item_Code='$itemcode'";
$result = mysql_query($sql, $con);
while($row = mysql_fetch_array($result)) {
echo $row['Item_Desc'];
echo $row['Balance_Stock'];
}
This is simple html form..
<form action="add.php" method="post">
<table style="border: 1px solid black;padding:20px;"cellspacing="1px">
</br></br>
<tr>
<td>Issue No:</td> <td><input name="issueno" type="text" id="issueno"/></td>
<td>Issue Date:</td> <td><input name="issuedate" type="date" id="issuedate"/></td></tr></br></br><tr></tr>
<tr>
<td>Item Code:</td> <td><input name="itemcode" type="text" id="itemcode" /></td>
<td>Description:</td> <td><input name="des" type="text" style="width:250px; height:23px" id="desc"/></td>
<td>Bal Quantity:</td> <td><input name="bal" type="text" id="bal"/></td>
</tr></br>
<tr> <td>Issue Quantity:</td> <td><input name="issuequ" type="text" id="issuequ"/></td></tr></br><tr></tr>
<tr><td>Remark:</td> <td><input name="remark" type="text" style="width:250px; height:23px" id="remark"/></td></tr></br>
<tr><td colspan="6"><center><input type="submit" value="submit"></center></td></tr>
</table>
</form>
When I alert(data) I am getting this samsung20.00. where samsung is description and 20.00 is bal. I want to assign description desc id an bal to bal id. So How do I do that??
In your ajax.php file, you need to use json_encode function so you can parse it after get response:
include "db.php";
$itemcode=$_POST['itemcode'];
$sql="select * from itemmaster where Item_Code='$itemcode'";
$result = mysql_query($sql, $con);
while($row = mysql_fetch_array($result)) {
$json = array("Item_Desc" = > $row['Item_Desc'],
"Balance_Stock" => $row['Balance_Stock']
);
}
echo json_encode($json);
After making adjusts that you can encode your response, Your ajax need to parse it.
Example:
<script type="text/javascript">
$(document).ready(function(){
$('#itemcode').blur(function(){
var itemcode = $(this).val();
$.ajax({
type: "POST",
url:'ajax.php',
data: {'itemcode' : itemcode} ,
cache: false,
success: function(data) {
var obj = JSON.parse(data);
$("#desc").html(obj.Item_Desc);
$("#bal").html(obj.Balance_Stock);
}
});
});
});
Its very simple.
Hope it helps you
in your php file join the value Samsung and 20.00 with || sign
include "db.php";
$itemcode=$_POST['itemcode'];
$sql="select * from itemmaster where Item_Code='$itemcode'";
$result = mysql_query($sql, $con);
while($row = mysql_fetch_array($result)) {
echo $row['Item_Desc'].'||'.$row['Balance_Stock'];
}
java script code add the var response = data.split('||'); function
<script type="text/javascript">
$(document).ready(function(){
$('#itemcode').blur(function(){
var itemcode = $(this).val();
$.ajax({
type: "POST",
url:'ajax.php',
data: {'itemcode' : itemcode} ,
cache: false,
success: function(data) {
alert(data)
var response = data.split('||');//spilt the value
$("#desc").val(response[0]);
$("#bal").val(response[1]);
}
});
});
});
</script>
<script type="text/javascript">
$(document).ready(function(){
$('#itemcode').blur(function(){
var itemcode = $(this).val();
$.ajax({
type: "POST",
url:'ajax.php',
data: {'itemcode' : itemcode} ,
cache: false,
success: function(data) {
alert(data)
var str1 = data.replace(/\d.+/g, '');
var str2 = data.replace(/[^\d.-]/g, '');
$("#desc").val(str1);
$("#bal").val(str2);
}
});
});
});
</script>
i am making a website in which i am to embbed the functionality of delete using multiple checkbox. here is my code. my problem is
1. Ajax call is not working.
2. how can i make search from database for array .
<?php
if(isset($_POST['Delete']))
{
$array=$_POST['check_box'];
}
?>
<form method="post" id="form">
<table width="200" border="1">
<tr>
<td>select</td>
<td>NAme</td>
<td>Action</td>
</tr>
<?php
while($selectnumberarr=mysql_fetch_array($selectnumber))
{
?>
<tr>
<td><input type="checkbox" name="check_box[]" class="check_box" id="<?php $selectnumberarr[0]; ?>" /> </td>
<td><?php echo $selectnumberarr[1]; ?></td>
</tr>
<?php
}?>
<input type="submit" name="Delete" id="delete">
</table>
</form>
and below is my ajax and javascript code.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#delete').click(function() {
$.ajax({
type: "POST",
url: "checkbox.php",
data: $('#form').serialize(),
cache: false,
success: function(html)
{
alert("true");
}
});//end ajax
});
});
</script>
any help would be appriciated
your code is almost correct. You need to remove `onChange="show()" for input checkbox, because if you have jquery then you don't need to put events on HTML elements.
Use jquery 'on' method for compatibility for latest php library.
Replace your jquery code with following jquery code :-
<script>
$(document).ready(function(){
$('#delete').on('click',function()
{
var cat_id = $('.check_box:checked').map(function() {
return this.id;
}).get().join(',');
console.log(cat_id);
$.ajax({
type: "POST",
url: "checkbox.php",
data: { "kw ":cat_id },
datatype:'json',
success: function(html)
{
alert("true");
}
});//end ajax
});
});
</script>
Use ".check_box" instead of "element" in jquery to prevent checks for all checkboxes, instead of desired ones.
Hope it helps.
Why you don't use an array for sending the checkboxes like:
HTML part:
<?php
if (isset($_POST['check_box'])) {
var_dump($_POST['check_box']);
echo "ajax call is working";
}
?>
<form id="form">
<table width="200" border="1">
<tr>
<td>select</td>
<td>NAme</td>
<td>Action</td>
</tr>
<?php
while ($selectnumberarr = mysql_fetch_array($selectnumber)) {
?>
<tr>
<td><input type="checkbox" name="check_box[]" class="check_box" value="<?php echo $selectnumberarr[0]; ?>" /> </td>
<td><?php echo $selectnumberarr[1]; ?></td>
</tr>
<?php
}
?>
</table>
<input type="button"name="delete" id="delete" value="Delete" />
</form>
JQuery part:
<script type="text/javascript">
$(document).ready(function(){
$('#delete').click(function() {
$.ajax({
type: "POST",
url: "checkbox.php",
data: $('#form').serialize(),
cache: false,
success: function(html)
{
alert("true");
}
});//end ajax
});
});
</script>
So you can easily get an array of the selected checkboxes in php with:
$idsArray = $_POST["check_box"];
this looks now like:
array(
"1", "2","etc.."
);
so this array contains all the ids of the selected checkboxes for delete.
I am trying to delete all records from the table using the checkbox. When the user checked the topmost checkbox, it will check all other checkboxes inside the loop then a confirmation box will appear about deleting the records. if the user click OK, the the all the records will be deleted using $.ajax, if he clicked Cancel, then, the page will return to the same state, and the checkboxes are not checked anymore.
<?php
include 'dbconn.php';
?>
<table border="1" >
<tr><td align="center" width="20"><input type="checkbox" name='checkALL' id='checkALL'></td><td>Name</td>
</tr>
<?php
$sql=mysql_query("SELECT * FROM names ORDER BY names ASC") or die(mysql_error());
while($rows=mysql_fetch_assoc($sql)){
?>
<tr>
<td><input type="checkbox" name="id[]" id="id[]" value="<?php print $rows['id'];?>">
</td><td>Name</td>
</tr>
<?php
}
?>
</table>
JQUERY
<script>
$(function(){
//click all
$('#checkALL').click(function(){
$(':checkbox').attr({checked: 'true'});
var del=confirm("You checked all the box. Delete All?");
if(del==true){
//delete here using $.ajax
}
else{
window.location.reload(false);
$('#checkAll').attr({checked: 'false'});
}
});
});
</script>
Place this in your if block.
$.ajax({
type: "GET",
url: '<php file which truncates table>',
success: function (data) {
if (data == 'truncated') {
alert('success');
} else {
alert('not truncated');
}
}
});
Return the string truncated from your php file on success.
Here U- PageUrl, A- Action on page, P- Parameter
if (confirm('Are you sure to delete this record?')) {
Operation(U, A, P);
}
function Operation(U, A, P) {
$.ajax({
type: "POST",
url: U + '/' + A,
data: P,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
cache: false,
success: function (r) {
var str = r.d;
}
});
}
You can use something like this in your HTML check all page
<html>
<head><title>Select/Delete ALL with jQuery/PHP</title>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
<table border="1" cellpadding="5">
<tr>
<th>
<input type="checkbox" id="checkAll" />
</th>
</tr>
<tr>
<td><input type="checkbox" class="check" name="posts[]" value="100" /></td>
</tr>
<tr>
<td><input type="checkbox" class="check" name="posts[]" value="200" /></td>
</tr>
<tr>
<td><input type="checkbox" class="check" name="posts[]" value="300" /></td>
</tr>
</table>
<script>
$(document).ready(function(){
var checked = false;
$('#checkAll').click(function(){
if (checked == false){
checked = true
} else {
checked = false
}
$('input.check').attr("checked",checked);
var confirmDelete=confirm("You checked all the box. Delete All?");
if (confirmDelete==true){
var csv = '';
$('.check').each(function() {
csv += $(this).attr("value") + ",";
});
$.ajax({
type: "POST",
url: "delete.php",
data: { tobeDeleted: csv }
}).done(function( msg ) {
console.log( "Data has been deleted: " + msg );
});
} else {
$('#checkAll').attr("checked", false);
$('input.check').attr("checked", false);
}
});
});
</script>
</body>
</html>
and use something like this in your PHP script
<?php
$postDeleted = substr($_POST['tobeDeleted'], 0, strlen($_POST['tobeDeleted'])-1);
$arrDeleted = explode(",", $postDeleted);
$sql = "DELETE FROM employee WHERE 1=1 ";
foreach($arrDeleted as $key=>$value){
$sql .= "OR employee_id = $value ";
}
echo $sql;
?>
if(del==true){
$.ajax({
type: "POST",
url: 'your_file_url',
async:false,
cache: false,
success: function(data){
if (data == 1) {
alert('success');
} else {
alert('Error');
}
}
});
}
In your php file, echo 1 for successful delete and 0 for some error
I have a form that will insert into table 'tags' using ajax. I was able to add manually but not without reloading the page.
This is my controller Controller (tags.php)
class Tags extends CI_Controller{
function __construct(){
parent:: __construct();
$this->load->model('tags_model');
$this->load->helper('form');
$this->load->helper('url');
}
function index(){
$data['tags']=$this->tags_model->get();
$this->load->view('tags/index',$data);
}
function add()
{
$this->tags_model->save();
return true;
}
}
?>
This is my view('index.php')
<script src="<?php echo base_url('assets/js/jquery.js');?>"></script>
<?php
foreach ($tags as $t){
echo '<span>';
echo $t['id'].':';
echo $t['title'];
echo '-';
echo '</span>';
}
?>
<form id="comment" method="post">
<?php echo form_input('title','text is here....');?>
<label> </label><input type="submit" value="Submit" />
</form>
<!-- here is the script that will do the ajax. It is triggered when the form is submitted -->
<script>
$(function(){
$("#comment").submit(function(){
dataString = $("#comment").serialize();
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>tags/add",
data: dataString,
return false; //stop the actual form post !important!
success: function(data)
{
alert('Successful!');
}
});
return false; //stop the actual form post !important!
});
});
</script>
Model
<?php
class Tags_model extends CI_Model{
function __construct()
{
parent:: __construct();
$this->load->database();
}
function save()
{
$title=$this->input->post('title');
$data=array(
'title'=>$title
);
$this->db->insert('tags',$data);
}
function get(){
$query=$this->db->get('tags');
return $query->result_array();
}
}
?>
code seem to be okay to me. I can insert normally but not in ajax . Any help is welcome.
Remove the return false that is inside your jquery ajax call.
$(function(){
$("#comment").submit(function(){
dataString = $("#comment").serialize();
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>tags/add",
data: dataString
success: function(data)
{
alert('Successful!');
}
});
return false; //stop the actual form post !important!
});
});
I did like in this tutorials and now workings
http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-day-8-ajax/
now my view is like that
<script src="<?php echo base_url('assets/js/jquery.js');?>"></script>
<?php
foreach ($tags as $t){
echo '<span>';
echo $t['id'].':';
echo $t['title'];
echo '-';
echo '</span>';
}
?>
<?php echo form_open('tags/add');?>
<?php echo form_input('title','text is here....','id="title"');?>
<?php echo form_submit('submit', 'Submit', 'id="submit"'); ?>
<?php echo form_close();?>
<!-- here is the script that will do the ajax. It is triggered when the form is submitted -->
<script type="text/javascript">
$('#submit').click(function() {
//var title = $('#title').val();
var form_data = {
title: $('#title').val(),
ajax: '1'
};
$.ajax({
url: "<?php echo site_url('tags/add'); ?>",
type: 'POST',
data: form_data,
success: function() {
alert('added Successfully');
}
});
return false;
});
</script>
instead of
url: "<?php echo base_url('tags/add'); ?>",
I also had to switch to
url: "<?php echo site_url('tags/add'); ?>",
Controller
public function added()
{
$data['name']=$this->input->post('name');
$data['address']=$this->input->post('address');
$re=$this->model->add('aa',$data);
if($re)
{
?>
<script> alert("inserted");</script>
<?php
}
else
{
?>
<script> alert("not insert");</script>
<?php
}
//redirect('Myconn/insert');
}
JS Code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function (e) {
$("#form1").on('submit',(function(e) {
var name = $("#name").val();
var address = $("#address").val();
e.preventDefault();
// document.getElementById("submit").value="Sending.....";
$.ajax({
url: "<?php echo base_url(); ?>Myconn/added",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(html)
{
$("#feed_m").after(html);
//document.getElementById("submit").value="submit.";
document.getElementById('name').value='';
document.getElementById('address').value='';
}
});
}));
});
</script>
HTML Code
<div id="feed_m"></div>
<form method="post" id="form1" action="">
<table >
<tr>
<td>name</td>
<td><input type="text" id="name" name="name"></td>
</tr>
<tr>
<td>address</td>
<td><input type="text" id="address" name="address"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" id="submit" name="submit" value="submit"></td>
</tr>
</table>
</form>
Controller
public function added()
{
$data['name']=$this->input->post('name');
$data['address']=$this->input->post('address');
$re=$this->model->add('aa',$data);
if($re)
{
?>
<script> alert("inserted");</script>
<?php
}
else
{
?>
<script> alert("not insert");</script>
<?php
}
}
Model
public function add($table,$data)
{
return $this->db->insert($table,$data);
}