-2

this is one of what I have tried.

<script>
   
        document.getElementById("button").addEventListener("click", redirectFunction);
        
        function redirectFunction(){
            window.location.href = "paymentsuccess.php";
        }
   
    </script>

 <form action="payment3.php" method="post">
    <div class ="transaction">
        <label style="font-size:20px">Paying for:</label> &nbsp;&nbsp;&nbsp;
        <select name="Namelist">
            <?php
                while ($namelist = mysqli_fetch_array(
                        $all_name,MYSQLI_ASSOC)):
    ?>
                
                <option value="<?php echo $namelist["UserID"];
                    // The value usually set as the primary key
                ?>">
                    <?php echo $namelist["Name"];
                        // To show the name list to the user
                    ?>
                </option>
            <?php
                endwhile;
                // While loop must be terminated
            ?>
        </select>
        
        <br>
        
        <div class = "transaction1">
        <label style="font-size:20px">Enter amount to pay:</label> &nbsp;
        <input style="color: darkgray" type ="text" size = "15" maxlength="7" name="amount" id="amount" pattern="(\d{3})[\.])(\d{2})" ondrop="return false;" onpaste="return false;" 
        onkeypress="return event.charCode>=48 && event.charCode<=57" **required**>
        
        </div>
        </div>

<div>
     <input type="submit" value="Pay" name="submit" id="button" onclick="redirectFunction()"/>  
     <input type="hidden" name="submitted" value="" />
</div>
</form>

after I click the submit button after input, it will save to the database but not redirect to the next page. but after I click the submit button for the second time, only then it will redirect to the next page. required validation form also does not work.

sr7
  • 1
  • 1
  • Your form action and your button are conflicting. Both redirect to another page. You need to have your payment3.php do the redirect. – aynber Dec 15 '22 at 19:39
  • Remember the javascript runs as soon as the button is clicked. So it wouldn't give time for the form to be submitted and the php script to run. Or vice versa if the form manages to submit before the event listener runs, it won't do the redirect. You need to trigger the redirect from php, at the end of the code which does the saving. – ADyson Dec 15 '22 at 20:15

1 Answers1

0

You should remove the javascript from that page and add code like the following to the page that process the form (payment3.php), after the data is saved to the database:

header('Location: /paymentsuccess.php');

Or if the redirect fails because the headers have already been sent, you can use javascript, on the same page(payment3.php), after the data is saved in the database, using js code like this:

<script type="text/javascript">
    window.location = "/paymentsuccess.php";
</script>
Pedro
  • 728
  • 8
  • 18
  • if I click the payment3.php page from the previous page to go to payment3.php, it will only show me paymentsuccess.php. – sr7 Dec 15 '22 at 20:01