php - Confirm Form Resubmission while using JavaScript form submit

989

When I'm trying to submit the form after the client change an option it's shows "Confirm Form Resubmission"

<?php
    if (isset($_POST['fix'])) {
        header('Location: ' . $_SERVER['HTTP_HOST']);
        exit();
    }
?>

<!DOCTYPE html>
<html>
    <head>
        <!-- unnecessary content -->
    </head>
    <body>
        <form method="POST">
            <fieldset>
                <legend>Confirm Form Resubmission</legend>
                <select name="choose" onchange="this.form.submit()">
                    <option value="1">1st</option>
                    <option value="2">2nd</option>
                    <option value="3">3rd</option>
                </select>
                <button name="fix">Fix</button>
            </fieldset>
        </form>
    </body>
</html>

I test the redirect code in comment when form submitted by button and it's works!

But in this case I want to able submit the form by changing option...

864

Answer

Solution:

After making a change on your combobox you are submitting the form through the commandonchange="this.form.submit()".

Your problem is that when the page receives this parameter (since you didn't specify an action attribute to your form it will submit to the same page) you check if$_POST['fix'] is set which IS NOT because you have a tag button "fix" in yourform which is an invalid input for aform tag. You have to change your if condition to something you are actually sending through yourform:

if (isset($_POST['choose'])) {

Here you will have another problem because you are redirecting you page to your same serverheader('Location: ' . $_SERVER['HTTP_HOST']); This variable$_SERVER['HTTP_HOST'] will probably yields tolocalhost so say that the name of your page ismypage.php then you call it on your browser:http://localhost/mypage.php when you change the combobox it will submit your form, enter in the if and redirect you tohttp://localhost/localhost which will likely returns aError 404 that means Page Not Found.

So you have to also fix this command to actually send your result to another page or just do something else like echoing the value of what you selected like:

<?php
    if (isset($_POST['choose'])) {
        echo "The value I choose was: " . $_POST['choose'];
        exit(); //only add this if want to stop here and 
                //not show your form again since it is the same page 
    }
?>

People are also looking for solutions to the problem: php - Backbone.js: Save's success function simply does not work

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.