<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(function() {
$('form').on('submit', function(event) {
event.preventDefault();
var formData = $(this).serialize();
$.ajax({
type: 'POST',
url: 'your-php-script.php',
data: formData,
success: function(response) {
if (response === 'error') {
// display error notification
alert('ห้ามใส่ลิ้งน๊า');
} else {
// display success notification or redirect to another page
alert('ส่งข้อมูลเรียบร้อย');
}
}
});
});
});
</script>
<form>
<!-- form fields go here -->
<button type="submit">Submit</button>
</form>
In this example, the jQuery library is used to handle the AJAX request. The form’s submit event is intercepted and prevented from refreshing the page. The form data is serialized and sent to a PHP script using the POST method. The success function is called when the server returns a response.
If the response is ‘error’, an error notification is displayed using the alert function. If the response is not ‘error’, a success notification is displayed or the page is redirected to another URL.
Note that you will need to replace ‘your-php-script.php’ with the actual filename of your PHP script that contains the code you want to run.