Hopefully just a quick one, I can see various answers to my problem but finding it difficult to implement into my code. As the title says, looking to allow apostrophes when posting data to MySQL using ajax and javascript. When submitting data just now, it doesn't work.
Hoping someone can finalise the code to make this work.
HTML
<!-- Modal - Add New Record/Info -->
<div class="modal fade" id="add_new_record_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Add some useful information</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="add_info">Add Info</label>
<input type="text" id="info" class="form-control"/>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" onclick="addRecord()">Add Record</button>
</div>
</div>
</div>
</div>
<!-- // Modal -->
Javascript
// Add Record
function addRecord() {
// get values
var info = $("#info").val();
// Add record
$.post("ajax/addRecord.php", {
info: info
}, function (data, status) {
// close the popup
$("#add_new_record_modal").modal("hide");
// read records again
readRecords();
// clear fields from the popup
$("#info").val("");
});
}
Ajax - addRecord.php
<?php
if(isset($_POST['info']))
{
// include Database connection file
include("db_connection.php");
// get values
$info = $_POST['info'];
$query = "INSERT INTO useful_info(info) VALUES('$info')";
if (!$result = mysql_query($query)) {
exit(mysql_error());
}
echo "1 Record Added!";
}
?>
From reading other questions on the site, I can either use get_magic_quotes_gpc
or by creating a JSON object and use JSON.stringify
. Just wondering how best to achieve this.
Any help would be appreciated.