How to run an sql query from a link with javascript onclick
I have a script in a joomla article that goes through the database an开发者_高级运维d lists all entries by a use. Everything is done in PHP with echo statements in it.
At the end of each entry, I added a "delete" button.
For example:
echo "[a href=\"#\" onclick=\"javascript:(NEED TO RUN QUERY HERE)\"]Delete[/a]";
How can I do this?
(please ignore the square brackets, I wasn't sure how to make the code show and the pre tag didnt work that well)
Ajax, you would make a call from javascript to your php script, return the data in JSON, parse it and append the entries to the dom
I would recommend looking at this http://www.w3schools.com/php/php_ajax_intro.asp it should lead you through the steps you requested to setup AJAX. The left side has links how you would code it in PHP and also make Database queries.
I recommend just taking the sample code and adjusting it to meet your needs. Have fun!
here is an example on how you would do it using jquery ajax.
$('#buttonid').click(function() {
$.ajax({
type: "POST",
url: "pathtoserversidescipt",
data: $("#formid").serialize(),
success: function(msg){
("whatever you want to happen next")
}
});
});
Also check here to learn more. http://api.jquery.com/category/ajax/
You need to post that link by ajax to edit section. In edit section you can get the post id and remove it like
if(isset($_GET['delete']) and $_GET['delete'] != '')
and remove
function remove() {
$id = JRequest::getVar('id');
$db =& JFactory::getDBO();
$query = "DELETE FROM #__cd_lend_request WHERE ck_CDid = '$id'";
$db->setQuery($query);
$result = $db->query();
$this->setRedirect(JRoute::_('index.php?option=com_cd'), 'CD has been removed from the list!', 'notice');
}
精彩评论