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;
});
Related
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");
}
});
I´ve tried retrive some info from mysql for put it in a form,and update the info.
I use ajax call in the same page,I call ajax passing the id and php return all info.
if(isset($_POST["retriveForm"])) {
$data_json =array();
$id = $_POST['retriveForm'];
$sql = "SELECT * FROM mytable WHERE Id = $id";
while ($row = mysqli_fetch_array($db->consulta($sql)) {
$data_json = array('item1' => $row['item1'],'item2' => $row['item2']) ;
}
$data_json['item_array'] = call_a_function_return_array();//this works
echo json_encode($data_json);
}
ajax call shows like this
$(document.body).on('click', '.edit' ,function(){
var id = $(this).data('id');
$.ajax({
type: "POST",
url: "is_the_same_page.php",
data: {retriveForm : id},
success: function(response) {
$('#myForm').find('input').eq(1).val(response.item1);
}
});
});
When put .val(response.item1) get anything,but if I put response the input take all values
$('#myForm').find('input').eq(1).val(response);
shows
{"item1":"item1","item2":"item2","item_array":["item_in_array1","item_in_array2"]}
I also tried
var obj = jQuery.parseJSON(response);
Try setting $.ajax() dataType options to "json"
"json": Evaluates the response as JSON and returns a JavaScript
object.
$(document.body).on("click", ".edit", function() {
var id = $(this).data("id");
$.ajax({
type: "POST",
url: "is_the_same_page.php",
dataType: "json", // set `dataType` to `"json"`
data: {
retriveForm: id
},
success: function(response) {
$("#myForm").find("input").eq(1).val(response.item1);
}
});
});
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'];
Hi all I have a site developed in codeigniter.
In some function I have to retrieve data from mysql and print the result in javascript because is an ajax call.
Thi is my php function to retrieve data:
public function getCityByNameOnly($name) {
$query = $this->db->query('SELECT * FROM city WHERE name LIKE "%'.$name.'%" ');
$city = array();
foreach ($query->result() as $row)
array_push($city, $row);
return json_encode($city);
}
}
And this is my ajax call:
$('#ricerca-ajax').keyup(function(){
var val = $(this).val();
if (val.length>2){
var site_url_city ="<?php echo(site_url('/city/get_city_by_text')); ?>";
$.ajax({
url: site_url_city,
type: "POST",
data: {search: val},
dataType: "text",
success: function(data) {
console.log(data);
for(var i=0;i<data.length;i++)
{
$('#msgid').append(data[i].name_en + '<br> ');
}
}
});
}
})
I append the result into a div but is always undefined.
This is the console.log of my created json:
{"id":"125","name_it":"Lèsina (Hvar)","name_en":"Hvar","nation_id":"23","region_id":"0","active":"1"},{"id":"127","name_it":"Trogir (Traù)","name_en":"Trogir","nation_id":"23","region_id":"0","active":"1"},{"id":"1088","name_it":"Città del Capo","name_en":"Cape Town","nation_id":"101","region_id":"0","active":"1"}]
How to print this json into my success function in javascript?
Thanks
change the datatype:"json" in the ajax will solve the issue
$('#ricerca-ajax').keyup(function(){
var val = $(this).val();
if (val.length>2){
var site_url_city ="<?php echo(site_url('/city/get_city_by_text')); ?>";
$.ajax({
url: site_url_city,
type: "POST",
data: {search: val},
dataType: "json",
success: function(data) {
console.log(data);
for(var i=0;i<data.length;i++)
{
$('#msgid').append(data[i].name + '<br> ');
}
}
});
}
})
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); }
});