0

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...

  • I'm not really sure what you are asking? You mention JavaScript in your title but you show absolutely no JS in the question. Should we guess? Mind read? Or what? – M. Eriksson Jun 18 '16 at 02:48
  • Refer this link : http://stackoverflow.com/questions/418076/is-there-a-better-jquery-solution-to-this-form-submit – Mani Jun 18 '16 at 04:34

1 Answers1

0

After making a change on your combobox you are submitting the form through the command onchange="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 your form which is an invalid input for a form tag. You have to change your if condition to something you are actually sending through your form:

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

Here you will have another problem because you are redirecting you page to your same server header('Location: ' . $_SERVER['HTTP_HOST']); This variable $_SERVER['HTTP_HOST'] will probably yields to localhost so say that the name of your page is mypage.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 to http://localhost/localhost which will likely returns a Error 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 
    }
?>
Jorge Campos
  • 22,647
  • 7
  • 56
  • 87