PHP-Zend
PHP-Zend

Reputation: 311

Redirecting to a new page using jquery upon user confirmation

In my web application(using Zend Framework) when adding a new entity to the database, it should check whether this entity has previously been added and deleted. (When deleting an entity from the database, that particular record's "STATUS" column is updated to 0(zero).)

If the entity has deleted earlier, the system asks for confirmation to edit the previously deleted record, or add a new record. If the user needs to edit the previously added record, user should be redirected to the edit page.

This check and redirecting is done using jQuery. Here is the jQuery code I'm using:

 $("input[name=btnSubmit]").click(function(){       

    var name = $("input[name=txtTblPrefix]").val();
        var parameters = 'name='+name;

        $.ajax({
            url: '../admin_mapping/get-id',
            async:false,
            data:parameters,
            type:'POST',
            dataType: 'text',
            success: function(data)
            {       
                var res = confirm("There is a record already added to the database for "+name+". Do you want to edit it?");

                if(res)
                {
                    window.location.replace("../admin_mapping/edit?id="+data);
                }
            }

        });

    });

In this code, everything works perfectly other than the redirecting. I have tested the line window.location.replace("../admin_mapping/edit?id="+data); separately and it works fine. Even the confirm() function returns true.

I have tried putting an alert() inside the if condition, and the alert prompts. But the redirecting is not working.

Can anyone please help me with this?

Thanks in advance

Upvotes: 0

Views: 243

Answers (2)

PHP-Zend
PHP-Zend

Reputation: 311

The only thing I need to do is put a return false inside the submit function. Otherwise as the button is a submit button, the form gets submitted.

Upvotes: 0

Riccardo Zorn
Riccardo Zorn

Reputation: 5615

First make sure the variable data contains what you expect: maybe it has some headers making the call go wrong (but you should spot this in the console).

If you can alert() the correct string, then try another approach: location has some security restrictions (i.e. an iframe cannot change its container's location); you may not be able to manipulate it in an unnamed function call, but you might be able to do so from a timeout (it worked for me!) so instead of

document.location.href='./yoururl?'+data 

write

setTimeout("document.location.href='yoururl?"+data +"';",10);

this will run it in a wider scope and should succeed.

Upvotes: 1

Related Questions