简体   繁体   中英

PHP submit refreshes the page and takes the submit's id as a parameter

This is my problem - when I hit the submit button it is supposed to execute some code. Unfortunately all it does is refresh the page at a weird url using the submit's ID and value. File is called Q2C.php. This is the code:

<?php
if(isset($_POST['submit'])) {
    $arraylength = count($selectedQuotes);
    for($i = 0; $i < $arraylength; $i = $i + 1) {
        $supplierQuoteID = $selectedQuotes[$i];
        $updateSQL = "UPDATE supplierquotes SET `Selected` = 1 WHERE `SupplierQuoteID` = '$supplierQuoteID'";
        $updateQuery = mysql_query($updateSQL);

    }
} 
?>

<div class="buttons">
    <input type="submit" name="submit" id="submit" value="Create Q2C" />
    <input type="submit" name="cancel" id="cancel" value="Cancel" />
</div>

It redirects me to this url: (path)/Q2C.php?submit=Create+Q2C

How can I stop it from redirecting me here, and instead just perform what is in the if isset statement?

You need to put your buttons into a form.

<form method="post">
  <input type="submit" name="submit" id="submit" value="Create Q2C" />
  <input type="submit" name="cancel" id="cancel" value="Cancel" />
</form>

You should consider changing your second submit button to a cancel button.

我在您的HTML中看不到<form> ,但是您需要使用POST而不是GET

<form method=post>

You have not submitted your form completely in the above code, but it is clear from the redirected url that you are using GET as post method of the form. And in your isset check you are using $_POST. Change the form method to post then it will work fine.

You need to specify method. By default its get

<div class="buttons">
    <form action="Q2C.php" method="post">
        <input type="submit" name="submit" id="submit" value="Create Q2C" />
        <input type="submit" name="cancel" id="cancel" value="Cancel" />    
    </form>
</div>

You should probably use ajax to submit a form if you don't want to refresh the page. Here is the same code you can refer to:

$.ajax({
                type: "GET",
               url: "abc.php",
               data: "name="+form.name.value,
                success: function(rs) 
                { 
                            // do success stuffs here
                          }
                           });

Hello am not sure if i get your problem correct its like You want Submit a form to Q2C.php with out a form attribute called "Method" if u dont set the method at all, the form will use the GET method as default..... but from your code it seems the form is using get to submit to Q2C.php.... and you are saying (isset($_POST['submit'])) which is wrong because the Submit button was never set.. what u need to do is change set the attribute method of the form to "post" method and try running the code again... your form should look like this...

.........

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM