Turtle Developer
Turtle Developer

Reputation: 19

How can I go to different link with different value from select tag?

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>

Upvotes: 0

Views: 60

Answers (2)

NcXNaV
NcXNaV

Reputation: 1761

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 :)

Upvotes: 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>

Upvotes: 1

Related Questions