I need to receive the status of vote using ajax and php and jquery. Following is my code :
var VoteStatus= GetStatus() ;
var ID = $('#ID').val();
function GetStatus() {
var res = '';
$.ajax({
type: "POST",
data: {VoteID:ID} ,
url: "/vote_status.php",
async: false,
success: function(result) { res=result; }
});
return res;
}
alert('Vote Status= ' + VoteStatus);
In my php file:
$VoteID = $_POST['VoteID'];
$Property = $_POST['Property'];
if ( $VoteID == 0 )
echo 'No Vote provided - Property = '. $Property;
exit;
The alert box shows: Vote Status = No Vote Provided
Please help.
I have posted the VoteID, but the php file doesn't seem to receive it.
Try the alert in here and check if its working
$.ajax({
type: "POST",
data: {"VoteID":ID} ,
url: "/vote_status.php",
async: false,
success: function(result) {
alert(result); }
});
The name of the POST variable needs to be in quotes, as in
data: {"VoteID":ID}
Try this and check jquery ajax manuals
$.ajax({
type: "POST",
data:"VoteID=" + ID +"&secondparam=" + secondvalue,
url: "/vote_status.php",
async: false,
success: function(result) { alert(result); }
});
Related
I am getting data from ajax call. But that data is coming in Jquery and I have saved it in a variable. Now I want that data to be utilized for running some php and mysql code. Can any one solve this?
$("#submit_bt").click(function () {
var name = $('#search-box').val();
var dataString = 'name=' + name;
if (name == "" ){
$('.alert').show().html('Please fill all information')
}
else
{
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "read_data.php",
data: dataString,
cache: false,
success: function (result) {
alert(result);
//$('.alert').show().html(result).delay(2000).fadeOut(3000);
setTimeout(function(){window.location.href = "index.php";},2000);
}
});
}
return result;
});
If what you want is to navigate to the index.php page on click of that button, then, do it this way:
$("#submit_bt").click(function () {
var name = $('#search-box').val();
var dataString = 'name=' + name;
if (name == "" ){
$('.alert').show().html('Please fill all information')
}
else
{
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "read_data.php",
data: dataString,
cache: false,
success: function (result) {
alert(result); //you may remove this. use console.log for debugging your js next time
setTimeout(function(){window.location.href = "index.php?result="+result;},2000); //why the timeout?
}
});
}
});
The easier and proper solution should be to re-use ajax to use this variable in another PHP file.
$("#submit_bt").click(function () {
var name = $('#search-box').val();
var dataString = 'name=' + name;
if (name == "" ){
$('.alert').show().html('Please fill all information')
}
else
{
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "read_data.php",
data: dataString,
cache: false,
success: function (result)
{
//AJAX code to execute your MySQL query
$.ajax({
type: "POST",
url: "read_data2.php",
data: result,
cache: false,
success: function (result)
{
//Manage your read_data2.php output
}
});
}
});
}
I am trying to create a table in MySQL dynamically from PHP.
User inputs should be inserted into that table using jQuery,AJAX and PHP.
my code is looking like this.
var toServer = brandname+','+prodname+','+qty;
$.ajax({
type: 'POST',
url: 'sample.php',
data: toServer,
cache: false,
success: function(res){
alert(res);
},
error: function(){
alert(1);
}
})
In sample.php:
require '../models/orderClass.php';
$order = new orderClass;
$data = explode(',',$_REQUEST['toServer']);
$res = $order->addToCart('mymail#domain.com', $data[0], $data[1],$data[2]);
echo $res;
In orderClass:
$this->con = new mysqli('servername','user','pass','dbnaem');
if($this->con->connect_error)
{
echo($this->con->connect_error);
}
Without AJAX, its working. After adding $.ajax() its not working.
Please help me out
Thanks in advance
I am new to AJAX and I added error: function at last since it was not working.
because the "data: " sent to server in $_REQUEST so it must be a hash of data.
try this:
var toServer = brandname+','+prodname+','+qty;
$.ajax({
type: 'POST',
url: 'sample.php',
data: {'toServer': toServer},
cache: false,
success: function(res){
alert(res);
},
error: function(){
alert(1);
}
})
or another version:
javascript:
$.ajax({
type: 'POST',
url: 'sample.php',
data: {
'brandname': brandname,
'prodname': prodname,
'qty': qty
},
cache: false,
success: function(res){
alert(res);
},
error: function(){
alert(1);
}
});
php part:
require '../models/orderClass.php';
$order = new orderClass;
$brandname = $_REQUEST['brandname'];
$prodname = $_REQUEST['prodname'];
$qty = (int)$_REQUEST['qty'];
$res = $order->addToCart('mymail#domain.com', $brandname, $prodname, $qty);
echo $res;
your variable should b like this
var toServer = "brandname="+brandname+" &prodname= "+prodname+" &qty="+qty;
and in sample.php
use like this
$brandname = $_REQUEST['brandname'];
$prodname= $_REQUEST['prodname'];
$qty= $_REQUEST['qty'];
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 />');
}
}
});
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;
});
I am new to ajax and CI .I want to send data and image through ajax . In my view i have 3 input fields and one image upload button .
var val1 = $("#val1"+id).val();
var val2 = $("#val2").val();
$.ajax({
type: "POST",
url: "page/save_data",
data: "{ val1 :'"+val1+"',val2:'"+val2+"}",
success: function(msg) {
alert(msg);
}
});
and in controller when i try this it shows me nothing
function save_data()
{
$val = $this->input->post('val1');
echo $val1;
}
In console it gives me nothing .
Try this :
$.ajax({
type: "POST",
url: "page/save_data",
data: { "val1 ":val1,"val2": val2},
success: function(msg) {
alert(msg);
}
});
Your ajax URL must be refer to your method name;
...
$.ajax({
type: "POST",
url: "page/save_data", //change this to your method;
...
should be like:
...
$.ajax({
type: "POST",
url: "page/save_iudata",//or whatever your method's name;
...
EDIT:
try this way:
function save_data(){
$val1 = $_REQUEST['val1'];
echo $val1;
}
Hello to send data via ajax is easy:
var data = {
val1: $('#val1').val(),
val2: $('#val2').val(),
};
$.ajax({
url: "<?php echo base_url().'page/save_data'; ?>",
dataType: 'json',
type: 'POST',
async : false,
data: data,
success: function(msg){
...
}
});
return false;
But to send an image you have to do some research...
here is a good tutorial