I am trying to add information to a MYSQL table using ajax. If I go direct to the insert page it works fine, but as soon as I pass the variables through the ajax code, the results entered in to the database table all say undefined instead of giving their actual value.
HTML CODE
<form action="social.php" method="POST" class="submit">
<div id="facebook">
<div class="row">
<input name="facebook" type="text" id="facebook" placeholder="Add Your Facebook Link" size="50" value="<?php echo "$facebook"; ?>">
</div>
</div>
<div id="twitter"><input name="twitter" id="twitter" type="text" placeholder="Add Your Twitter Link" size="50" value="<?php echo "$twitter"; ?>"></div>
<div id="googleplus"><input name="googleplus" id="googleplus" type="text" placeholder="Add Your Google + Link" size="50" value="<?php echo "$googleplus"; ?>"></div>
<div id="linkedin"><input name="linkedin" type="text" id="linkedin" placeholder="Add Your Linkedin Link" size="50" value="<?php echo "$linkedin"; ?>"></div>
<div id="socialsubmitbutton">
<input type="hidden" name="usern" id="usern" value="<?php echo "$usern"; ?>" />
<input type="submit" name="button" id="button" value="Submit">
</div>
</form>
Javascript / Ajax
<script type"text/javascript">
$(document).ready(function(){
$('form.submit').submit(function () {
var usern = $(this).find('.usern').attr('value');
var facebook = $(this).find('.facebook').attr('value');
var googleplus = $(this).find('.googleplus').attr('value');
var linkedin = $(this).find('.linkedin').attr('value');
var twitter = $(this).find('.twitter').attr('value');
// ...
$.ajax({
type: "POST",
url: "social.php",
data: "usern="+ usern +"& facebook="+ facebook +"& googleplus="+ googleplus +"& linkedin="+ linkedin +"& twitter="+ twitter,
success: function(){
$('form.submit').hide(function(){$('div.success').fadeOut();});
}
});
return false;
});
});
</script>
PHP
include 'db.php';
$usern = htmlspecialchars(trim($_POST['usern']));
$facebook = htmlspecialchars(trim($_POST['facebook']));
$twitter = htmlspecialchars(trim($_POST['twitter']));
$linkedin = htmlspecialchars(trim($_POST['linkedin']));
$googleplus = htmlspecialchars(trim($_POST['googleplus']));
$addClient = "INSERT INTO settings (username, facebook, googleplus, linkedin, twitter) VALUES ('$usern', '$facebook', '$googleplus', '$linkedin', '$twitter')";
mysql_query($addClient) or die(mysql_error());