Reputation: 4695
I am posting a form with the following:
if ( flag ) {
jQuery.ajax({
url: 'submit_licence.php',
dataType: 'json',
type: 'POST',
data: 'flag=' + flag + '®ion=' + jQuery('#licenceRegion').val() + '&lnum=' + jQuery('#enterLicence').val() + '&fname=' + jQuery('#fname').val() + '&lname=' + jQuery('#lname').val(),
success: function( data ) {
}
});
}
The HTML
<input class="buttondb4 white2 large" type="text" value=" " id="enterLicence" />
<input type="hidden" name="licenceRegion" id="licenceRegion" value="" />
<input type="hidden" name="fname" id="fname" value="First" />
<input type="hidden" name="lname" id="lname" value="Last" />
and my PHP:
if ($_POST){
$lnum = noescape($_POST['lnum']);
$fname = noescape($_POST['fname']);
$lname = noescape($_POST['lname']);
$region = noescape($_POST['region']);
$flag = noescape($_POST['flag']);
//mysql query here
}
But when its posted, it is not retrieving any of the posted data (the posted values are blank)
Why is it not pulling the data? What am I missing? What have I done wrong?
Unfortunately I cannot post the entire script/page due to it being a private project for a client although I think I have provided enough information for what I am trying to do.
Upvotes: 2
Views: 982
Reputation: 5770
Read this on SO
You need to wait for the return of the function:
onSubmit="return sendData()"
Otherwise the form will be submitted immediatly and does'nt wait till data is changed.
inside the function replace this
document.data.submit();
with this:
return true;
Link posted in comment above
Upvotes: 1
Reputation: 1
Hvae you confirmed the values you pass in AJAX have values? Try a simple alert and the var to see what is in it.
If the alert shows a value then change datatype to text and see if it passes data. DONT USE THIS FOR FINAL SOLUTION, just debugging
Upvotes: 0
Reputation: 1193
You haven't provided nearly enough info on this question. How do you trigger the ajax request? If it happens inside a submit()-handler, you'll need to prevent the default behavior using event.preventDefault() or the request will be aborted by the default browser behavior. Are you sure you actually get inside the statement at all? This can be checked using Firebug to see if a request is even made, like many others have suggested.
So, try these simple things:
Seeing as you're not really supplying enough info on how and where you call this code, I can only make guesses on what the problem is.
Hope this was helpful :)
Upvotes: 0
Reputation: 6946
Use Firebug for Firefox to inspect the HTTP headers using Ajax and without Ajax (just set <form method="post"...>
and add a <input type="submit">
).
Upvotes: 1
Reputation: 1862
HTML
<script type="text/javascript">
$(document).ready(function(){
...
if ( flag ) {
$.ajax({
url: 'submit_licence.php',
dataType: 'json',
type: 'POST',
data: 'flag=' + flag + '&' + $('#someId').serialize(),
success: function( data ) {
}
});
}
});
</script>
...
<form id='someId'>
...
<input class="buttondb4 white2 large" type="text" value=" " id="enterLicence" />
<input type="hidden" name="licenceRegion" id="licenceRegion" value="" />
<input type="hidden" name="fname" id="fname" value="First" />
<input type="hidden" name="lname" id="lname" value="Last" />
</form>
checks whether the data has been sent, " print_r($_POST); " , and this should work
//print_r($_POST);
if ($_POST){
$lnum = noescape($_POST['lnum']);
$fname = noescape($_POST['fname']);
$lname = noescape($_POST['lname']);
$region = noescape($_POST['region']);
$flag = noescape($_POST['flag']);
//mysql query here
...
echo json_encode($result);
}
Upvotes: 1
Reputation: 1395
jQuery ajax calls fail silently when the json response is malformed.
Make sure that you use this format for json:
{"firstName": "John", "lastName": "Smith", "age": 25}
Do not use single quotes.
PHP's json_encode()
does it correctly.
To further debug the error use a complete(XMLHttpRequest, textStatus)
callback.
The second argument is the error type. (see jQuery.ajax)
Upvotes: 2
Reputation: 29160
Being relatively new to jQuery Ajax, Im not sure, but could this be a reason?
data (Object, String): Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests.
Perhaps since this is not a GET, the data is 'falling off' ?
Upvotes: 0