nik
nik

Reputation: 11

dynamically submit form based on dropdown value

I want to submit form(or setting value for action) with following details

  1. if 'cash' is selected then go to a.php
  2. else go to b.php clicking on submit button.
<tr>  
    <td>
        <label for="payment">Payment Mode:</label>
    </td>  
    <td>
        <select id="payment" name="payment" required="required">  
            <option value="">Select one...</option>  
            <option value="Cash">Cash</option>
            <option value="Cheque">Cheque</option>  `enter code here`
            <option value="Draft">Draft</option>                                                                   
        </select>
    </td>
</tr>

Upvotes: 0

Views: 432

Answers (2)

Dipiks
Dipiks

Reputation: 3928

Here is a code snippet doing what you want:

$("#payment").on("change", function() {
    if($(":selected", this).val() === "Cash") {
        $("#YourFormId").attr("action", "a.php");
    };
    else {
        $("#YourFormId").attr("action", "b.php");
    }
    $("#YourFormId").submit();
});

That will attach a change event handler to your select, check the value of the selected option and setting the correct action of your form according to your conditions

But you need to change the form selector for targetting the form you want to submit.

And a working jsfiddle

Upvotes: 0

Mr. K. O. Rolling
Mr. K. O. Rolling

Reputation: 278

I think best practice would be to always go to the same php file on submit, and there check if($_POST["payment"] == "cash").

Upvotes: 0

Related Questions