posting data to a database problem (newbie)
Hi, I'm trying to send data to a database. The data will be generated in a app, which include jquery. Jquery will be used to send the data. I thought it should work like the way I have it now but it doesn't.
I have a form and a js in the (html)app. And there is a php file on the server, which adds the data to the database. At least thats what I'm trying to do.
When I hit the php-code in my browser, the database will be updated. When I fill in the form for the app, at this moment for testing in my browser. File is on the same domain as the php-file and the database. So I don't have to worry about whitelists.
My code:
Form
- <div id="form-1" data-form-id="1">
<form>
<label for="comment">
<b>Comment</b>
<textarea id="comment" name="comment" cols="30" rows="10"></textarea>
</label>
<input type="submit" value="Verzenden">
</form>
</div>
js
- $('form').submit(function(){
var formID = $(this).parent().attr('data-form-id');
var postData = $(this).serialize();
$.ajax({
type: 'POST',
data: postData+'&lid='+formID,
url: 'http://mydomain.nl/smo-app/save.php',
success: function(data){
console.log(data);
alert('Your comment was successfully added');
},
error: function(){
console.log(data);
alert('There was an error adding your comment');
}
});
return false;
});
sql query
- CREATE TABLE `comments` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`comment` text,
PRIMARY KEY (`id`)
)
php-file
- $server = "";
$username = "";
$password = "";
$database = "";
$con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());
mysql_select_db($database, $con);
$locationID = $_POST["lid"];
$email = mysql_real_escape_string($_POST["email"]);
$comment = mysql_real_escape_string($_POST["comment"]);
$sql = "INSERT INTO comments (comment) ";
$sql .= "VALUES ('$comment')";
if (!mysql_query($sql, $con)) {
die('Error: ' . mysql_error());
} else {
echo "Comment added";
}
mysql_close($con);
Does anybody have a tip for me?