I'm working on an existing script at the moment which uses Ajax, something I've never worked with before. I have a variable set in my javascript file which gets its value from an input field on my page. I need to use Ajax to post this to my PHP page only I've no idea where to start,
Im not sure what code you would need to see, but My javascript/AJAX code is, the variable I need to pass is 'var credoff'
$(".getPoint").click(function () {
var theid = $(this).attr("id");
var onlyID = theid.split("_");
var onlyID = onlyID[1];
var credoff = parseInt($(this).children('input.credoff:hidden').val());
$.ajax({
url: 'do.php',
type: 'POST',
data: "userID=" + onlyID,
success: function (data) {
if (data != "success1" && data != "success5") {
$("#" + theid).text(data);
} else {
$("#thediv_" + onlyID).fadeOut("slow");
$('#creditsBalance').fadeOut("slow");
newbalance = parseInt($('#creditsBalance').text());
Wouldit have to be in this format?
data: "userID=" + onlyID,
"credoff=" + credoff
...
data: {
userId: onlyID,
credoff: credoff
},
...
Or you can do this:
data: "userID=" + onlyID + "&credoff=" + credoff
don't forget the ampersand! &
Related
here is my ajax request :
$(".colorme").on("click", function () {
var c = $(this);
var b = "id=" + c.attr("id");
$.ajax({
type: "POST",
url: "../../colorme",
data: b,
success: function (a) {
$.when(c.fadeOut(300).promise()).done(function () {
if (c.hasClass("btn")) {
c.removeClass("btn-default").addClass("btn-success").text(a).fadeIn()
} else {
c.replaceWith('<span class="notice_mid_link">' + a + "</span>")
}
})
}});
return false
})
so here is what I receive as a response :
{"f0d8c0":0.3269616519174,"d8d8d8":0.22377581120944,"181818":0.10926253687316,"d8a890":0.091268436578171,"303030":0.054454277286136}
I would like to be able display each one of those values as a pair.Right now it returns :[object OBJECT]
Use,
data = $.parseJSON(JSON.stringify(a));
Try this.
var obj = jQuery.parseJSON(a);
alert( obj.f0d8c0);
alert( obj.d8d8d8);
You can assign your response value in var a =$.parseJSON(RESPONSE VALUE)
For more Details Read this LINK
$.ajax({type: "POST", url: "../../colorme", data: b, success: function (a) {
resp = jQuery.parseJSON(a);
alert(resp.f0d8c0);
alert(resp.d8d8d8); //maybe you need to use a better way to name the data?
$.when(c.fadeOut(300).promise()).done(function () {
if (c.hasClass("btn")) {
c.removeClass("btn-default").addClass("btn-success").text(a).fadeIn()
} else {
c.replaceWith('<span class="notice_mid_link">' + a + "</span>")
}
})
}});
I need to serialize two forms in the same ajax function with one call. I have tried the following but it just serialize the last one.
function sssssss2(page){
var form1 = document.myform1;
var form2 = document.myform2;
var dataString1 = $(form1).serialize() + '&page=' + page;
var dataString2 = $(form2).serialize() + '&page=' + page;
$.ajax
({
type: "GET",
url: "load_data.php",
data: {dataString1, dataString2}
success: function(ccc)
{
$("#container").html(ccc);
}
});
}
It doesn't work. How may I serialize both of them within the same function?
Try this one
var dataString1 = $(form1).serialize() + '&page=' + page +$(form2).serialize();
data: {dataString1}
function DoAjax( args )
{
var formData = "?" + $("form[name=" + args.formName + "]").serialize();
$.ajax({
type: "POST",
url: args.url + formData,
data: {
request: args.request
},
success: function (data) {
alert(data);
args.callback.html(data);
},
error: function (a, b, c) {
alert("error: " + a + ", " + b + ", " + c + ".");
}
});
return false;
}
I need to pass in a custom bit of data to my php script called "request" which will denote which process to run on the php page... but I also want to serialize any other form fields on my form. Is this the correct way of doing it (add the serialize to the end of the URL) because I just cannot seem to get anything from my PHP $_POST["whatever"]
EDIT:::///
<?php
$request = $_POST[ "request" ];
if( isset( $request ) )
{
require_once( "includes.php" );
function LogInWithClientCred()
{
$username = CheckIsset( "username" );
$password = CheckIsset( "password" );
echo $_REQUEST["username"];
echo $_GET["username"]; // echos correctly the username.. but I want it to be post data instead!
Please ignore the security awesomeness, it is pseudo for your purpose.
Combine formData and your additional data into one string.
var formData = $("form[name=" + args.formName + "]").serialize() + "&request=" + args.request;
$.ajax({
url: args.url,
data: formData,
type: "POST",
...
})
I am not too sure If i got ur question well, But I think you are appending the form data to URL and using POST in ajax.
Can you try
$_REQUEST["whatever"] to see what you are getting
EDIT
After seeing the commnets I think I now got it
function DoAjax( args )
{
var formData = $("form[name=" + args.formName + "]").serialize();
$.ajax({
type: "POST",
url: args.url,
data: {
request: args.request,
formData:formData
},
success: function (data) {
alert(data);
args.callback.html(data);
},
error: function (a, b, c) {
alert("error: " + a + ", " + b + ", " + c + ".");
}
});
return false;
}
Now $_POST[formData] should work
Edit
see this as well
github.com/maxatwork/form2js
I'm trying to add ajax form submission to a PHP web app I'm working on using jquery. The form is being submitted and writing to a database, but it'll still doing it all with a refresh.
Here's my code:
$("form#form_customer").submit(function() {
var customer123first_name = $('input[name=customer123first_name.]');
var customer123last_name = $('input[name=customer123last_name.]');
var customer123date_of_birth = $('input[name=customer123date_of_birth.]');
var customer123email_address = $('input[name=customer123email_address.]');
var customer123telephone_number = $('input[name=customer123telephone_number.]');
var customer123picture = $('input[name=customer123picture.]');
var customer123id_picture = $('input[name=customer123id_picture.]');
var customer123id_expiration = $('input[name=customer123id_expiration.]');
var data = 'customer123first_name=' + customer123first_name.val() + '&customer123last_name=' + customer123last_name.val() + '&customer123date_of_birth=' + customer123date_of_birth.val() + '&customer123email_address=' + customer123email_address.val() + '&customer123telephone_number=' + customer123telephone_number.val() + '&customer123picture=' + customer123picture.val() + '&customer123id_picture=' + customer123id_picture.val() + '&customer123id_expiration=' + customer123id_expiration.val();
$.ajax({
url: "inc/createObject.php",
type: "POST",
data: data,
cache: false,
success: function(data){
$('form_success').fadeIn();
}
});
return false;
});
Everything I've read online for this specific issue has found the return false; call to be in the wrong place or not there at all. I've checked mine is in the right place, I just can't find a reason why it's refreshing.
I know jquery is working because I use it to do popups windows which are working fine.
If you want to see the code in context, go to www.sfyfe.com/studioadmin
The problem is caused by the dot at the end of the selector on your $('input[name=customer123... lines. The dot isn't doing anything, and it's making the selector invalid. Removing the dots should fix the problem. Hope this helps!
Maybe
$("form#form_customer").submit(function(e) {
e.preventDefault();
});
You should send the data in JSON format , using : " data : data " is not valid ,
try:
$.ajax({
url: "inc/createObject.php",
type: "POST",
data:{dataField : data },
Your code should be like this.
$("form#form_customer").submit(function() {
var customer123first_name = $('input[name=customer123first_name]');
var customer123last_name = $('input[name=customer123last_name]');
var customer123date_of_birth = $('input[name=customer123date_of_birth]');
var customer123email_address = $('input[name=customer123email_address]');
var customer123telephone_number = $('input[name=customer123telephone_number]');
var customer123picture = $('input[name=customer123picture]');
var customer123id_picture = $('input[name=customer123id_picture]');
var customer123id_expiration = $('input[name=customer123id_expiration]');
var data = 'customer123first_name=' + customer123first_name.val() + '&customer123last_name=' + customer123last_name.val() + '&customer123date_of_birth=' + customer123date_of_birth.val() + '&customer123email_address=' + customer123email_address.val() + '&customer123telephone_number=' + customer123telephone_number.val() + '&customer123picture=' + customer123picture.val() + '&customer123id_picture=' + customer123id_picture.val() + '&customer123id_expiration=' + customer123id_expiration.val();
$.ajax({
url: "inc/createObject.php",
type: "POST",
data: data,
cache: false,
success: function(data){
$('form_success').fadeIn();
}
});
return false;
});
I have built an AJAX function that links to some PHP on the same page. The PHP worked fine but with the AJAX function there is an error and the validation is not taking place.
The AJAX
function contact() {
var ENQemail = $('#ENQemail').val();
var ENQfirstname = $('#ENQfirst_name').val();
var ENQlastname = $('#ENQlast_name').val();
var ENQmessage = $('#ENQmessage').val();
var ENQsecword = $('#ENQsecword').val();
var dataString = 'ENQemail=' + ENQemail + '&ENQfirstname=' + ENQfirstname + '&ENQlastname=' + ENQlastname + '&ENQmessage=' + ENQmessage + '&ENQsecword=' + ENQsecword;
$.ajax({
type: 'POST',
url: 'http://www.divethegap.com/update/contact',
data: dataString,
dataType:'json',
success: function(data) {
$('#ERRemail').html(data.ERRemail);
$('#ERRfirstname').html(data.ERRfirstname);
$('#ERRlastname').html(data.ERRlastname);
$('#ERRmessage').html(data.ERRmessage);
$('#ERRsecword').html(data.secword);
$("#enquiry").effect("shake", { times:4 }, 100);
},
error: function() {
$("#enquiry").effect("shake", { times:4 }, 100);
},
});
}
Could it be that one cannot have the PHP on the same page when using AJAX.
Any ideas,
Marvellous
It is always a good idea to encode request parameters. This may or may not be the source of your problem:
var dataString = 'ENQemail=' + encodeURIComponent(ENQemail) + '&ENQfirstname=' + encodeURIComponent(ENQfirstname) + '...';
Also, it looks like you are telling the AJAX function to send a JSON message (dataType:'json'). However, the data you are passing into it is not a JSON message.