Create table in Mysql dynamically with PHP,ajax,jQuery - php

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'];

Related

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");
}
});

Pagin With Ajax, Post no working

I Use this code for paging
function changePagination(pageId,liId,modul,userid){
$(".flash").show();
var dataModul = 'modul='+modul;
var dataUser = 'userid='+userid;
var dataString = 'pageId='+ pageId;
$.ajax({
type: "POST",
url: "loadDataVerifikasi.php",
data: dataString,
data: dataModul,
data: dataUser,
cache: false,
success: function(result){
$(".flash").hide();
$(".link a").removeClass("In-active current") ;
$("#"+liId+" a").addClass( "In-active current" );
$("#pageData").html(result);
}
});}
And this for an action file with name loadDataVerifikasi.php
if(isset($_POST['pageId']) && !empty($_POST['pageId'])){
$id=$_POST['pageId'];
}
else{
$id='0';
}
$pageLimit=PAGE_PER_NO*$id;
$query="select * from pengajuan_".$_POST['modul']." join verifikasi on pengajuan_".$_POST['modul'].".ketkdpengajuan = verifikasi.ketkdpengajuan where kdadmin = '".$_POST['userid']."' group by kdverifikasi limit $pageLimit,".PAGE_PER_NO;
But $_POST['modul'] post nothing,
I am not familiar with your syntax.
Please try:
function changePagination(pageId,liId,modul,userid){
$(".flash").show();
var datastring = {
modul: modul,
userid: userid,
pageId: pageId
}
$.ajax({
type: "POST",
url: "loadDataVerifikasi.php",
data: dataString,
cache: false,
success: function(result){
$(".flash").hide();
$(".link a").removeClass("In-active current") ;
$("#"+liId+" a").addClass( "In-active current" );
$("#pageData").html(result);
}
});
}
The $.ajax data attribute accepts an object with key-value pairs
I believe it is because you overwrite the data with dataUser.
I haven't tested i but the code below should work.
Also check out this for more info
function changePagination(pageId,liId,modul,userid){
$(".flash").show();
var dataModul = 'modul='+modul;
var dataUser = 'userid='+userid;
var dataString = 'pageId='+ pageId;
$.ajax({
type: "POST",
url: "loadDataVerifikasi.php",
data: { 'dataString':dataString, 'dataModul':dataModul, 'dataUser':dataUser },
cache: false,
success: function(result){
$(".flash").hide();
$(".link a").removeClass("In-active current") ;
$("#"+liId+" a").addClass( "In-active current" );
$("#pageData").html(result);

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;
});

jQuery ajax fetch data from PHP into a jQuery variable

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); }
});

Categories