0

I have select tag and I want if I choose one of the option, i want to go to different page my code doesnt work. Can somebody help me

<div id="paymentmethod" style="margin-bottom:50px;">
  <h4 style="font-weight: bold; color: #0C7D53;">Payment Method </h4>
  <form action="" method="post">
    <label style="padding-top:8px;">Choose your paymet method:</label>
    <select id="payment" class="form-control">
      <option value="cash">Cash</option>
      <option value="paypal">PayPal</option>
    </select>
    <p><br/></p>
    <center><input style="width:25%;" type="submit" name="submit" value="Checkout" class="btn btn-primary btn-lg"> </center>
  </form>
  <p><br/></p>


    <?php
    if(isset($_POST["paymentmethod"])){
        $payment = $_POST["payment"];

        if ($payment == "cash") {

            header("Location:http://localhost/Ecommerce-app-h/congrats.php");
            exit();

        } else {

            header("location:cart.php");

        }
    }
    ?>

</div>
Barmar
  • 741,623
  • 53
  • 500
  • 612

2 Answers2

1

Is this isset($_POST["paymentmethod"]) the problem?

You need to add name="payment" to <select> tag and use $_POST["submit"] to check if the submit button has been clicked or not.

if(isset($_POST["submit"])){ 
    $payment = $_POST["payment"];

    ....
    ....
}

Let me know if it solves your problem :)

NcXNaV
  • 1,657
  • 4
  • 14
  • 23
  • I change it based on your answer but still it doesnt work. or is it because i put my php code in that div? Im sorry i just dont know – Turtle Developer Jun 23 '21 at 15:56
  • It's better to put your php before html code. You can try to echo $_POST['payment] or use var_dump ($_POST) to check the $_POST value. Have you added name="payment" inside – NcXNaV Jun 23 '21 at 18:01
1

Use simple javascript in your PHP code like this

$(document).ready(function(){
  $("#submit_btn").click(function(){
  var payment = $("#payment").val();
  if(payment == "cash"){
  location.href="your web page";
  }
  else
  {
    location.href="your web page";
  }
  })
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="paymentmethod" style="margin-bottom:50px;">
  <h4 style="font-weight: bold; color: #0C7D53;">Payment Method </h4>
  <form id="my_form" action="" method="post">
    <label style="padding-top:8px;">Choose your paymet method:</label>
    <select id="payment" class="form-control">
      <option value="cash">Cash</option>
      <option value="paypal">PayPal</option>
    </select>
    <p><br/></p>
    <center><input style="width:25%;" id="submit_btn" type="button" name="submit" value="Checkout" class="btn btn-primary btn-lg"> </center>
  </form>
  <p><br/></p>
</div>

Other wise another method

<?php
if(isset($_POST["submit"])){
    $payment = $_POST["payment"];

    if ($payment == "cash") {

        header("Location:http://localhost/Ecommerce-app-h/congrats.php");
        exit();

    } else {

        header("location:cart.php");

    }
}
?>
    <div id="paymentmethod" style="margin-bottom:50px;">
  <h4 style="font-weight: bold; color: #0C7D53;">Payment Method </h4>
  <form action="" method="post">
    <label style="padding-top:8px;">Choose your paymet method:</label>
    <select name="payment" id="payment" class="form-control">
      <option value="cash">Cash</option>
      <option value="paypal">PayPal</option>
    </select>
    <p><br/></p>
    <center><input style="width:25%;" type="submit" name="submit" value="Checkout" class="btn btn-primary btn-lg"> </center>
  </form>
  <p><br/></p>