I need to run some PHP code from a file when I click a button from jQuery. This is the code I have:
$(document).ready(function(){
$("#btnSubmit").button().click(function(){
alert("button 1");
$.ajax({
url: 'releaseBackEnd.php',
type: 'POST',
async: false,
data: {},
dataType: 'xml',
error: function(){
alert('Error');
},
success: function(data){
//check error
alert("success!");
}
});
});
});
It shows the alert of "error". I have the releaseBackEnd.php file in the same directory of the javascript file.
check the response in firebug console .. if u don't have firebug add it to your firefox using this link
https://addons.mozilla.org/en-US/firefox/addon/1843/
Change
$("#btnSubmit").button().click(
to
$("#btnSubmit").click(
and check whether the php page is sending the response or not.
Try getting the error in the error callback function using the XMLHttpRequest.
error(XMLHttpRequest, textStatus) {
}
Try this code...
$(document).ready(function(){
$("#btnSubmit").bind('click',function() {
alert("button 1");
$.ajax({
url: 'releaseBackEnd.php',
type: 'POST',
async: false,
data: {},
dataType: 'xml',
error: function(){
alert('Error');
},
success: function(data){
//check error
alert("success!");
}
});
});
});
Related
I'm learning PHP and JS, and while doing a Get with Ajax, the PH return is coming in white. I'm turning the net but not finding anything, I'd like some help.
$idEvent = $_REQUEST["idEvent"];
$event = $this->model->getById($idEvent);
header('Content-type: application/json');
echo json_encode($event, JSON_PRETTY_PRINT);
exit;
$('#ShowEvent').click(function() {
var idEvent = document.getElementById("ShowEvent").getAttribute("idEvent");
$.ajax({
url: 'http://localhost/salgadar/public_html/Event/GetEventById',
type: 'get',
data: {
idEvent: idEvent
},
dataType: 'JSON',
success: function(data) {
alert('AJAX call was successful!');
$("#AjaxReturn").html(data)
},
error: function() {
alert('There was some error performing the AJAX call!');
}
});
console.log(data);
});
ReferenceError: data is not defined
First place the console.log() inside your ajax call, like this:
$.ajax({
url: 'http://localhost/salgadar/public_html/Event/GetEventById',
type: 'get',
data: {
idEvent: idEvent
},
dataType: 'JSON',
success: function(data) {
alert('AJAX call was successful!');
console.log(data); //HERE CODE
$("#AjaxReturn").html(data);
},
error: function() {
alert('There was some error performing the AJAX call!');
}
});
Then tell me what appears and we will continue to solve the problems.
I am trying to send some data with post to a php file but for some reason its not being posted. I receive an empty array in console log from the php file on var_dump.
Here is code I have in these files:
index.php
<button type="button" class="btn btn-warning" id="resetPassword">Reset Password</button>
<script>
$(document).ready(function (e){
$("#resetPassword").click(function(e){
e.preventDefault();
$.ajax({
url: "scripts/process.php",
type: "POST",
data: {name: 'test'},
cache: false,
processData:false,
success: function(data) {
if (data === 'Success') {
new PNotify({
title: 'Success!!!',
text: 'Request completed successfully.',
type: 'success',
styling: 'bootstrap3'
});
$('#exampleModal').modal('hide');
$('#datatable-buttons').DataTable().ajax.reload();
} else {
console.log(data);
new PNotify({
title: 'Error!!!',
text: data,
type: 'error',
styling: 'bootstrap3'
});
//document.getElementById("status").className += " alert-danger";
//$("#status").html(data);
}
},
error: function (xhr, ajaxOptions, thrownError) {
alert("ERROR:" + xhr.responseText+" - "+thrownError);
}
});
});
});
</script>
Here is the php file process.php.
var_dump($_POST);
Result in console.log:
array(0) {}
Solution:
Remove processData: false
thanks guys.
Remove processData:false, section and then try again.
Try this
before or after
e.preventDefault();
var formData = $("#yourform_id").serializeArray(); //form data
and replace data with this line datas
data: formData,
remove
processData:false,
What you are using while returning data in process.php file? ECHO or RETURN?
Try ECHO, if it does not work then try RETURN statement. Rest of your code seems OK.
To debug use print_r() on scripts/process.php and check it in browser's console for out put. Also, you can use toSource of javascript to check variable's data
If you try by processData:false ,its need data is raw string
change from
data: {name: 'test'},
into
data: "name=test",
i would like to get an json type value from an mvc controller method. everything is correct but an error occures'.
my jquery ajax function:
function user_login(uname,pass){
$.ajax({
url: 'http://localhost/s/login_request',
type:'POST',
data:{uname:uname,pass:pass},
dataType:"json",
cache: false,
})
.done(function(response){
//do something
alert('1234');
})
.fail(function(jqXHR,textStatus){
alert(JSON.stringify(jqXHR));
});
}
and here is my php code(mvc controller method):
function login_request(){
header('Content-Type: application/json');
echo json_encode(array('testvalue'));
}
when i run the code, the .fail section executed and the following value was returned:
{"readyState":4,"responseText":"[\"testvalue\"]","status":200,"statusText":"OK"}
how can i solve this? thanks...
Try using
$.ajax({
url: 'http://localhost/s/login_request',
type:'POST',
data:{uname:uname,pass:pass},
dataType:"json",
cache: false,
success: function(data) { },
fail: function(xhr, status) { alert(JSON.stringify(xhr)) },
error: function() { },
complete: function() { alert('1234') }
});
I am using ajax for sending to the server that a checkbox is checked and now other inputs fields need to be required:
EDIT: ok i will change the question to be more clarified.
i have this code:
$(document).ready(function() {
$('#button_ajax').click(function() {
var request = jQuery.ajax({
type: 'get',
url: 'http://orenmizrah.git/nir1.php',
data: {'id1': $('#input_text').val()},
async: true,
success: function(){
console.log("sent");
}
});
});
and now i check that i got the info in $_GET['id1'] with php code and if $_GET['id1'] is set, echo something to the browser.
the problem is that $_GET['id1'] is set but there is no output to the browser.
what i should do?
No output is shown in the browser because nothing is done to show it.
$(document).ready(function() {
$('#button_ajax').click(function() {
var request = jQuery.ajax({
type: 'get',
url: 'http://orenmizrah.git/nir1.php',
data: {'id1': $('#input_text').val()},
async: true,
success: function(data){
console.log(data); // outputs the returned data to the console
}
});
});
I create a JSON object with fisrtTest.php
The JSON is correct when I open this page with WampServer ..
But I cannot do a Ajax request :/
Why ? Cross domain policy ?
$.getJSON('http://localhost/tests/fisrtTest.php',
success
);
function success(data) {
}
This is for a mobile app with phonegap
$(document).ready(function(){
$("#ibutton").click(function() {
alert("go");
$.ajax({
url: "http://localhost:8080/tests/fisrtTest.php?callback=?",
dataType: "json",
crossDomain: true,
success: function(data) {
}
});
});
});
Well, this works but firebug gets an error :
SyntaxError: invalid label
{"jack":1,"gt":2,"c":3,"d":4,"e":5}
What is your error? Can you view JSON response in the firebug console?
Below is working code
$(document).ready(function(){
$("#ibutton").click(function() {
alert("go");
$.ajax({
url: "http://localhost:8080/tests/fisrtTest.php",
dataType: "jsonp",
crossDomain: true,
jsonpCallback: "CallBackFunction"
});
});
});
function CallBackFunction(json){
//
};