submitting form with Jquery submithandler - php

I can get Ajax to send the request successfully but I want it to also redirect to the new page and it's not redirecting. Could anyone suggest an alternative method or correct way to redirect?
submitHandler: function(form) {
$("div.error").hide();
$.ajax({
type: "POST",
url: '<?php echo base_url().'subscribe/process_payment/'?>',
data: $(form).serialize(),
success: function(msg){
console.log( "Data Saved: " + msg );
},
error: function(msg){
console.log( "Error: " + msg);
}
});
return true;
},

Usually you can simply do the redirect in the success function
success: function(msg){
console.log( "Data Saved: " + msg );
window.location = "mysite.com/newURL";
},
or return the url
success: function(url){
window.location = url;
},

Related

Wordpress Ajax always returns 0 despite success in Ajax call

I am trying to read from Wordpress's wp_users table (via Ajax request), and view it in the page. I need your help let me know what I have done wrong to successfully print the root email address by using Ajax.
My JavaScript ajax call is:
function ajaxTest ()
{
// Creating the URL that Ajax must refer to.
var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
// Do Ajax call.
jQuery.ajax({
url: ajaxurl,
type: "POST",
data: { action: "wp_ajax_tester" },
complete: function( xhr, status ){
console.log("Request completion status: " + status);
},
success: function (response){
console.log("result: " + response);
},
error: function(errorThrown){
console.log("error: " + errorThrown);
}
});
}
And in the functions.php I have created the code below:
function app_tester()
{
global $wpdb;
$sql = "SELECT user_email FROM wp_users WHERE user='root'";
$results = $wpdb->get_results($sql, OBJECT);
echo json_encode($results;);
wp_die();
}
add_action('wp_ajax_tester', 'app_tester');
add_action('wp_ajax_nopriv_tester', 'app_tester');
I want to print out the root email address, but this is what printed in the browser's console:
result: 0
Request completion status: Successful
I am using Wordpress's own jQuery by putting the line below in the files head section:
<?php wp_enqueue_script("jquery"); ?>
<?php wp_head(); ?>
Here is documentation for Ajax, please remove wp_ajax_ from action
Example code
function ajaxTest ()
{
// Creating the URL that Ajax must refer to.
var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
// Do Ajax call.
jQuery.ajax({
url: ajaxurl,
type: "POST",
data: { action: "tester" },
complete: function( xhr, status ){
console.log("Request completion status: " + status);
},
success: function (response){
console.log("result: " + response);
},
error: function(errorThrown){
console.log("error: " + errorThrown);
}
});
}

Getting data from PHP to AJAX

I'm trying to send some data from PHP back to AJAX. I found some examples but it doesn't seem to work. The result of console log is: "test: success". How can I get the data?
$.ajax({
url: "assets/psv.php",
method: "POST",
dataType: "HTML",
success: function(results, test){
console.log("test:" + test);
},
error : function (e) {
console.log("error " + e);
}
});
PHP
$test= "pgv";
echo $test;
Try:
$.ajax({
url: "assets/psv.php",
method: "GET",
success: function(data){
console.log("test:" + data);
},
error : function (e) {
console.log("error " + e);
}
});
or something like this:
$.get( "assets/psv.php", function( data ) {
alert( "Data Loaded: " + data );
});
The first variable in the success callback contains data received from the page you called.
PHP
header('Content-Type: application/json');
$test= "pgv";
echo json_encode($test);
JS
$.ajax({
url: "assets/psv.php",
method: "GET",
dataType: "json",
success: function(response, status){
console.log("test", response.data); //CHANGE THIS!!
},
error : function (e) {
console.log("error " + e);
}
});
data will contain
{
headers: "...",
data: "pgv",
....
}

How handle errors in php script fired by Jquery.ajax?

I have php-script, firing with jquery ajax function. Somthing like this:
$("a.test").click (function () {
var new_id = $(this).attr("new_id");
$.ajax({
url: 'test.php',
type: "POST",
cache: false,
async: true,
data: ({
new_id : new_id
}),
success: function (data) {
alert (data);
},
error: function(){
alert('error');
}
});
return false;
});
Now, a have some errors in test.php, but I can't see them. Sript just runs and I have no feedback, only error alert (alert ('error')).
How can I get back errors, that I have in test.php to handle them?
If you echo the errors in test.php, you can simply do:
$.ajax({
url: 'test.php',
type: "POST",
cache: false,
async: true,
data: ({
new_id : new_id
}),
success: function (data) {
alert (data);
},
error: function(data){
alert('error:'+data);
}
});
return false;
});
Edit:
I usually do something like this. In test.php if you get an error, create an array with your error info and echo it json encoded:
$message=array('error' => 1,'message' => '<div class="alert alert-danger" role="alert">' . $login['message'] . '</div>' );
echo json_encode($message);
Now in your jquery you can retrive the error by:
success: function (data) {
alert (data);
},
error: function(data){
var obj = JSON.parse(data);
alert(obj.message);
}
When you have it in array like this you dont even need error: function(data) anymore, since you can simply:
success: function (data) {
var obj = JSON.parse(data);
if (obj.error) {
// do something
alert (obj.message);
}else{
// do something else
}
},
On test.php you could show errors using the code explained here: https://stackoverflow.com/a/21429652/6525724
And then on this page instead of alert('error') you could use alert(data).
Try this
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus); alert("Error: " + errorThrown);
}

How do i submit a form using get method in third party url using jquery and ajax?

I want to submit a form using ajax with GET method in my word press project.
i tried two method but that not work for me there is any problem in my code.
Method-1 Using $.get
$.get( "http://**.70.120.**/web/sms.aspx", { fullname: "John", contactnumber: "123333",Email_ID:"aniruddha#thinkbar.in",State:"MP",City:"Indore",Magma_Customer:"YES",Proposal_Number:"1234567890",Service_type:"CASE",Query_Type:"Test type",Message:"Hellotesting" } )
.done(function( data ) {
alert( "Data Loaded: " + data );
})
.fail(function( data ) {
alert( "Data NOT Loaded: " + data );
});
Method-2 using Ajax
$.ajax({
method: "GET",
url: "http://**.70.120.**/webleads/sms.aspx",
data: {fullname: "John", contactnumber: "123333",Email_ID:"aniruddha#thinkbar.in",State:"MP",City:"Indore",Magma_Customer:"YES",Proposal_Number:"1234567890",Service_type:"CASE",Query_Type:"Test type",Message:"Hellotesting" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
})
.fail(function( msg ) {
alert( "Data NOT Saved: " + msg );
})
Please help
You can do it like this
$.ajax({
type: 'GET',
url: 'http://**.70.120.**/webleads/sms.aspx',
data: {
'fullname': 'John',
'contactnumber': '123333',
'Email_ID': 'aniruddha#thinkbar.in',
'State': 'MP',
'City': 'Indore',
'Magma_Customer': 'YES',
'Proposal_Number': '1234567890',
'Service_type': 'CASE',
'Query_Type': 'Test type',
'Message': 'Hellotesting'
},
dataType: 'application/x-www-form-urlencoded',
success: function(response) {
console.log( response );
},
error: function(errorThrown) {
console.log( errorThrown );
},
});
the url that will be submitted on your server will be http://**.70.120.**/webleads/sms.aspx/?fullname=John&contactnumber=123333&Email_ID=aniruddha#thinkbar.in&State=MP&City=Indore&Magma_Customer=YES&Proposal_Number=1234567890&Service_type=CASE&Query_Type=Test%20type&Message=Hellotesting
if you want to do using ajax
$.ajax({
data: { fullname: "John", contactnumber: "123333",Email_ID:"aniruddha#thinkbar.in",State:"MP",City:"Indore",Magma_Customer:"YES",Proposal_Number:"1234567890",Service_type:"CASE",Query_Type:"Test type",Message:"Hellotesting" },
url: "http://**.70.120.**/web/sms.aspx",
type: "GET",
success: function(msg){
//some function here
}
});

How to Write PHP in AJAX

I am trying to get my session in ajax.. for that i had written my code like this
BTLJ.ajax({
type: "POST",
url: btlOpt.BT_AJAX,
data: datasubmit,
success: function(html){
//if html contain "Registration failed" is register fail
BTLJ("#btl-register-in-process").hide();
if(html.indexOf('$error$')!= -1){
...
...
}
}else{
BTLJ(".btl-formregistration").children("div").hide();
BTLJ("#btl-success").html(html);
BTLJ("#btl-success").show();
alert(<?php session_start(); print_r($_SESSION); ?>);
setTimeout(function() { ); BTLJ(".kcregbox").show();},7000);
// BTLJ("#btl-success").hide();
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus + ': Ajax request failed');
}
});
but ajax is not working if i write like that.. please help me in getting my session in ajax. thanks in advance.
Your AJAX query in Joomla should be formatted as below.
jQuery.post('index.php',{
'option' : 'com_componentname',
'controller' : 'controllername',
'task' : 'task_name',
'format' : 'raw',
'data' : data
}).success(function(result) {
//if the request is success
}).error(function() {
//if the request is fails
});
I'm using this format for ajax in joomla
$.ajax({
type: 'GET',
url: 'index.php',
data: {option: 'com_componenetname', task: 'taskname.youroperation', format: 'json', tmpl: 'raw'},
dataType: 'json',
async: true, // can be false also
error: function(xhr, status, error) {
console.log("AJAX ERROR in taskToggleSuceess: ")
var err = eval("(" + xhr.responseText + ")");
console.log(err.Message);
},
success: function(response){
// on success do something
// use response.valuname for server's data
}
,
complete: function() {
// stop waiting if necessary
}
});
In your component/controllers you should have a file yourcontroller.json.php which will process your call and return encoded json array will all the data you need in the client

Categories