I'm using PHP to submit a very simple data to db. On my page.php, I have a link, by clicking it I insert its id and some other data in the database. For example:
echo "<a href='".$base_url."?submit=$page_id'> Submit it</a>";
It generates the url like page/test-page/?submit=12
On the top of the page I get the page id with $_GET and insert it to the db in the following way:
if (isset($_GET["submit"])) {
$page_id = $_GET['submit'] //yes, its not secure.
//insert in db.
//get db success/error msg in $db_msg
}
How can I submit the data and get the database success or fail message without refreshing the page.
check out the JQuery Documentation about Ajax Functions: http://api.jquery.com/jQuery.post/
you'd do something like
$.ajax({
type: 'POST',
url: page.php,
data: submit=$page_id,
success: function(){print("success");}
});
btw: it is better to use post than get, it just looks cleaner. (at least in my opinion)
<a href="#" onclick="$.post(
'<?php echo $base_url ?>',
{ submit= '<?php echo $page_id ?>'},
callback_function(data)
)">Submit it</a>
Send a post request to php, and deal with the message ( "data" ) with javascript?
Youll need to use a ajax method as per CyrillC's answer. here is the official docs ajax jquery
(there is a shortcode for using the POST method)
$.post.({ etc
the post method hides your data from the browser (get puts it in the query string) so its cleaner but also better for security