How Can I Call A Function With A Href?
I´m trying to call a function into a href, my function is on functions.php and my href is on views/something.php so, this is my function: function discount($connection, $us){
Solution 1:
You'll need to change this to use an ajax call or form post to call the PHP function.
Here's a really basic example which should point you in the right direction
discount.php
<?php// Load $connection from somewhere// Get user, it's better to get this from a cookie or session rather than GET$user = $_GET['user']
$discount = $connection->prepare("UPDATE postule SET seen = 1 WHERE id = :user");
$discount->bindParam(':user', $user);
$result = $discount->execute();
// Throw error if something went wrong with the update, this will cause $.ajax to use the error functionif (!$result) {
http_response_code(500);
}
html, assuming $notu[0]
contains the user id
<?phpforeach ($total_notuas$notu) : ?><li><aonClick="return callDiscount('<?phpecho"$notu[0]"; ?>');"href="#"> Notificaciones <spanclass="badge "><?phpecho"$notu[0]"; ?></span></a></li><?phpendforeach; ?>
js, requires jquery
functioncallDiscount(user_id)
{
// Perform ajax call to discount.php
$.ajax({
url: '/discount.php?user=' + user_id,
error: function() {
alert('An error occurred');
},
success: function(data) {
// Redirect user to notificaciones.phpdocument.location = '/notificaciones.php';
}
});
// Prevent link click doing anythingreturnfalse;
}
Post a Comment for "How Can I Call A Function With A Href?"