I have some jQuery that when you click the save button it triggers a function to grab the HTML matching a selector and post the HTML to save_report.php:
function saveReport() {
$.post('save_report.php', function(data) {
$('.report').html(data);
});
}
$('.save').click(function () {
saveReport();
});
In save_report.php I want to know how i can then save that string to my db.
$report = $_POST['']; # <-- not sure how to post
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
mysql_query("INSERT INTO reports (id, report) VALUES('', $report) ")
or die(mysql_error());
How do I retrieve the POST value in my php file?
Thanks
Couple of things wrong here… The posted code doesn’t actually post any data, and the post and html functions are called incorrectly.
So, first I’ll grab the html from the .report selector, and store it in a variable. Then I’ll post it providing a variable name of ‘report’. I added a simple callback that alerts what the web server sends back, which you can remove or change.
In your PHP, you would be looking for $_POST[‘report’] which is how I named the data being posted.
You’re not sanitizing any of the input, so basically any random hacker could take over your entire database with SQL injection. At a minimum, after getting $_POST[‘report’], run it through the mysql_real_escape_string() function.