Values NOT being sent to mysql Database from Ajax
Pregunta
I am trying to get jquery's .ajax() to send values sent to a form :
<form method="post" id="FanDetail">
<label for="bio">Brief Bio<br /></label>
<textarea id="bio" name="fan_bio" cols="27" rows="3"></textarea><br />
<label for="dob">Date of Birth<br/></label>
<input id="dob" name="fan_dob" value="(e.g. 01/05/1965)" onFocus="clearText(this)" /><br />
<label for="zip">Location<br /></label>
<div class="ui-widget">
<input id="zip" name="term" value="What is your Zipcode?" onFocus="clearText(this)" /><br />
<input id="actualZip" type="hidden" name="actualzipval" value="" />
</div><!--ui-widget :: zip -->
<label for="occup">Occupation<br /></label>
<div class="ui-widget">
<input id="occup" type="text" name="term2" value="e.g. Computer Programmer, etc" onFocus="clearText(this)" /><br />
<input id="actualOccup" type="hidden" name="actualOccupval" value="" />
</div><!--ui-widget :: occup -->
<label for="fbkurl">Facebook Handle [?]<br /></label>
<input id="fbkurl" type="text" name="fan_fbk" value="e.g. SportsFan12" onFocus="clearText(this)" /><br />
<label for="twiturl">Twitter Handle [?]<br /></label>
<input id="twiturl" type="text" name="fan_twit" value="e.g. AboutSports2012" onFocus="clearText(this)" /><br />
<label for="phoNum">Phone Number<br /></label>
<input id="phoNum" type="text" name="fan_pho" value="cell or home phone" onFocus="clearText(this)" /><br />
<input style="background-image:url('img/save.png');" type="submit" name="saveAbout" value="" id="submit" />
</form>
<div class="success" style="display:none;">Got it!</div>
This HTML
connects to the JS
file submitvalues.js
//////////////// About Tab
$(document).ready(function(){
$("form#FanDetail").submit(function() {
// store the values from the form input box, then send via ajax below
var bio = $('#bio').val();
var dob = $('#dob').val();
var zip = $('#actualZip').val();
var occup = $('#actualOccup').val();
var fbkurl = $('#fbkurl').val();
var twiturl = $('#twiturl').val();
var phoNum = $('#phoNum').val();
$.post(
"../src/php/registration/about/submitvalues_about_tab.php",
$("form#FanDetail").serialize(),
function(){
$('form#FanDetail').hide(function(){
$('div.success').fadeIn();
});
});
return false;
});
});
This JS file, then connects to a php script that sends them to mysql
via PDO
UPDATED PHP:
///////////////////////////////////////////////
######## Get Input to Submit ############## //
///////////////////////////////////////////////
$fanBio = $_POST['bio']; //////
$fanDob = $_POST['dob']; //////
$zipval = $_POST['zip']; //////
$occupval = $_POST['occup']; //////
$facebookurl = $_POST['fbkurl']; //////
$twitterurl = $_POST['twiturl']; //////
$phoneNum = $_POST['phoNum']; //////
$fanID = 1;
///////////////////////////////////////////////
try{
## Get current user and their session ID:
$sessionvar = session_id();
### DB Connection already established above.
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
// INSERT CLEAN DATA INTO TABLE…
$sth = $dbh->prepare("
UPDATE Fan
SET fanBio=?,fanDob=?,fanDetLocID=?,occupID=?,fanFbk=?,fanTwit=?,fanPho=?
WHERE fanID = ?
");
$sth->bindValue(1,$fanBio);
$sth->bindValue(2,$fanDob);
$sth->bindValue(3,$zipval);
$sth->bindValue(4,$occupval);
$sth->bindValue(5,$facebookurl);
$sth->bindValue(6,$twitterurl);
$sth->bindValue(7,$phoneNum);
$sth->bindValue(8,$fanID);
$sth->execute();
$count = $stmt->rowCount(); // check row count
$sth->debugDumpParams(); ## debugging query
}
Error I'm getting: None. It says after I fill in all the input
s Got it!
and thus it had fired off the .success
class.
I tried:
- Manually entering in this information via mysql - works fine.
- Running Javascript Console to debug, it is sending it fine on the JS end, since I get this, once I click the send button:
When I fill in the input and look at the JS Console under Network->Response I get on the PHP FILE:
SQL: [107]
UPDATE Fan
SET fanBio=?,fanDob=?,fanDetLocID=?,occupID=?,fanFbk=?,fanTwit=?,fanPho=?
WHERE fanID = 1
Params: 7
Key: Position #0:
paramno=0
name=[0] ""
is_param=1
param_type=2
Key: Position #1:
paramno=1
name=[0] ""
is_param=1
param_type=2
Key: Position #2:
paramno=2
name=[0] ""
is_param=1
param_type=2
Key: Position #3:
paramno=3
name=[0] ""
is_param=1
param_type=2
Key: Position #4:
paramno=4
name=[0] ""
is_param=1
param_type=2
Key: Position #5:
paramno=5
name=[0] ""
is_param=1
param_type=2
Key: Position #6:
paramno=6
name=[0] ""
is_param=1
param_type=2
Should $sth->execute();
have all the bound params in it as an array??
Solución
try the following i hope it will post here is a more detailed tutorial oh how it works
http://api.jquery.com/jQuery.ajax/
$(document).ready(function(){
$("form#FanDetail").submit(function() {
// store the values from the form input box, then send via ajax below
var bio = ;
var dob = $('#dob').val();
var zip = $('#actualZip').val();
var occup = $('#actualOccup').val();
var fbkurl = $('#fbkurl').val();
var twiturl = $('#twiturl').val();
var phoNum = $('#phoNum').val();
$.ajax({
type: "POST",
url: "../src/php/registration/about/submitvalues_about_tab.php"
data: "bio=" + bio + "&dob=" + dob +"&zip=" + zip + "&occup=" + occup +"&fbkurl=" + fbkurl + "&twiturl=" + twiturl +"& phoNum=" + phoNum,
}).done(function( msg ) {
$('div.success').fadeIn();
});
return false;
});
});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow