I am still learning proper and more efficient ways of writing code - at the moment I am stuck on this current issue.
I have a form that currently works fine - writes to the sql database, then reloads the current page. I would like the form to NOT reload the page.
I know there is a way to do this with jQuery and Ajax -- honestly I'm not skilled yet with these.
If there's a way to do this with PHP/HTML - I would love guidance on this method.
If the only answer is Java and Ajax - I would love guidance for this specific instance of changing this form to not reload the page.
Screen shot of what the button looks like -- three colors to represent 3 stages of tracking payment on invoices. Red turns to yellow when clicked, yellow to green, green back to red (in case of a mistake click).
Here's front end code for my form:
<form action="jump_invoicepaid.php" method="post">
<input type="hidden" name="invoicepaid" value="'.$projectid.'">');
if ($projectpaid == 2) {
echo('<button type="submit" name="action" class="greenbutton" value="unpaid">Paid</button></form>');
} else if ($projectpaid == 1) {
echo('<button type="submit" name="action" class="yellowbutton" value="paid">Paid</button></form>');
} else {
echo('<button type="submit" name="action" class="redbutton" value="sent">Paid</button></form>');
}
Here's the back end code for my form:
if ($_POST['action'] == paid) {
///marking project as paid
include_once('toolbox.php');
$projectid = $_POST['invoicepaid'];
mysqli_query($con, "UPDATE kmis_projects SET project_paid='2' WHERE project_id= '$projectid'");
header('Location: ./?page=pastevents');
} else if ($_POST['action'] == sent) {
///marking project as sent
include_once('toolbox.php');
$projectid = $_POST['invoicepaid'];
mysqli_query($con, "UPDATE kmis_projects SET project_paid='1' WHERE project_id= '$projectid'");
header('Location: ./?page=pastevents');
} else if ($_POST['action'] == unpaid) {
///marking project as unpaid
include_once('toolbox.php');
$projectid = $_POST['invoicepaid'];
mysqli_query($con, "UPDATE kmis_projects SET project_paid='0' WHERE project_id= '$projectid'");
header('Location: ./?page=pastevents');
}
I do want to learn jQuery and ajax fully, it's next on my to learn list. I know they would make this issue a lot easier to solve.
UPDATE -- I spent some time with the link posted, suggesting it was a duplicate question of the link. I do see why... but it's not the same at the heart of what I'm asking or the answer that was given in the link. I think the link could be "an" answer to the issue - but I was interested in any ways of solving the issue without having to use jQuery and AJAX -- and if that was unavoidable, then specifically how AJAX would fit into my code and this specific situation. I equally think this is unique from the link, given that my buttons are populated with 3 different "if" checks, to write a different value depending on what button is populated and clicked.
UPDATE 2 -- Here's the current code I'm working with. The ajax isn't sending name or value of button though it seems.
<script>
$( document ).ready(function() {
$("button").click(function(e) {
e.preventDefault(); // prevent page refresh
$.ajax({
type: "POST",
url: "jump_invoicepaid.php",
data: $(this).serialize(), // serialize form data
success: function(data) {
window.location.replace("?page=pastevents");
},
error: function() {
// Error ...
}
});
});
});
</script>
<form action="">
<input type="hidden" name="invoicepaid" value="'.$projectid.'">
if ($projectpaid == 2) {
echo('<button type="submit" name="action" class="greenbutton" value="unpaid">Paid</button></form>');
} else if ($projectpaid == 1) {
echo('<button type="submit" name="action" class="yellowbutton" value="paid">Paid</button></form>');
} else {
echo('<button type="submit" name="action" class="redbutton" value="sent">Paid</button></form>');
}