i want to redirect form after ( success ) to another page. i use this code
$(document).ready(function(){
$("#ajax-form").submit(function(){
$.post(
"ajaxContact/ajax-register.php",
$("#ajax-form").serialize(),
function(data){
if (data.success)
$("span#ajax-message").css({'color':'green'});
window.location("index.html");
else
$("span#ajax-message").css({'color':'red'});
$("span#ajax-message").html(data.message);
},
"json"
);
return false;
});
});
how to redirect.
Regards
If you want to just redirect the page there, this should work:
if(data.success)
window.location = "url";
Updated:
$(document).ready(function()
{
$("#ajax-form").submit(function()
{
$.post("ajaxContact/ajax-register.php", $("#ajax-form").serialize(),
function(data)
{
if (data.success)
{
//If successful...
window.location = 'http://www.google.com';
}
else
{
//If unsuccessful...
alert("Post was unsuccessful.");
}
},"json");
return false;
});
});
That should work for you as long as your post is returning successfully. Here's a demo using a confirm to imitate your Post:
Demo here
window.location = "http://www.google.com";
Related
Hello and thank for your time. I have a problem with my response.getWriter.write
Here is my jsp code
enter code here
<script>
$(document).ready(function() {
var form = $('#form-registrazione');
form.submit(function () {
$.ajax({
type:'post',
url: 'utente',
data: form.serialize(),
success:function (data) {
var result = data;
if(result == "usernameEsistente"){
alert("IL PENE");
$('#errore').html("NON VA BENE");
}
if(result == "vabene"){
window.location.replace("/index.jsp");
}
}
});
}
}
</script>
and in my servlet "utente" I have this code portion:
if(model.checkUsername(username)){
Utente utente = new Utente();
utente.setUser(username);
utente.setPass(password);
utente.setTipo("Cliente");
model.doSaveNewAccount(utente);
response.getWriter().write("vabene");
}
else{
response.getWriter().write("usernameEsistente");
}
the problem is, the response.getWriter().write() overwrite the whole page
http://imgur.com/a/D5M8r
I don't understand why it stays in the servlet page, and does not return in the jsp page where ajax is.
I already make login form and success, but when I'm trying add ajax, it fails, does anyone know a solution? this is my code:
$(document).ready(function()
{
$("#formLogin").submit(function(e)
{
$.ajax({
url:"<?php echo site_url('login'); ?>",
type:"POST",
data:$("#formLogin").serialize()
})
.done(function(response)
{
if(response =='gagal')
{
$(".alert.alert-danger").show('fast');
}
else
{
window.location.href = "http://localhost/epeds_web/";
}
});
e.preventDefault():
});
});
I have been stuck at this problem for a whole day. What im trying to do is to send 2 values from view to controller using Ajax.
This is my code in hot_products view:
<script>
$(function(){
$('#btnSubmit').click(function() {
var from = $('#from').val();
var to = $('#to').val();
alert(from+" "+to);
$.ajax({
url: "/orders/hot_products",
type: 'POST',
data: {"start_time": from, "end_time": to,
success: function(data){
alert("success");
}
}
});
});
});
and my hot_products controller:
public function hot_products()
{
if( $this->request->is('ajax') ) {
$this->autoRender = false;
//code to get data and process it here
}
}
I dont know how to get 2 values which are start_time and end_time.
Please help me. Thanks in advance.
PS: im using cakephp 2.3
$this->request->data gives you the post data in your controller.
public function hottest_products()
{
if( $this->request->is('ajax') ) {
$this->autoRender = false;
}
if ($this->request->isPost()) {
// get values here
echo $this->request->data['start_time'];
echo $this->request->data['end_time'];
}
}
Update
you've an error in your ajax,
$.ajax({
url: "/orders/hot_products",
type: 'POST',
data: {"start_time": from, "end_time": to },
success: function(data){
alert("success");
}
});
If you method is POST:
if($this->request->is('ajax'){
$this->request->data['start_time'];
$this->layout = 'ajax';
}
OR
public function somefunction(){
$this->request->data['start_time'];
$this->autoRender = false;
ANd if method is GET:
if($this->request->is('ajax'){
$this->request->query('start_time');
$this->layout = 'ajax';
}
OR
public function somefunction(){
$this->request->query('start_time');
$this->autoRender = false;
I want to validate my course name field if the course name inputted already exist using AJAX, but my ajax function always return the alert('Already exist') even if i inputted data that not yet in the database. Please help. Here is my code. Thanks.
View:
<script type="text/javascript">
var typingTimer;
var doneTypingInterval = 3000;
$('#course_name').keyup(function(){
typingTimer = setTimeout(check_course_name_exist, doneTypingInterval);
});
$('#course_name').keydown(function(){
clearTimeout(typingTimer);
});
function check_course_name_exist()
{
var course_name=$("#course_name").val();
var postData= {
'course_name':course_name
};
$.ajax({
type: "POST",
url: "<?php echo base_url();?>/courses/check_course_name_existence",
data: postData,
success: function(msg)
{
if(msg == 0)
{
alert('Already Exist!');
return false;
}
else
{
alert('Available');
return false;
}
return false;
}
});
$("html, body").animate({ scrollTop: 0 }, 600);
return false;
}
</script>
Controller:
function check_course_name_existence()
{
$course_name = $this->input->post('course_name');
$result = $this->course_booking_model->check_course_name_exist($course_name);
if ($result)
{
return true;
}
else
{
return false;
}
}
Model:
function check_course_name_exist($course_name)
{
$this->db->where("course_name",$course_name);
$query=$this->db->get("courses");
if($query->num_rows()>0)
{
return true;
}
else
{
return false;
}
}
You could use console.log() function from firebug. This way you will know exactly what the ajax returns. Example:
success: function(msg) {
console.log(msg);
}
This way you also know the type of the result variable.
jQuery, and Javascript generally, does not have access to the Boolean values that the PHP functions are returning. So either they return TRUE or FALSE does not make any difference for the JS part of your code.
You should try to echo something in your controller and then make your Javascript comparison based on that value.
I have the following js code:
function getSelectedPlace(form) {
var places = 1
$(form).find(":input").each(function () {
if ($(this).attr("name") === "Signed_Order_B64") {
$.ajax({
type: 'POST',
url: '/MyController/MyAction',
data: "id=" + places,
success: function (response) {
if (response.Result) {
ret = response.Message;
result = true;
$("input[type=hidden][name=email]").val(response.Email);
$("input[type=hidden][name=appendix]").val(response.Appendix);
} else {
// show fancybox
result = false;
}
},
error: function () {
result = false;
},
async: false
});
$(this).val(ret);
}
});
if (!result) {
$(submitButton).attr("value", btnText);
setStateFotBuyButton(false);
}
return result;
}
I have if statement in success block. I want show partial view in the fancybox in else block. How can I do this? Thanks
Not totally sure what you mean with "partial view" but you could do
} else {
$.fancybox(response,{
// fancybox options here
}); // fancybox
}
You could use the content switch which allows you to set the content of the fancybox to any html:
$.fancybox({
content: response
});