Redirect Form After Validated












0















I'm trying to redirect the form on https://sodacitysolar.com/consultation.html to https://sodacitysolar.com/thanks.html once the form is validated. I've traced the code and believe the solution lies somewhere below, which is the form.js file:



$(function()
{
function after_form_submitted(data)
{
if(data.result == 'success')
{
$('form#reused_form').hide();
$('#success_message').show();
$('#error_message').hide();
}
else
{
$('#error_message').append('<ul></ul>');

jQuery.each(data.errors,function(key,val)
{
$('#error_message ul').append('<li>'+key+':'+val+'</li>');
});
$('#success_message').hide();
$('#error_message').show();

//reverse the response on the button
$('button[type="button"]', $form).each(function()
{
$btn = $(this);
label = $btn.prop('orig_label');
if(label)
{
$btn.prop('type','submit' );
$btn.text(label);
$btn.prop('orig_label','');
}
});

}//else
}

$('#reused_form').submit(function(e)
{
e.preventDefault();

$form = $(this);
//show some response on the button
$('button[type="submit"]', $form).each(function()
{
$btn = $(this);
$btn.prop('type','button' );
$btn.prop('orig_label',$btn.text());
$btn.text('Sending ...');
});


$.ajax({
type: "POST",
url: 'handler.php',
data: $form.serialize(),
success: after_form_submitted,
dataType: 'json'
});

});
});


I've tried to replace this code with the code below but it's not working:



$('form#reused_form').hide();
$('#success_message').show();
$('#error_message').hide();



function redirect()
{
window.location.href="thanks.html";
}


Any chance you guys could point me in the right direction?



EDIT:



Here's the code I was trying to use:



$(function()
{
function after_form_submitted(data)
{
if(data.result == 'success')
{
function redirect()
{
window.location.href="thanks.html";
}
}
else
{
$('#error_message').append('<ul></ul>');

jQuery.each(data.errors,function(key,val)
{
$('#error_message ul').append('<li>'+key+':'+val+'</li>');
});
$('#success_message').hide();
$('#error_message').show();

//reverse the response on the button
$('button[type="button"]', $form).each(function()
{
$btn = $(this);
label = $btn.prop('orig_label');
if(label)
{
$btn.prop('type','submit' );
$btn.text(label);
$btn.prop('orig_label','');
}
});

}//else
}

$('#reused_form').submit(function(e)
{
e.preventDefault();

$form = $(this);
//show some response on the button
$('button[type="submit"]', $form).each(function()
{
$btn = $(this);
$btn.prop('type','button' );
$btn.prop('orig_label',$btn.text());
$btn.text('Sending ...');
});


$.ajax({
type: "POST",
url: 'handler.php',
data: $form.serialize(),
success: after_form_submitted,
dataType: 'json'
});

});
});









share|improve this question

























  • Where do you actually call the redirect function?

    – Bibberty
    Jan 1 at 19:32











  • Pseudo code if(myValudation() === true) redirect();

    – Bibberty
    Jan 1 at 19:34











  • @Bibberty just added the code I tried to use and where I called the redirect function.

    – BillyW
    Jan 2 at 0:04











  • ok, you have wrapped the redirect in a function inside success. This prevents it being called. get rid of the function declaration and just leave window.location.href="thanks.html"; This should solve the problem (if it is successful. Have you looked in chrome debug?)

    – Bibberty
    Jan 2 at 0:08











  • @samuel has corrected this for you below.

    – Bibberty
    Jan 2 at 0:09
















0















I'm trying to redirect the form on https://sodacitysolar.com/consultation.html to https://sodacitysolar.com/thanks.html once the form is validated. I've traced the code and believe the solution lies somewhere below, which is the form.js file:



$(function()
{
function after_form_submitted(data)
{
if(data.result == 'success')
{
$('form#reused_form').hide();
$('#success_message').show();
$('#error_message').hide();
}
else
{
$('#error_message').append('<ul></ul>');

jQuery.each(data.errors,function(key,val)
{
$('#error_message ul').append('<li>'+key+':'+val+'</li>');
});
$('#success_message').hide();
$('#error_message').show();

//reverse the response on the button
$('button[type="button"]', $form).each(function()
{
$btn = $(this);
label = $btn.prop('orig_label');
if(label)
{
$btn.prop('type','submit' );
$btn.text(label);
$btn.prop('orig_label','');
}
});

}//else
}

$('#reused_form').submit(function(e)
{
e.preventDefault();

$form = $(this);
//show some response on the button
$('button[type="submit"]', $form).each(function()
{
$btn = $(this);
$btn.prop('type','button' );
$btn.prop('orig_label',$btn.text());
$btn.text('Sending ...');
});


$.ajax({
type: "POST",
url: 'handler.php',
data: $form.serialize(),
success: after_form_submitted,
dataType: 'json'
});

});
});


I've tried to replace this code with the code below but it's not working:



$('form#reused_form').hide();
$('#success_message').show();
$('#error_message').hide();



function redirect()
{
window.location.href="thanks.html";
}


Any chance you guys could point me in the right direction?



EDIT:



Here's the code I was trying to use:



$(function()
{
function after_form_submitted(data)
{
if(data.result == 'success')
{
function redirect()
{
window.location.href="thanks.html";
}
}
else
{
$('#error_message').append('<ul></ul>');

jQuery.each(data.errors,function(key,val)
{
$('#error_message ul').append('<li>'+key+':'+val+'</li>');
});
$('#success_message').hide();
$('#error_message').show();

//reverse the response on the button
$('button[type="button"]', $form).each(function()
{
$btn = $(this);
label = $btn.prop('orig_label');
if(label)
{
$btn.prop('type','submit' );
$btn.text(label);
$btn.prop('orig_label','');
}
});

}//else
}

$('#reused_form').submit(function(e)
{
e.preventDefault();

$form = $(this);
//show some response on the button
$('button[type="submit"]', $form).each(function()
{
$btn = $(this);
$btn.prop('type','button' );
$btn.prop('orig_label',$btn.text());
$btn.text('Sending ...');
});


$.ajax({
type: "POST",
url: 'handler.php',
data: $form.serialize(),
success: after_form_submitted,
dataType: 'json'
});

});
});









share|improve this question

























  • Where do you actually call the redirect function?

    – Bibberty
    Jan 1 at 19:32











  • Pseudo code if(myValudation() === true) redirect();

    – Bibberty
    Jan 1 at 19:34











  • @Bibberty just added the code I tried to use and where I called the redirect function.

    – BillyW
    Jan 2 at 0:04











  • ok, you have wrapped the redirect in a function inside success. This prevents it being called. get rid of the function declaration and just leave window.location.href="thanks.html"; This should solve the problem (if it is successful. Have you looked in chrome debug?)

    – Bibberty
    Jan 2 at 0:08











  • @samuel has corrected this for you below.

    – Bibberty
    Jan 2 at 0:09














0












0








0








I'm trying to redirect the form on https://sodacitysolar.com/consultation.html to https://sodacitysolar.com/thanks.html once the form is validated. I've traced the code and believe the solution lies somewhere below, which is the form.js file:



$(function()
{
function after_form_submitted(data)
{
if(data.result == 'success')
{
$('form#reused_form').hide();
$('#success_message').show();
$('#error_message').hide();
}
else
{
$('#error_message').append('<ul></ul>');

jQuery.each(data.errors,function(key,val)
{
$('#error_message ul').append('<li>'+key+':'+val+'</li>');
});
$('#success_message').hide();
$('#error_message').show();

//reverse the response on the button
$('button[type="button"]', $form).each(function()
{
$btn = $(this);
label = $btn.prop('orig_label');
if(label)
{
$btn.prop('type','submit' );
$btn.text(label);
$btn.prop('orig_label','');
}
});

}//else
}

$('#reused_form').submit(function(e)
{
e.preventDefault();

$form = $(this);
//show some response on the button
$('button[type="submit"]', $form).each(function()
{
$btn = $(this);
$btn.prop('type','button' );
$btn.prop('orig_label',$btn.text());
$btn.text('Sending ...');
});


$.ajax({
type: "POST",
url: 'handler.php',
data: $form.serialize(),
success: after_form_submitted,
dataType: 'json'
});

});
});


I've tried to replace this code with the code below but it's not working:



$('form#reused_form').hide();
$('#success_message').show();
$('#error_message').hide();



function redirect()
{
window.location.href="thanks.html";
}


Any chance you guys could point me in the right direction?



EDIT:



Here's the code I was trying to use:



$(function()
{
function after_form_submitted(data)
{
if(data.result == 'success')
{
function redirect()
{
window.location.href="thanks.html";
}
}
else
{
$('#error_message').append('<ul></ul>');

jQuery.each(data.errors,function(key,val)
{
$('#error_message ul').append('<li>'+key+':'+val+'</li>');
});
$('#success_message').hide();
$('#error_message').show();

//reverse the response on the button
$('button[type="button"]', $form).each(function()
{
$btn = $(this);
label = $btn.prop('orig_label');
if(label)
{
$btn.prop('type','submit' );
$btn.text(label);
$btn.prop('orig_label','');
}
});

}//else
}

$('#reused_form').submit(function(e)
{
e.preventDefault();

$form = $(this);
//show some response on the button
$('button[type="submit"]', $form).each(function()
{
$btn = $(this);
$btn.prop('type','button' );
$btn.prop('orig_label',$btn.text());
$btn.text('Sending ...');
});


$.ajax({
type: "POST",
url: 'handler.php',
data: $form.serialize(),
success: after_form_submitted,
dataType: 'json'
});

});
});









share|improve this question
















I'm trying to redirect the form on https://sodacitysolar.com/consultation.html to https://sodacitysolar.com/thanks.html once the form is validated. I've traced the code and believe the solution lies somewhere below, which is the form.js file:



$(function()
{
function after_form_submitted(data)
{
if(data.result == 'success')
{
$('form#reused_form').hide();
$('#success_message').show();
$('#error_message').hide();
}
else
{
$('#error_message').append('<ul></ul>');

jQuery.each(data.errors,function(key,val)
{
$('#error_message ul').append('<li>'+key+':'+val+'</li>');
});
$('#success_message').hide();
$('#error_message').show();

//reverse the response on the button
$('button[type="button"]', $form).each(function()
{
$btn = $(this);
label = $btn.prop('orig_label');
if(label)
{
$btn.prop('type','submit' );
$btn.text(label);
$btn.prop('orig_label','');
}
});

}//else
}

$('#reused_form').submit(function(e)
{
e.preventDefault();

$form = $(this);
//show some response on the button
$('button[type="submit"]', $form).each(function()
{
$btn = $(this);
$btn.prop('type','button' );
$btn.prop('orig_label',$btn.text());
$btn.text('Sending ...');
});


$.ajax({
type: "POST",
url: 'handler.php',
data: $form.serialize(),
success: after_form_submitted,
dataType: 'json'
});

});
});


I've tried to replace this code with the code below but it's not working:



$('form#reused_form').hide();
$('#success_message').show();
$('#error_message').hide();



function redirect()
{
window.location.href="thanks.html";
}


Any chance you guys could point me in the right direction?



EDIT:



Here's the code I was trying to use:



$(function()
{
function after_form_submitted(data)
{
if(data.result == 'success')
{
function redirect()
{
window.location.href="thanks.html";
}
}
else
{
$('#error_message').append('<ul></ul>');

jQuery.each(data.errors,function(key,val)
{
$('#error_message ul').append('<li>'+key+':'+val+'</li>');
});
$('#success_message').hide();
$('#error_message').show();

//reverse the response on the button
$('button[type="button"]', $form).each(function()
{
$btn = $(this);
label = $btn.prop('orig_label');
if(label)
{
$btn.prop('type','submit' );
$btn.text(label);
$btn.prop('orig_label','');
}
});

}//else
}

$('#reused_form').submit(function(e)
{
e.preventDefault();

$form = $(this);
//show some response on the button
$('button[type="submit"]', $form).each(function()
{
$btn = $(this);
$btn.prop('type','button' );
$btn.prop('orig_label',$btn.text());
$btn.text('Sending ...');
});


$.ajax({
type: "POST",
url: 'handler.php',
data: $form.serialize(),
success: after_form_submitted,
dataType: 'json'
});

});
});






javascript forms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 0:04







BillyW

















asked Jan 1 at 19:16









BillyWBillyW

73110




73110













  • Where do you actually call the redirect function?

    – Bibberty
    Jan 1 at 19:32











  • Pseudo code if(myValudation() === true) redirect();

    – Bibberty
    Jan 1 at 19:34











  • @Bibberty just added the code I tried to use and where I called the redirect function.

    – BillyW
    Jan 2 at 0:04











  • ok, you have wrapped the redirect in a function inside success. This prevents it being called. get rid of the function declaration and just leave window.location.href="thanks.html"; This should solve the problem (if it is successful. Have you looked in chrome debug?)

    – Bibberty
    Jan 2 at 0:08











  • @samuel has corrected this for you below.

    – Bibberty
    Jan 2 at 0:09



















  • Where do you actually call the redirect function?

    – Bibberty
    Jan 1 at 19:32











  • Pseudo code if(myValudation() === true) redirect();

    – Bibberty
    Jan 1 at 19:34











  • @Bibberty just added the code I tried to use and where I called the redirect function.

    – BillyW
    Jan 2 at 0:04











  • ok, you have wrapped the redirect in a function inside success. This prevents it being called. get rid of the function declaration and just leave window.location.href="thanks.html"; This should solve the problem (if it is successful. Have you looked in chrome debug?)

    – Bibberty
    Jan 2 at 0:08











  • @samuel has corrected this for you below.

    – Bibberty
    Jan 2 at 0:09

















Where do you actually call the redirect function?

– Bibberty
Jan 1 at 19:32





Where do you actually call the redirect function?

– Bibberty
Jan 1 at 19:32













Pseudo code if(myValudation() === true) redirect();

– Bibberty
Jan 1 at 19:34





Pseudo code if(myValudation() === true) redirect();

– Bibberty
Jan 1 at 19:34













@Bibberty just added the code I tried to use and where I called the redirect function.

– BillyW
Jan 2 at 0:04





@Bibberty just added the code I tried to use and where I called the redirect function.

– BillyW
Jan 2 at 0:04













ok, you have wrapped the redirect in a function inside success. This prevents it being called. get rid of the function declaration and just leave window.location.href="thanks.html"; This should solve the problem (if it is successful. Have you looked in chrome debug?)

– Bibberty
Jan 2 at 0:08





ok, you have wrapped the redirect in a function inside success. This prevents it being called. get rid of the function declaration and just leave window.location.href="thanks.html"; This should solve the problem (if it is successful. Have you looked in chrome debug?)

– Bibberty
Jan 2 at 0:08













@samuel has corrected this for you below.

– Bibberty
Jan 2 at 0:09





@samuel has corrected this for you below.

– Bibberty
Jan 2 at 0:09












1 Answer
1






active

oldest

votes


















0














You created a function block, that will do nothing unless you actually call that specific function.



For example:



function foo() {
console.log('bar');
}


Here, I have a simple function which would print bar in my Console. However, this function above will do nothing unless I first call it:



foo() // bar


After calling it in my code I will then see bar in my console.





You can brush up your function knowledge here:




Generally speaking, a function is a "subprogram" that can be called by code external (or internal in the case of recursion) to the function. Like the program itself, a function is composed of a sequence of statements called the function body. Values can be passed to a function, and the function will return a value.






Solution



But to make your code work, simply add:



//...
redirect(); // calls the redirect function
// ...


Completed code



This is what this should look like:






$(function () {
function after_form_submitted(data) {
if (data.result == 'success') {
$('form#reused_form').hide();
$('#success_message').show();
$('#error_message').hide();

// call the function
redirect();

} else {
$('#error_message').append('<ul></ul>');

jQuery.each(data.errors, function (key, val) {
$('#error_message ul').append('<li>' + key + ':' + val + '</li>');
});
$('#success_message').hide();
$('#error_message').show();

//reverse the response on the button
$('button[type="button"]', $form).each(function () {
$btn = $(this);
label = $btn.prop('orig_label');
if (label) {
$btn.prop('type', 'submit');
$btn.text(label);
$btn.prop('orig_label', '');
}
});

} //else
}

$('#reused_form').submit(function (e) {
e.preventDefault();

$form = $(this);
//show some response on the button
$('button[type="submit"]', $form).each(function () {
$btn = $(this);
$btn.prop('type', 'button');
$btn.prop('orig_label', $btn.text());
$btn.text('Sending ...');
});


$.ajax({
type: "POST",
url: 'handler.php',
data: $form.serialize(),
success: after_form_submitted,
dataType: 'json'
});
});

});

function redirect() {

window.location.href = 'thanks.html';

}








share|improve this answer


























  • Thank you for the informatoin Samuel! But when I try to run your code the page just refreshes. It doesn't go to the thanks page.

    – BillyW
    Jan 2 at 0:05













  • @BillyW I am not sure why that would happen... Not really supposed to do that. I have included the rest of the code within the $(function() {/*...*/}); see if that works.

    – Samuel
    Jan 2 at 3:50













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998249%2fredirect-form-after-validated%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You created a function block, that will do nothing unless you actually call that specific function.



For example:



function foo() {
console.log('bar');
}


Here, I have a simple function which would print bar in my Console. However, this function above will do nothing unless I first call it:



foo() // bar


After calling it in my code I will then see bar in my console.





You can brush up your function knowledge here:




Generally speaking, a function is a "subprogram" that can be called by code external (or internal in the case of recursion) to the function. Like the program itself, a function is composed of a sequence of statements called the function body. Values can be passed to a function, and the function will return a value.






Solution



But to make your code work, simply add:



//...
redirect(); // calls the redirect function
// ...


Completed code



This is what this should look like:






$(function () {
function after_form_submitted(data) {
if (data.result == 'success') {
$('form#reused_form').hide();
$('#success_message').show();
$('#error_message').hide();

// call the function
redirect();

} else {
$('#error_message').append('<ul></ul>');

jQuery.each(data.errors, function (key, val) {
$('#error_message ul').append('<li>' + key + ':' + val + '</li>');
});
$('#success_message').hide();
$('#error_message').show();

//reverse the response on the button
$('button[type="button"]', $form).each(function () {
$btn = $(this);
label = $btn.prop('orig_label');
if (label) {
$btn.prop('type', 'submit');
$btn.text(label);
$btn.prop('orig_label', '');
}
});

} //else
}

$('#reused_form').submit(function (e) {
e.preventDefault();

$form = $(this);
//show some response on the button
$('button[type="submit"]', $form).each(function () {
$btn = $(this);
$btn.prop('type', 'button');
$btn.prop('orig_label', $btn.text());
$btn.text('Sending ...');
});


$.ajax({
type: "POST",
url: 'handler.php',
data: $form.serialize(),
success: after_form_submitted,
dataType: 'json'
});
});

});

function redirect() {

window.location.href = 'thanks.html';

}








share|improve this answer


























  • Thank you for the informatoin Samuel! But when I try to run your code the page just refreshes. It doesn't go to the thanks page.

    – BillyW
    Jan 2 at 0:05













  • @BillyW I am not sure why that would happen... Not really supposed to do that. I have included the rest of the code within the $(function() {/*...*/}); see if that works.

    – Samuel
    Jan 2 at 3:50


















0














You created a function block, that will do nothing unless you actually call that specific function.



For example:



function foo() {
console.log('bar');
}


Here, I have a simple function which would print bar in my Console. However, this function above will do nothing unless I first call it:



foo() // bar


After calling it in my code I will then see bar in my console.





You can brush up your function knowledge here:




Generally speaking, a function is a "subprogram" that can be called by code external (or internal in the case of recursion) to the function. Like the program itself, a function is composed of a sequence of statements called the function body. Values can be passed to a function, and the function will return a value.






Solution



But to make your code work, simply add:



//...
redirect(); // calls the redirect function
// ...


Completed code



This is what this should look like:






$(function () {
function after_form_submitted(data) {
if (data.result == 'success') {
$('form#reused_form').hide();
$('#success_message').show();
$('#error_message').hide();

// call the function
redirect();

} else {
$('#error_message').append('<ul></ul>');

jQuery.each(data.errors, function (key, val) {
$('#error_message ul').append('<li>' + key + ':' + val + '</li>');
});
$('#success_message').hide();
$('#error_message').show();

//reverse the response on the button
$('button[type="button"]', $form).each(function () {
$btn = $(this);
label = $btn.prop('orig_label');
if (label) {
$btn.prop('type', 'submit');
$btn.text(label);
$btn.prop('orig_label', '');
}
});

} //else
}

$('#reused_form').submit(function (e) {
e.preventDefault();

$form = $(this);
//show some response on the button
$('button[type="submit"]', $form).each(function () {
$btn = $(this);
$btn.prop('type', 'button');
$btn.prop('orig_label', $btn.text());
$btn.text('Sending ...');
});


$.ajax({
type: "POST",
url: 'handler.php',
data: $form.serialize(),
success: after_form_submitted,
dataType: 'json'
});
});

});

function redirect() {

window.location.href = 'thanks.html';

}








share|improve this answer


























  • Thank you for the informatoin Samuel! But when I try to run your code the page just refreshes. It doesn't go to the thanks page.

    – BillyW
    Jan 2 at 0:05













  • @BillyW I am not sure why that would happen... Not really supposed to do that. I have included the rest of the code within the $(function() {/*...*/}); see if that works.

    – Samuel
    Jan 2 at 3:50
















0












0








0







You created a function block, that will do nothing unless you actually call that specific function.



For example:



function foo() {
console.log('bar');
}


Here, I have a simple function which would print bar in my Console. However, this function above will do nothing unless I first call it:



foo() // bar


After calling it in my code I will then see bar in my console.





You can brush up your function knowledge here:




Generally speaking, a function is a "subprogram" that can be called by code external (or internal in the case of recursion) to the function. Like the program itself, a function is composed of a sequence of statements called the function body. Values can be passed to a function, and the function will return a value.






Solution



But to make your code work, simply add:



//...
redirect(); // calls the redirect function
// ...


Completed code



This is what this should look like:






$(function () {
function after_form_submitted(data) {
if (data.result == 'success') {
$('form#reused_form').hide();
$('#success_message').show();
$('#error_message').hide();

// call the function
redirect();

} else {
$('#error_message').append('<ul></ul>');

jQuery.each(data.errors, function (key, val) {
$('#error_message ul').append('<li>' + key + ':' + val + '</li>');
});
$('#success_message').hide();
$('#error_message').show();

//reverse the response on the button
$('button[type="button"]', $form).each(function () {
$btn = $(this);
label = $btn.prop('orig_label');
if (label) {
$btn.prop('type', 'submit');
$btn.text(label);
$btn.prop('orig_label', '');
}
});

} //else
}

$('#reused_form').submit(function (e) {
e.preventDefault();

$form = $(this);
//show some response on the button
$('button[type="submit"]', $form).each(function () {
$btn = $(this);
$btn.prop('type', 'button');
$btn.prop('orig_label', $btn.text());
$btn.text('Sending ...');
});


$.ajax({
type: "POST",
url: 'handler.php',
data: $form.serialize(),
success: after_form_submitted,
dataType: 'json'
});
});

});

function redirect() {

window.location.href = 'thanks.html';

}








share|improve this answer















You created a function block, that will do nothing unless you actually call that specific function.



For example:



function foo() {
console.log('bar');
}


Here, I have a simple function which would print bar in my Console. However, this function above will do nothing unless I first call it:



foo() // bar


After calling it in my code I will then see bar in my console.





You can brush up your function knowledge here:




Generally speaking, a function is a "subprogram" that can be called by code external (or internal in the case of recursion) to the function. Like the program itself, a function is composed of a sequence of statements called the function body. Values can be passed to a function, and the function will return a value.






Solution



But to make your code work, simply add:



//...
redirect(); // calls the redirect function
// ...


Completed code



This is what this should look like:






$(function () {
function after_form_submitted(data) {
if (data.result == 'success') {
$('form#reused_form').hide();
$('#success_message').show();
$('#error_message').hide();

// call the function
redirect();

} else {
$('#error_message').append('<ul></ul>');

jQuery.each(data.errors, function (key, val) {
$('#error_message ul').append('<li>' + key + ':' + val + '</li>');
});
$('#success_message').hide();
$('#error_message').show();

//reverse the response on the button
$('button[type="button"]', $form).each(function () {
$btn = $(this);
label = $btn.prop('orig_label');
if (label) {
$btn.prop('type', 'submit');
$btn.text(label);
$btn.prop('orig_label', '');
}
});

} //else
}

$('#reused_form').submit(function (e) {
e.preventDefault();

$form = $(this);
//show some response on the button
$('button[type="submit"]', $form).each(function () {
$btn = $(this);
$btn.prop('type', 'button');
$btn.prop('orig_label', $btn.text());
$btn.text('Sending ...');
});


$.ajax({
type: "POST",
url: 'handler.php',
data: $form.serialize(),
success: after_form_submitted,
dataType: 'json'
});
});

});

function redirect() {

window.location.href = 'thanks.html';

}








$(function () {
function after_form_submitted(data) {
if (data.result == 'success') {
$('form#reused_form').hide();
$('#success_message').show();
$('#error_message').hide();

// call the function
redirect();

} else {
$('#error_message').append('<ul></ul>');

jQuery.each(data.errors, function (key, val) {
$('#error_message ul').append('<li>' + key + ':' + val + '</li>');
});
$('#success_message').hide();
$('#error_message').show();

//reverse the response on the button
$('button[type="button"]', $form).each(function () {
$btn = $(this);
label = $btn.prop('orig_label');
if (label) {
$btn.prop('type', 'submit');
$btn.text(label);
$btn.prop('orig_label', '');
}
});

} //else
}

$('#reused_form').submit(function (e) {
e.preventDefault();

$form = $(this);
//show some response on the button
$('button[type="submit"]', $form).each(function () {
$btn = $(this);
$btn.prop('type', 'button');
$btn.prop('orig_label', $btn.text());
$btn.text('Sending ...');
});


$.ajax({
type: "POST",
url: 'handler.php',
data: $form.serialize(),
success: after_form_submitted,
dataType: 'json'
});
});

});

function redirect() {

window.location.href = 'thanks.html';

}





$(function () {
function after_form_submitted(data) {
if (data.result == 'success') {
$('form#reused_form').hide();
$('#success_message').show();
$('#error_message').hide();

// call the function
redirect();

} else {
$('#error_message').append('<ul></ul>');

jQuery.each(data.errors, function (key, val) {
$('#error_message ul').append('<li>' + key + ':' + val + '</li>');
});
$('#success_message').hide();
$('#error_message').show();

//reverse the response on the button
$('button[type="button"]', $form).each(function () {
$btn = $(this);
label = $btn.prop('orig_label');
if (label) {
$btn.prop('type', 'submit');
$btn.text(label);
$btn.prop('orig_label', '');
}
});

} //else
}

$('#reused_form').submit(function (e) {
e.preventDefault();

$form = $(this);
//show some response on the button
$('button[type="submit"]', $form).each(function () {
$btn = $(this);
$btn.prop('type', 'button');
$btn.prop('orig_label', $btn.text());
$btn.text('Sending ...');
});


$.ajax({
type: "POST",
url: 'handler.php',
data: $form.serialize(),
success: after_form_submitted,
dataType: 'json'
});
});

});

function redirect() {

window.location.href = 'thanks.html';

}






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 3:49

























answered Jan 1 at 19:34









SamuelSamuel

2,07631224




2,07631224













  • Thank you for the informatoin Samuel! But when I try to run your code the page just refreshes. It doesn't go to the thanks page.

    – BillyW
    Jan 2 at 0:05













  • @BillyW I am not sure why that would happen... Not really supposed to do that. I have included the rest of the code within the $(function() {/*...*/}); see if that works.

    – Samuel
    Jan 2 at 3:50





















  • Thank you for the informatoin Samuel! But when I try to run your code the page just refreshes. It doesn't go to the thanks page.

    – BillyW
    Jan 2 at 0:05













  • @BillyW I am not sure why that would happen... Not really supposed to do that. I have included the rest of the code within the $(function() {/*...*/}); see if that works.

    – Samuel
    Jan 2 at 3:50



















Thank you for the informatoin Samuel! But when I try to run your code the page just refreshes. It doesn't go to the thanks page.

– BillyW
Jan 2 at 0:05







Thank you for the informatoin Samuel! But when I try to run your code the page just refreshes. It doesn't go to the thanks page.

– BillyW
Jan 2 at 0:05















@BillyW I am not sure why that would happen... Not really supposed to do that. I have included the rest of the code within the $(function() {/*...*/}); see if that works.

– Samuel
Jan 2 at 3:50







@BillyW I am not sure why that would happen... Not really supposed to do that. I have included the rest of the code within the $(function() {/*...*/}); see if that works.

– Samuel
Jan 2 at 3:50






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998249%2fredirect-form-after-validated%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas