Skip to content
Advertisement

How to change AJAX POST URL based on radio button?

Can you help me and give me advice . How exactly can i make the AJAX to change the post url based on radio button. Do i need to use If statement or ?

var barray = [];

function cbutton() {
  $('input:radio[name="cheking"]:checked').val();
  var varies = $("#fromdate").val();
  var varies2 = $("#todate").val();
  
  if (varies == "" || varies2 == "") {
    alert("Please fill in all 2 fields first and then submit again");
  } else {
    barray.push(varies + "~" + varies2);
    $.ajax({
      type: "POST",
      url: "pickingeff.php", // <------ That what i want to change depending on radio button
      data: {
        fromdate: varies,
        todate: varies2
      }
    }).done(function(msg) {
    });
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" id="pickingi" name="cheking" class="checking" value="pickingeff.php" onchange="toggleVisibility('show1');" checked> &nbsp;
<label for="cheking">Picking</label> &nbsp;

<input type="radio" id="packingi" name="cheking" class="checking" value="packingeff.php" onchange="toggleVisibility('show2');"> &nbsp;
<label for="cheking">Packing</label> &nbsp;

<input type="radio" id="overall" name="cheking" class="checking" value="overalleff.php" onchange="toggleVisibility('show3');"> &nbsp;
<label for="cheking">Overall</label>

Advertisement

Answer

Thank you guys , simple !

var barray = [];
function cbutton() {
  $('input:radio[name="cheking"]:checked').val();
  var varies = $("#fromdate").val();
  var varies2 = $("#todate").val();
  
  if (varies == "" || varies2 == "") {
    alert("Please fill in all 2 fields first and then submit again");
  } else {
    barray.push(varies + "~" + varies2);
    $.ajax({
      type: "POST",
      url:   $('input:radio[name="cheking"]:checked').val(), // <------ That worked !
      data: {
        fromdate: varies,
        todate: varies2
      }
    }).done(function(msg) {
    });
  }
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement