Ajax serialise - issue with data format - ajax

I have several inputs formatted with this jquery plugin here.
I use $.ajax to do my mysql insert:
$.ajax({
type: 'GET',
url: 'xxxx.php',
data: $('#new_form').serialize(),
}),
I face an issue as my input values are formatted with the plugins and can't get into mysql db.
As an example:
Input value: $450,000.00 is not accepted.
Is there a way to unformat within the serialise function values that have a specific classes (like class="money")?
Thanks for your help!
I have tried the below code:
$.ajax({
type: 'GET',
url: 'xxx.php',
data: $('#new_form').serialize(),
dataType:"json",
beforeSend: function(){
$(".money").cleanVal();
},
<script>
function cleanVal(v) {
return v.replace(/^\,/,'');
};
</script>
the result of the insert in mysql is still 450 for 450,000.
Do you have an idea?
thanks

You can try using the plugin $.cleanVal() method to retrieve the unmasked type value of the corresponding HTML element, prior to your AJAX form submission. So something like this:
$.ajax({
type: 'GET',
url: 'xxxx.php',
data: $('#new_form').serialize(),
beforeSend: function(){
$(".money").cleanVal();
}
}),

I couldn't make it work with beforehand. I found a solution which is to unmask values before calling ajax.
If anyone knows why it does not work with beforesend, thanks for letting me know.
cheers

Related

Meteor HTTP.call and jquery ajax

I would like to use Meteor.call('GET') instead of $.ajax(). I have an ajax call as the following:
$.ajax({
url: url,
crossDomain:true,
type: method,
data: query,
dataType: 'json'
}).done(function(data) {
_tokens.request = {
token: data.oauth_token,
secret: data.oauth_token_secret.split('')
};
});
Have some options but I don't know how to pass to Meteor.call(). Please help!
Thank you so much
You probably mean HTTP.call or HTTP.get. Meteor.call is related to another concept.
http://docs.meteor.com/#/full/http_get

redirecting is not working in ajax

This below script is in view/template folder.
<script>
$(document).ready(function(){
$("#change").click(function(){
$.ajax({
type: "POST",
url: "common/customer.php",
data: { oldemail: $("#oldemail").val(), newemail: $("#newemail").val() }
});
});
});
</script>
I want to send data in common/customer.php but it is not working. I
already used ../common/customer.php but same problem. What is the
solution?
Its a absolute path problem.
Your script is in view/template
If your path of javascript file is like something,
view/template/myjavascript.js
And if your path of customer.php file is
view/common/
Then you must switch your directory by ../
Use
../common/customer.php
Its a relative path of your file.
According to jQuery documentation, you must declare the data type:
$.ajax({
type: 'POST',
url: url,
data: data,
success: success,
dataType: dataType
});
TRY...
url: "/common/customer.php", // note the leading slash
Sorry friends problem was in my html code. I used the type of input submit but when i changed it into button then it is working fine. Thanks to all of you.

Ajax data collect in code lines

I did some admin panel in wordpress sheet but i'm adding options and have everything in one line in the data, it's pain if I keep adding options, works that way but it looks messy.
example
$.ajax({
type: 'POST',
url: ajaxurl,
data: 'action=general_settings_action&zkr_logo='+zkrlogo+'&zkr_favicon='+zkrfavicon+'&zkr_background='+zkrbackground+'&zkr_linkcolor='+zkrlinkcolor+'&zkr_linkhover='+zkrlinkhover+'&zkr_colorbackground='+zkrcolorbackground,
success: function(data){
alert(data);
}});
I would like to make some lines to that data field like for example
$.ajax({
type: 'POST',
url: ajaxurl,
data:
'action=general_settings_action&
zkr_logo='+zkrlogo+'&
zkr_favicon='+zkrfavicon+'&
zkr_background='+zkrbackground+'&
zkr_linkcolor='+zkrlinkcolor+'&
zkr_linkhover='+zkrlinkhover+'&
zkr_colorbackground='+zkrcolorbackground,
success: function(data){
alert(data);
}});
But putting the code like that doesn't work I have tried with \n and some other stuff but still wont do the work.
I apreciate the help... Thanks
Try doing this:
Make a JSON data object that contains the parameters you want to send
var DATA = {
action:'general_settings_action',
zkr_logo:zkrlogo,
zkr_favicon:zkrfavicon,
zkr_background:zkrbackground,
zkr_linkcolor:zkrlinkcolor,
zkr_linkhover:zkrlinkhover,
zkr_colorbackground:zkrcolorbackground
}
The send the data in your AJAX request using the data field
$.ajax({
type: 'POST',
url: ajaxurl,
data: DATA,
success: function(data){
alert(data);
}});
I checked this out at: David Walsh's guide
You need to add
'zkr_logo=' + zkrlogo + '' +
instead of
zkr_logo='+zkrlogo+'&
then it will form one String

How to send json in post body with jQuery mobile

Please help me, what is wrong in this code?
$.ajax({
type: 'POST',
url: baseUrl+url,
data: {language: 'it'},
xhrFields: {
withCredentials : true
}
})
why server receives:
'language=it_IT'
Try to specify your dataType and use JSON.stringify():
$.ajax({
type: 'POST',
url: baseUrl+url,
data: JSON.stringify ({language: 'it'}),
xhrFields: {
withCredentials : true
},
contentType: "application/json",
dataType: 'json'
})
I just run through the same issue: for some reason, whenever you send language parameter using ajax it somehow automatically changes to get and all post parameters get lost. Solution: avoid using language parameter at all (or stringify yout data as #Agash Thamo suggested. That is strange for me and I would really like if somoeno could explain that a little bit better.

CodeIgniter - jQuery, JSON, simple example not working

Ok, Im a real newbie when it comes to ajax and json ... Im trying to figure it out in my codeigniter project.
Ive written something simple to start, just to bring up an alertbox, but it doesnt seem to be working, if someone could let me know where im going wrong, that would be grand.
In my view i have the following code.
$('.users').change(function(){
$.ajax({
type: "POST",
url: "/edituser/returndata",
data: {id: this.find(':selected').val()},
dataType: json,
success: function(data){
alert(data);
}
});
});
in the edituser/returndata controller, i just simply have the following
function returndata(){
echo $_POST['id'];
}
I know this will look pretty stupid to some people, but im still trying to figure it out, if someone could help :)
Cheers
----------------- UPDATED CODE BELOW
<script type="text/javascript" charset="utf-8">
$('#users').live('change', function(){
$.ajax({
type: "POST",
url: "/edituser/returndata",
data: {id: $(':selected', this).val()},
dataType: 'json',
success: function(data){
alert(data.id);
}
});
});
</script>
Controller code
function returndata()
{
$ID = $this->input->post('id'); // Use this instead of $_POST['id']
echo json_encode(array('id'=>$ID));
}
Your dataType should be:
dataType: 'json',
Your data should be:
data: {id: $(this).find(':selected').val()},
Inside of a event callback, this is a DOM element, so it needs to wrapped in $().
or:
data: {id: $(':selected', this).val()},
Which is the same as above, just less characters.
Also, in your PHP, you need to output JSON.
function returndata(){
$ID = $this->input->post('id'); // Use this instead of $_POST['id']
echo json_encode(array('id'=>$ID));
}
Then in your success function, you can do:
alert(data.id);
UPDATE
Disregard the answer. I thought the JSON was sent as a string, but it's not, as pointed out by Rocket. It is converted to url encoded value pairs. I'm leaving the answer up just in case someone thought the same thing as me....
The incoming JSON is not a request parameter, you need to read the body of the request
$json = json_decode(trim(file_get_contents('php://input'));
$id = $json->id;

Resources