Actually the following function works fine, but now I need to add other variable in order to return from the php file the right statement.
function sssssss1(page) {
loading_show();
$.ajax({
type: "GET",
url: "load_data.php",
data: "page=" + page,
success: function (msg) {
$("#search").ajaxComplete(function (event, request, settings) {
loading_hide();
$("#search").html(msg);
});
}
});
}
I need to add the following two variable to be read by my php file. I have tried different solution, but nothing seem working
var form2 = document.myform2;
var dataString1 = $(form2).serialize();
How to add those variable in my existing function? Any idea?
You can send object as data,
this line:
data: "page="+page,
could be
data: {mypage:"page="+page, form2:document.myform2, dataString1:$(form2).serialize()}
and your PHP can get it like:
$page = $_GET['mypage'];
$form2 = $_GET['form2'];
$dataString = $_GET['dataString1'];
Hope it Help.
Related
I am fairly new to Laravel and ajax in general, what I am trying to implement is to pass the value of an input field through an ajax get request.
My request looks like this:
function getInfo() {
$.ajax({
url: "info",
dataType: "json"
}).success(function(data){
$('#result').append(JSON.stringify(data));
}).fail(function(){alert('Unable to fetch data!');;
});
}
$('#infoSubmit').click(getInfo);
I have setup a route for my function in laravel that works like this
public/info/Variable <--
When I add a variable after info/
I get the data for that variable (e.g profile name)
I need to pass this variable from an inputfield to ajax request to something like this:
url: "info/+$inputfieldVariable"
Change:
url: "info",
TO:
url: "info/" + $('input-field-selector').val(),
Not sure about the correctness of your JS code: Shouldn't you be using done instead of success?
JavaScript:
function getInfo() {
var myFieldsValue = {};
var $inputs = $("#myForm :input");
$inputs.each(function() {
myFieldsValue[this.name] = $(this).val();
});
$.ajax({
url: "info",
dataType: "json",
data: myFieldsValue,
type: "GET" // Even if its the default value... looks clearer
success: function(data){
$('#result').append(JSON.stringify(data));
},
error: function(){
alert('Unable to fetch data!');
}
});
return false;
}
$('#infoSubmit').click(getInfo);
Untested but should be something like that
I have two files filter.php and products.php included to index.php and I am using .post submit form without refreshing. I send data to products.php and return it back also i refresh my filter and there is my problem. When i try to var dump post data in filter.php it is empty
Here is my script
function ajaxFilter()
{
var str = jQuery( "form[name=\"filters\"]" ).serialize();
jQuery.post( "/", str )
.done(function( data ) {
var productList = jQuery(data).find(".list_product").html();
var filter = jQuery(data).find(".filters").html();
jQuery(".list_product").html(productList);
jQuery(".filters").html(filter);
});
}
Any ideas how to get POST?
I though about putting my post via script to hidden inputs and return them also as html
If it is bad idea or just wrong start.
try this:
var Data = $("form[name=\"filters\"]").serializeArray();
var URL = "products.php"; // whatever filepath where you send data
jQuery.ajax({
type: "POST",
url: URL,
processData: true,
data: Data,
dataType: "html",
success: function (productList) {
// filter your result whatever you return from products.php
jQuery(".list_product").html(productList);
},
error: function (x, y, z) {
var a = x.responseText;
jQuery(".list_product").html(a);
}
});
Use the full URL.
Provide the return values of .error() if you still have problems.
I have this php function inside a class the returns json data
function getPhotoDetails( $photoId ) {
$url = $this::APP_URL . 'media/' . $photoId . $this::APP_ID;
return $this->connectToApi($url);
}
and this ajax request
function getPhotoDetails( photoId ) {
$.ajax({
type: "GET",
cache: false,
url: 'index.php',
success: function (data) {
console.log(data);
}
});
}
The question is how I can call the php function to get the json data.
Solution:
A big thanks to all of you guys and thanks to Poonam
The right code
PHP:
I created a new object instance in php file
$photoDetail = new MyClass;
if(isset($_REQUEST['image_id'])){
$id = $_REQUEST['image_id'];
echo (($photoDetail->getPhotoDetails($id)));
}
JavaScript
function getPhotoDetails( photoId ) {
$.ajax({
type: "GET",
cache: false,
url: './instagram.php?image_id=' + photoId,
success: function (data) {
var data = $.parseJSON(data);
console.log(data);
}
});
}
Try with setting some parameter to identify that details needs to send for e.g assuming photoid params needed for function
function getPhotoDetails( photoId ) {
$.ajax({
type: "GET",
cache: false,
url: 'index.php?sendPhoto=1&photoid=23',
success: function (data) {
console.log(data);
}
});
}
and then on index.php check (You can make check for photoid whatever you need as per requirement)
if(isset($_REQUEST['sendPhoto'])){
$id = $_REQUEST['photoid'];
return getPhotoDetails($id);
}
setup a switch-case. Pass the function name as GET or POST variable such that it calls the php function
You need a file which calls the PHP function. You can't just call PHP functions from Ajax. And as pointed out by Tim G, it needs to use the proper header, format the code as JSON, and echo the return value (if the function is not already doing these things).
I have a function that does a simple call to a PHP file with a simple INSERT statement in it. When I click the corresponding button, the function does not even send the AJAX request or return false.
$(function() {
$('#add_employee_submit').click(function() {
var fname = $('#eie_fname').text();
var lname = $('#eie_lname').text();
var doNo = $('#eie_doNo').val();
var emergency = 0;
if($('#eie_emergency').is(':checked')) {
emergency = 1;
}
$.ajax({
type: "POST",
url: "scripts/employee_information_entry.php",
data: "fname="+fname+"&lname="+lname+"&dono="+doNo+"&emergency="+emergency,
success: function() {
showMessage('employee_added');
}
});
return false;
});
});
Am I missing something obvious here?
EDIT:
Instead of wasting more time trying to figure out what was wrong I went with #ThiefMaster's advice and used jquery.malsup.com/form. This is working correctly and is a lot let work.
Thanks for everyone's help. :)
The success function should have a response parameter i.e. your code would be like:
$.ajax({
type: "POST",
url: "scripts/employee_information_entry.php",
data: "fname="+fname+"&lname="+lname+"&dono="+doNo+"&emergency="+emergency,
success: function(response) {
showMessage('employee_added');
}
});
Also check if the various id's are present there or not.
The best method to check is to use firebug/developer console to check what parameters are being sent and what are being received.
I've created one Ajax function like this:
function searchPlanAjax(url){
jQuery('#loader').css('display','block');
var plan = jQuery('.rad').val();
var site = url+"fbilling_details/subscription";
jQuery.ajax({
type: "POST",
url: site,
data: "plan="+plan,
//data: "business_name="+val+"&bs="+bs,
success: function(msg){
jQuery('#loader').css('display','none');
}
});
}
Which I'm calling on radio button's onclick:
<?php echo $form->text(BILLING_DETAIL.'][id_plan', array('type'=>'radio', 'value'=>$val[PLAN]['id'], 'id'=>'id_plan_'.$val[PLAN]['id'], 'class'=>'rad','onclick'=>'searchPlanAjax('."'".SITE_NAME.ROOT_FOLDER_NAME."'".')')); ?>
The Ajax call will be processed in controller of cakePHP like this:
if(!empty($_REQUEST['plan'])){
$search_plan = $this->{PLAN}->find("all",array("conditions"=>array("id=".$_REQUEST['plan'])));
$this->set('search_plan',$search_plan);
}
but I couldn't get value in $search_plan variable. thanks if anybody can help.
Look at conditions in your find query
if(!empty($_REQUEST['plan'])){
$search_plan = $this->{PLAN}->find("all",array("conditions"=>array("id" => $_REQUEST['plan'])));
$this->set('search_plan',$search_plan);
}