php - Selectively post form to different pages, based on form result?

127

Say I have a simple form on a page calledphoto/delete.php. This form deletes an image specified by the user. All it is, is this:

<form action="?" method="post">
 <input type="checkbox" name="confirmDelete" value="confirm" />    
 <input type="submit" value="delete" />
</form>

So, this form contains a confirmation check box that must be ticked to ensure the image is deleted. How can I dynamically choose what page to POST this form to, based on its contents?

For example, if the checkbox is not checked, yet the submit button is clicked, I'd like to stay on the samephoto/delete.php page and display an error, since its possible they really do want to delete the image and simply forgot to tick the box.

But otherwise, if everything is successful and the checkbox is ticked, I'd like to POST it to another page, sayhome.php since it makes no sense to stay on the same page of a just-deleted image.

How can I implement this?

750

Answer

Solution:

You may try something like this

HTML:

<form action="delete.php" method="post">
    <input type="checkbox" name="confirmDelete" value="confirm" />    
    <input id="btn_delete" type="submit" value="delete" />
</form>

JS:

window.onload = function(){
    document.getElementById('btn_delete').onclick = function(e){
        var checkBox = document.getElementsByName('confirmDelete')[0];
        if(!checkBox.checked) {
            if(e && e.preventDefault) {
                e.preventDefault();
            }
            else if(window.event && window.event.returnValue) {
                window.eventReturnValue = false;
            }
            alert('Please check the checkbox!');
        }
    };
};

People are also looking for solutions to the problem: php - Second parameter in rtrim function

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.