Send ajax - check and confirm - php

how can i make ajax to check first for result and if any result found to ask for confirm and than to do something ?
$(".btn-delete").click(function(){
var url = "index.php?route=catalog/product/delete&user_token={{user_token}}";
var id = $('input[name*=\'selected\']:checked');
$.ajax({
url: url,
data: id,
dataType: 'json',
success: function(data){
}
});
});
Controller:
if (isset($this->request->post['selected']) && $this->validateDelete()) {
foreach ($this->request->post['selected'] as $product_id) {
$this->model_catalog_product->deleteProduct($product_id);
}
Model:
public function deleteProduct($product_id) {
$checkPreporuchaniProducts = $this->db->query("SELECT product_id FROM oc_preporuchani_category");
$checkid = array();
foreach ($checkPreporuchaniProducts->rows as $PreporuchaniProducts) {
$getId = unserialize($PreporuchaniProducts['product_id']);
foreach ($getId as $id) {
if ($product_id == $id) {
echo "yes: $id<br>";
}else {
echo "no: $id<br>";
}
}
}

Actually i did it with 2 ajax one that check result and another to confirm
$(".btn-delete").click(function(){
var urlcheck = "index.php?route=catalog/product/checkBeforeDelete&user_token={{user_token}}";
var urldelete = "index.php?route=catalog/product/delete&user_token={{user_token}}";
var urldeleteSpecial = "index.php?route=catalog/product/deleteSpecial&user_token={{user_token}}";
var id = $('input[name*=\'selected\']:checked');
$.ajax({
url: urlcheck,
data: id,
type: 'POST',
dataType: 'json',
success: function(data){
if (data.delete == null) {
if (confirm("This product is not used you can delete it")) {
$.ajax({
url: urldelete,
data: id,
type: 'POST',
dataType: 'json',
});
}
}else {
if (confirm("!!! WARNING: Product is used as SPECIAL if delete will effect other system as well !!!")) {
$.ajax({
url: urldeleteSpecial,
data: id,
type: 'POST',
dataType: 'json',
});
}
}
}
})

Related

CodeIgniter Json Ajax Database insertion is not working

I have tried to solve this issue, here i am inserting dynamic inputbox box values into database including their title. But not working...
Inputbox dynamic generation: (This works well)
$('#myTable tbody').append("<tr><td>"+rno+"</td><td>"
+item.stdname+"</td><td><input type='text' name='stdmark[]' class='mark' title='"+item.stdid+"' style='padding: 0px; width: 50px;'/></td></tr>");
Ajax to Send these values into controller:
$('#marklist').submit(function(e){
//var mark = 10;
jsonObj = [];
$("input[class=mark]").each(function() {
var id = $(this).attr("title");
var subjectmark = $(this).val();
item = {}
item ["stdid"] = id;
item ["mark"] = subjectmark;
jsonObj.push(item);
});
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>office/addmark",
data: {senddata :JSON.stringify(jsonObj)},
dataType: "json",
processData:false,
contentType:false,
cache:false,
async:false,
success:
function(retrived_data){
}
});
e.preventDefault();
});
Controller:
public function addmark()
{
$marks = json_decode($this->input->post('senddata'), true);
$this->load->Model('wtcmodel');
foreach($marks as $row)
{
$data = array(
'stdid' => $row->stdid,
'mark' => $row->mark
);
$this->wtcmodel->adddata($data);
}
}
Model:
public function adddata($data)
{
$this->load->database();
$this->db->insert('table_info',$data);
}
you can post input data using this method.
var stdid = $('input[name="stdmark[]"]').map(function(){
return $(this).attr('title');
}).get();
var marks = $('input[name="stdmark[]"]').map(function(){
return this.value;
}).get();
$.ajax({
type: 'POST',
url: 'users.php',
data: {
'stdid[]': stdid,
'marks[]':marks
},
success: function() {
}
});
You can access stdid[] and marks[] variable as array directly in controller.
Controller
public function addmark()
{
$stdid = $this->input->post('stdid');
$marks = $this->input->post('marks');
$this->load->Model('wtcmodel');
foreach($stdid as $key => $row)
{
$data = array(
'stdid' => $stdid,
'mark' => $marks[$key]
);
$this->wtcmodel->adddata($data);
}
}
why did't use Jquery serialize function : http://api.jquery.com/serialize/
$('#marklist').submit(function(e){
e.preventDefault();
//var mark = 10;
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>office/addmark",
data: $(this).serialize(),
dataType: "json",
processData:false,
contentType:false,
cache:false,
async:false,
success:
function(retrived_data){}
});
});
or you can use param : http://api.jquery.com/jquery.param/

selecting 2 different option values and sending data to php using ajax

I am taking two different values from select options and sending it using AJAX, but PHP is not responding to the request.
This is my jQuery code:
$(document).ready(function() {
$("#updateStatus").change(function() {
var opt = $("#updateStatus").val();
$("#updateStatus1").change(function() {
var sta = $("#updateStatus1").val();
$.ajax({
url: 'updatecode.php',
type: 'POST',
data: "option=" + opt + "&status=" + sta,
dataType: 'json',
success: function(data) {
alert(data + "hello");
}
});
});
});
});
This is my PHP code:
$id = $_POST['opt'];
$status = $_POST['sta'];
$query = "UPDATE projectstable SET projectStatus='".$status."'WHERE id='".$id."'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
if (!$row) {
echo json_encode("fail");
} else {
echo json_encode("sucess");
}
Replace your existing ajax function like this
$(document).ready(function() {
var opt = '';
var sta = '';
$("#updateStatus").change(function() {
opt = $("#updateStatus").val();
});
$("#updateStatus1").change(function() {
sta = $("#updateStatus1").val();
callAjax(opt,sta);
});
});
function callAjax(opt,sta) {
$.ajax({
url: 'updatecode.php',
type: 'POST',
data: "opt=" + opt + "&sta=" + sta,
dataType: 'json',
success: function(data) {
alert(data + "hello");
}
});
}
You are passing the json body as a query string, and you are declaring to use dataType: "json"
$.ajax({
url: 'updatecode.php',
type: 'POST',
data: "option=" + opt + "&status=" + sta,
dataType: 'json',
success: function(data) {
alert(data + "hello");
}
});
instead that you should do it like this:
$.ajax({
url: 'updatecode.php',
type: 'POST',
data: {"option": opt, "status": sta},
dataType: 'json',
success: function(data) {
alert(data + "hello");
}
});
You are sending request under data using "option=" and "&status=" but you are reading these values in PHP through $_POST['opt'] and $_POST['stat'] which should actually be $_POST['option'] and $_POST['status'] respectively.
Also you need to change the data request to JSON format like data: {option:opt,status:sta} for your dataType is JSON
Your code should be like this
$(document).ready(function () {
$("#updateStatus,#updateStatus1").change(function () {
var opt = $("#updateStatus").val();
var sta = $("#updateStatus1").val();
$.ajax({
url: 'updatecode.php',
type: 'POST',
data: {option:opt,status:sta},
dataType: 'json',
success: function (data) {
alert(data + "hello");
}
});
});
})
and php code should like this
$id = $_POST['option'];
$status = $_POST['status'];
$query = "UPDATE projectstable SET projectStatus='".$status."'WHERE id='".$id."'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
if (!$row) {
echo json_encode("fail");
} else {
echo json_encode("sucess");
}
Thanks every one looks like i just had to change some code in ajax this is my new code which is working fine
This is my PHP code I got rid of if(!$row)
mysql_select_db("dts_db",$con);
$id=$_POST['opt'];
$status=$_POST['sta'];
$query="UPDATE projectstable SET projectStatus='".$status."'WHERE id='".$id."'";
$result=mysql_query($query);if(!$result){ die("My sql query result ".mysql_error());}
else {
echo json_encode("success");
}
and this is my ajax
$.ajax({
url: 'updatecode.php',
type: 'POST',
data: {opt:opt,sta:sta},
dataType: 'json',
success: function(data) {
alert(data + "hello");
}
});

Call Ajax Again in for loop

I want to request ajax many time in for loop . But it only return 1 value . result.ID. Can anyone help me?
$('#show').html('');
var min = parseInt($('#number_coupon_from').val());
var max = parseInt($('#number_coupon_to').val());
var total_time = parseInt($('#time_coupon').val())*1000;
var randcoupon = Math.floor(Math.random()*(max-min+1)+min);
var timetb = total_time/randcoupon;
var random = 0;
var i,j;
for(i=0;i<randcoupon;i++)
{
for(j=random;j>=0;j--)
{
if(j==0)
{
$.ajax({
url: 'backend/publish_auto/update',
type: 'post',
data: {},
dataType: 'json',
success: function (result)
{
if(result.st)
{
$('#show').append('Update Coupon ID: '+result.ID+'<br />');
}
}
});
}
}
}
});
Thank you !!
use async:False to get ajax response one by one
$.ajax({
url: 'backend/publish_auto/update',
async: false,
type: 'post',
data: {},
dataType: 'json',
success: function (result)
{
if(result.st)
{
$('#show').append('Update Coupon ID: '+result.ID+'<br />');
}
}
});

Access Data from Database with Ajax

I have tried just about every solution and I know my database returns values if i hard code in the php so the issues is returning the values or determining if it is actually sent as post. It gets to the success function and alerts Worked but I am getting undefined for Item ID.
$(document).ready(function() {
$('.check').click(function(){
alert("Step1");
var thisID = $(this).attr('id');
alert(thisID);
$.ajax({
type: "GET",
url: "XXX.PHP",
data: { "ID": thisID},
cache: false,
async:true,
datatype: "json",
success: function(data)
{
alert("WORKED");
alert(data.ItemID);
}
});
});
});
Here is the PHP
if(isset($_POST['ID']))
{
$ID = $_POST['ID'];
function retrieve($ID)
{
$stmt = $mysqli->query("SELECT * FROM database1.menu WHERE ItemID = $ID");
if($stmt->num_rows) //if there is an ID of this name
{
$row = $stmt->fetch_assoc();
echo $row;
print json_encode($row);
}
}
You could try something like this. Not sure if this is what you need.
$(".check").click(function(event){
event.preventDefault();
var $this = $(this);
var thisID = $this.attr('id');
$.ajax({
type: "POST",
url: base_url + "url.php",
data: {
id: thisID
},
dataType: "text",
cache:false,
success:
function(data){
alert(data);
}
});
return false;
});

Using AJAX form submit to submit and retrieve data from MySQL

Basically, I'm trying to make it so that when a post is submitted to my site, it sends the post using AJAX so that they don't change page, and then if the AJAX post is successful, retrieve all the posts for said user from MySQL and write them onto the page.
My problem is that the browsers (Chrome, IE) are completely ignoring the AJAX request.
My form:
<div id="updatestatus">
<form action="" method="post" id="ps">
<textarea name="status" id="status"></textarea>
<input type="hidden" name="uid" id="uid" value="<?php echo $uid; ?>" />
<input type="submit" id="poststatus" name="poststatus" value="Share" />
</form>
</div>
My AJAX request:
$(function() {
$("#poststatus").click(function() {
var status = $("textarea#status").val();
if (status == "") {
return false;
}
var uid = $("input#uid").val();
var dataString = 'status='+ status + '&uid=' + uid;
$.ajax({
type: "POST",
url: "updatestatus.php",
data: dataString,
success: function() {
$.ajax({
url: 'ajax/query.php',
data: "uid=<?php echo $uid; ?>",
dataType: 'json',
success: function(data) {
var status = data[0];
var sid = data[1];
$('#mainprofile').html("<div id='statuses'><p>"+status+"</p></div>);
return false;
}
});
return false;
});
});
});
});
My ajax/query.php request
<?php
//connect stuff
$uid = strip_tags(stripslashes(htmlspecialchars(htmlentities(mysql_real_escape_string($_GET['uid'])))));
$result = mysql_query("SELECT * FROM mingle_status WHERE uid = '$uid' ORDER BY timestamp DESC"); //query
$array = mysql_fetch_row($result); //fetch result
echo json_encode($array);
?>
Thanks in advance for any help - Joe
In this section of JS code
$.ajax({
type: "POST",
url: "updatestatus.php",
data: dataString,
success: function() {
$.ajax({
url: 'ajax/query.php',
data: "uid=<?php echo $uid; ?>",
dataType: 'json',
success: function(data) {
var status = data[0];
var sid = data[1];
$('#mainprofile').html("<div id='statuses'><p>"+status+"</p></div>);
return false;
}
});
return false;
});
});
You need to remove the ending bracket after the curly brace which follows the last return false, such as...
$.ajax({
type: "POST",
url: "updatestatus.php",
data: dataString,
success: function() {
$.ajax({
url: 'ajax/query.php',
data: "uid=<?php echo $uid; ?>",
dataType: 'json',
success: function(data) {
var status = data[0];
var sid = data[1];
$('#mainprofile').html("<div id='statuses'><p>"+status+"</p></div>");
return false;
}
});
return false;
};
});
Try this
$(function() {
$("#poststatus").click(function() {
var status = $.trim($("#status").val());
if (status == "") {
return false;
}
var uid = $("#uid").val();
var dataString = 'status='+ status + '&uid=' + uid;
$.ajax({
type: "POST",
url: "updatestatus.php",
data: dataString,
success: function() {
$.ajax({
url: 'ajax/query.php',
data: "uid="+<?php echo $uid; ?>,
dataType: 'json',
success: function(data) {
var status = data[0];
var sid = data[1];
$('#mainprofile').html("<div id='statuses'><p>"+status+"</p></div>);
return false;
}
});
return false;
}
});
});
});

Categories