ARTICLE AD BOX
Update (hopefully simplified issue description): I am trying to perform a basic ajax post call using a field value, which passes to a PHP (the format of which I have used on other pages with a convention POST modal). In this modal, I need to submit the PHP separately.
This is the jquery ajax call.
<script>
$(document).ready(function() {
$('#addhostgroupdatabtn').on('click', function(e) {
// Find value for hostname, run ajax query
let valaddhostgroupname = document.getElementById('addhostgroupname').value;
alert("add hostgroup - name " + valaddhostgroupname);
$.ajax({
type: 'POST',
url: 'addsudoershostgroup.php', // Your PHP processing script
data: { addhostgroupname: valaddhostgroupname,
},
success: function(response) {
// CALL YOUR JQUERY FUNCTION HERE
alert("last id " + response);
}
});
});
});
</script>
This is the back-end code.
[root@rhel8lab1 public]# cat addsudoershostgroup.php
<?php
// WARNING! script alerts will not work on this. don't even bother.
//
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 'On');
// include config.php for connection details
include('../includes/config.php');
$connection = mysqli_connect($db_host, $db_user, $db_password);
$db = mysqli_select_db($connection, $db_database);
// No post check, sent straight from jquery
$addhostgroupname = $_POST['addhostgroupname'];
$query = "INSERT INTO host_groups (`groupname`,`expires`,`active`,`modified_by`) VALUES ('$addhostgroupname',0, 1,'admin')";
$query_run = mysqli_query($connection, $query);
// Don't use query run, use lastid
// $lastId = $connection->lastInsertId();
$lastId = mysqli_insert_id($connection);
if ( $lastId > 0 )
{
echo $lastId;
} else
{
echo 0;
}
mysqli_close($connection);
?>
The problem is that it usually fails to add an entry. I cannot see any issue with my back-end code, so I assume this is HTML5 and jquery having issues. Any pointer to existing templates that work would be much appreciated.
