How to trigger PHP logic on submit with an a-tag [PHP]

46 Views Asked by At

How to form submit with an a-tag i an way that the PHP $_POST logic is going to be triggered.


Form-Structure

<form id="idMonthList" method="post">
  <div class="btn-toolbar" role="toolbar" aria-label="Toolbar with button groups">
    <div class="btn-group mr-2" role="group" aria-label="First group">
        <a class="btn btn-outline-dark" name="Januar" data-month="Januar" onclick="document.getElementById("idMonthList").submit()" href="google.com/?month=1">Januar</a>
        <a class="btn btn-outline-dark" name="Februar" data-month="Februar" onclick="document.getElementById("idMonthList").submit()" href="google.com/?month=2" >Februar</a>
    </div>
  </div>
</form>

What i tried

  • Submitting it via onclick="document.getElementById("idMonthList").submit()" event

  • Submitting via jQuery

    $(".btn-outline-dark").click(function(oEvent) {
      const sMonth = oEvent.currentTarget.attributes["data-month"].value;
      $.ajax({
          type: "POST",
          url: "index.php",
          data: {
              name: "Januar"
          },
          success: function (response) {},
          error: function (error) {}
      });
    })
    

Issue

The following PHP-Code is not going to be executed:

if (isset($_POST['Januar'])) {
    echo "Hallo Welt";
}

The only time is executed is when the form does have the following button/s (with jQuery-operation disabled):

<button type="submit" class="btn btn-outline-dark" name="Januar" data-month="Januar">Januar</button>

Context

I do have an Calendar application. The calendar is generated via PHP, i want by clicking the month to update the in calendar showcased month. When submitting (with buttons), the page auto-refreshes, and the isset($_POST), is automatically executed, i wanted via the addition of the extra URL parameters to prevent this execution, as well as highlight the appropriate button

0

There are 0 best solutions below