How can I determine which submit button was clicked?
I do not want to use <form>
, because the page refreshes. By clicking the submit button, a PHP-file gives back a response that is shown on the current page.
HTML in index.html:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
</head>
<body>
[...]
<div class="form-inline" id="form">
<div class="container">
<div class="form-group">
<div class="input-group input-group-sm">
<div class="input-group-text regwidth">
First name
</div>
<input type="text" class="form-control" name="registerFirstname" id="firstname">
</div>
<div class="input-group input-group-sm">
<div class="input-group-text regwidth">
Last name
</div>
<input type="text" class="form-control" name="registerLastname" id="lastname">
</div>
<div class="d-grid gap-2 col-8 mx-auto">
<button type="submit" class="btn btn-primary" id="sublogreg"><i class="fa-solid fa-address-card"></i> Register</button>
<button type="submit" class="btn btn-primary" id="subloginfo"><i class="fa-solid fa-circle-info"></i> Request information</button>
<p id="returnmessage"></p>
</div>
[...]
jQuery script in index.html:
<script>
$("#form").ready(function() {
$("#sublogreg,#subloginfo").click(function() {
var firstname = $("#firstname").val();
var lastname = $("#lastname").val();
var sublogreg = $("#sublogreg").val();
var subloginfo = $("#subloginfo").val();
[...]
I tried to add
value="sublogreg"
in the <button>
. The value is set, but then both submit button has their value.
I am an autodidact searching for solutions if I need something. I am not a specialist.