I am trying to Execute Ajax in Yii.
But gives me error as :
Empty string passed to getElementById()
Below is my code of AJAX :
$url1 = Yii::$app->homeUrl;
$crurl = 'https://'.$_SERVER['SERVER_NAME'].$url1;
$this->registerJs('$(document).ready(function(){
$(".glyphicon.glyphicon-eye-closed").click(function(){
$.ajax({
url: "'.$crurl.'?r=history/move",
type: "post",
data: {id: this.id},
success: function(data){
alert(data);
}
});
});
});
Check the following options;
Check the html code that the .glyphicon.glyphicon-eye-closed classes has that has id attr?
Replace this.id to $(this).attr('id');
Related
Hey I can't pass a JS variable through AJAX to php file. No matter if I try POST or GET it's not working: with POST it gave me [] or Undefined index if I want to print_r($_POST['adminID']
And GET, there I get {"fancybox":"true"}
$('#adminList tr').on('click', function() {
var adminID = $(this).find(".adminID").text();
console.log(adminID);
$.ajax({
type: 'POST',
url: 'modules/management/user_edit.php',
data: {adminID : adminID},
success: function(data)
{
$.fancybox.open({
src : 'modules/management/user_edit.php',
type : 'ajax'
});
}
});
});
and module/management/user_edit.php i just want to post this adminID
You send double request to user_edit.php file, first with POST data, and second without. You should display result of first request in fancybox.
$('#adminList tr').on('click', function() {
var adminID = $(this).find(".adminID").text();
$.ajax({
type: 'POST',
url: 'modules/management/user_edit.php',
data: {adminID : adminID},
success: function(response)
{
$.fancybox.open(response);
}
});
});
Hey all im trying to use ajax to get a php file and run a few functions.
My ajax looks like this :
jQuery(document).ready(function(){
jQuery(".claim-button").click(function(){
alert("click ok");
var baseUrl = document.location.origin;
jQuery.ajax({
type: "POST",
url: '/custom_functions/win_checker.php'
});
});
});
Then in the php file I put something simple jsut to test it works:
echo "Test";
I get the jquery "click ok" but it does not return the php echo.
Im am very new to ajax could someone please advise what im doing wrong.
Many thanks
David
You need to have success to capture the PHP response:
jQuery(document).ready(function(){
jQuery(".claim-button").click(function(){
alert("click ok");
var baseUrl = document.location.origin;
jQuery.ajax({
type: "POST",
url: '/custom_functions/win_checker.php',
data: {var1: 'someValue'},
success: function(response){
alert(response); // this will alert 'Test'
}
});
});
});
In PHP, you will get parameters in $_POST.
To handle the output of the AJAX request, you have to provide a function to be executed on success:
jQuery(document).ready(function(){
jQuery(".claim-button").click(function(){
alert("click ok");
var baseUrl = document.location.origin;
jQuery.ajax({
type: "POST",
url: '/custom_functions/win_checker.php',
success: function(result) {
// Do with your result what you want, e.g. print it to the console
console.log(result);
}
});
});
});
I am trying to create a simple shopping cart using AJAX and PHP.
Everything works as it should BUT 1 thing doesn't work all the time and it seems that it fails to execute. (it works 3 times out of 5).
to explain this issue please take a look at the code bellow:
jQuery(document).ready(function() {
//////////////////////LETS RUN OUR ADD TO CART FEATURE USING AJAX //////////////
$(function(){
$('.form1').on('submit', function(e){
$( "#preloader" ).fadeIn( 850 );
// prevent native form submission here
e.preventDefault();
// now do whatever you want here
$.ajax({
type: $(this).attr('method'),// <-- get method of form
url: $(this).attr('action'),
//url: "cart.php",
data: $(this).serialize(), // <-- serialize all fields into a string that is ready to be posted to your PHP file
beforeSend: function(){
},
success: function(data){
$( "#preloader" ).fadeOut( 850 );
}
});
});
});
//////////////////////LETS RUN LOAD THE cart-header.php ON PAGE LOAD USING AJAX //////////////
$(document).ready(function () {
function load1() {
$.ajax({ //create an ajax request to load_page.php
type: "GET",
url: "cart-header.php",
dataType: "html", //expect html to be returned
success: function (data2) {
$('#headerCart').html($(data2));
//setTimeout(load2, 500);
}
});
}
load1();
});
//////////////////////LETS LOAD THE cart-header.php on form1 submit USING AJAX //////////////
<!----- This is the part that SOMETIMES Fails to work --->
$(function(){
$('.form1').on('submit', function(load2){
// prevent native form submission here
load2.preventDefault();
// now do whatever you want here
$.ajax({
type: "GET",// <-- get method of form
url: "cart-header.php",
//url: "cart.php",
dataType: "html", // <-- serialize all fields into a string that is ready to be posted to your PHP file
beforeSend: function(){
},
success: function(data){
//$('#test').load('cart.php #total').html();
$('#headerCart').html($(data));
}
});
});
});
//////////////////////LETS RUN OUR DELETE FROM CART FEATURE USING AJAX //////////////
$(document).on('submit', '.delForm', function(dleItem){
// prevent native form submission here
dleItem.preventDefault();
// now do whatever you want here
$.ajax({
type: $(this).attr('method'),// <-- get method of form
url: "cart-header.php",
//url: "cart.php",
data: $(this).serialize(), // <-- serialize all fields into a string that is ready to be posted to your PHP file
beforeSend: function(){
},
success: function(data){
$('#headerCart').html($(data));
}
});
});
});
//////////////////////LETS GET THE QUANTITY OF CURRENT ITEMS ADDED IN THE CART USING AJAX/////////////
$(document).ready(function () {
function load() {
$.ajax({ //create an ajax request to load_page.php
type: "GET",
url: "cart.php",
//url: "cart-header.php",
dataType: "html", //expect html to be returned
success: function (data) {
$('.item_count').html($(data).find('#total').text());
//$('#headerCart').html($(data));
setTimeout(load, 1000);
}
});
}
load();
});
I have commented the code so you can see the parts of the code and what they do.
the issue is this part:
//////////////////////LETS LOAD THE cart-header.php on form1 submit USING AJAX //////////////
<!----- This is the part that SOMETIMES Fails to work --->
$(function(){
$('.form1').on('submit', function(load2){
// prevent native form submission here
load2.preventDefault();
// now do whatever you want here
$.ajax({
type: "GET",// <-- get method of form
url: "cart-header.php",
//url: "cart.php",
dataType: "html", // <-- serialize all fields into a string that is ready to be posted to your PHP file
beforeSend: function(){
},
success: function(data){
//$('#test').load('cart.php #total').html();
$('#headerCart').html($(data));
}
});
});
});
As I mentioned above, this code works fine but it only works when it wants to as if it has mind of its own!
could someone please advise on this issue?
Thanks in advance.
I know this has been asked before, but I can't get it working.
I'm loading a file on a page whose path and name are a variable contained in a PHP script.
My script.php is outputing a variable $filename which contains the path to a file that has to be open in an ajax request.
So, the file can be for example:
'../path/to/file/filea.json' or
'../another/path/fileb.json'
I tried this in my jQuery:
$.ajax({
url:'script.php',
success:$.ajax({
url: ??? // This ($filename) is what I'm trying to get from the 1st Ajax call
sucess: function(data){
//other code here
}
})
);
I know $filename is wrong in the second Ajax call but how do I get the value of that variable?
$.ajax({
url: "script.php",
success: function(data){
$.ajax({
var someParam=data; //response Data from 1st Ajax Call
type: "post",
url: "example.php",
data: 'page='+someParam,
success: function(data){
//Do More Here!
});
});
Try this:
inside script.php
echo json_encode($filename); //somewhere you need to have this echo.
jQuery
$.ajax({
url: 'script.php',
success: function (data) {
alert(data); //or console.log() and doublecheck you have the right info
$.ajax({
url: JSON.parse(data),
success : function (data) {
//other code here
}
})
}
}); // added a extra } to close the ajax
This solved the problem:
$.ajax({
async:false,
url: 'script.php',
success: function(data){
$.ajax({
url: data,
success: //code here
})
})
#Sergio: script.php wasn't echoeing the output of $filename... Thanks for reminding!
I want to pass values to a PHP script so i am using AJAX to pass those, and in the same function I am using another AJAX to retrieve those values.
The problem is that the second AJAX is not retrieving any value from the PHP file. Why is this? How can I store the variable passed on to the PHP script so that the second AJAX can retrieve it?
My code is as follows:
AJAX CODE:
$(document).ready(function() {
$("#raaagh").click(function(){
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: 145}),
success: function(data){
console.log(data);
}
});
$.ajax({
url:'ajax.php',
data:"",
dataType:'json',
success:function(data1){
var y1=data1;
console.log(data1);
}
});
});
});
PHP CODE:
<?php
$userAnswer = $_POST['name'];
echo json_encode($userAnswer);
?>
Use dataType:"json" for json data
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
dataType:'json', // add json datatype to get json
data: ({name: 145}),
success: function(data){
console.log(data);
}
});
Read Docs http://api.jquery.com/jQuery.ajax/
Also in PHP
<?php
$userAnswer = $_POST['name'];
$sql="SELECT * FROM <tablename> where color='".$userAnswer."'" ;
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
// for first row only and suppose table having data
echo json_encode($row); // pass array in json_encode
?>
No need to use second ajax function, you can get it back on success inside a function, another issue here is you don't know when the first ajax call finished, then, even if you use SESSION you may not get it within second AJAX call.
SO, I recommend using one AJAX call and get the value with success.
example: in first ajax call
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: 145}),
success: function(data){
console.log(data);
alert(data);
//or if the data is JSON
var jdata = jQuery.parseJSON(data);
}
});
$(document).ready(function() {
$("#raaagh").click(function() {
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: 145}),
success: function(data) {
console.log(data);
$.ajax({
url:'ajax.php',
data: data,
dataType:'json',
success:function(data1) {
var y1=data1;
console.log(data1);
}
});
}
});
});
});
Use like this, first make a ajax call to get data, then your php function will return u the result which u wil get in data and pass that data to the new ajax call
In your PhP file there's going to be a variable called $_REQUEST and it contains an array with all the data send from Javascript to PhP using AJAX.
Try this: var_dump($_REQUEST); and check if you're receiving the values.
you have to pass values with the single quotes
$(document).ready(function() {
$("#raaagh").click(function(){
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: '145'}), //variables should be pass like this
success: function(data){
console.log(data);
}
});
$.ajax({
url:'ajax.php',
data:"",
dataType:'json',
success:function(data1){
var y1=data1;
console.log(data1);
}
});
});
});
try it it may work.......