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
Related
I need to read data from my web page with jquery + ajax
This is my function:
public function getvalueshahr()
{
if($_POST['ostan']!=0){
$db= JFactory::getDbo();
$query=$db->getQuery(TRUE);
$query->select('id,title')->from('#__categories')->
where($db->quoteName('parent_id').'='.$db->quote($_POST['ostan']));
$db->setQuery($query);
$res=$db->loadObjectList();
echo $db->getErrorMsg();
echo json_encode($res);}
}
I can read data with c# like this:
But my ajax method is not working. Here is the method:
$.ajax({
type: "POST",
url: "www.mysite.net/index.php?task=shahrestan.getvalueshahr",
data: "{ostan=77}",
dataType: "json",
success: function (result)
{
var d = $.parseJSON(result);
$.each(d, function (i, field)
{
$("#output").append("id: " + field.id + " title: " +
field.title+"br/>");
});
},
error: function (e)
{
alert("error:" + e.responseText);
}
});
The method returns nothing.
You're sending a string instead of an object in your ajax-request. Try changing :
data: "{ostan=77}"
to
data: { ostan: 77 }
And you should set the content type before echoing the results in PHP:
header('Content-Type: application/json');
echo json_encode($res);
Now you don't need $.parseJSON in the JS-code. You will get a json-object straight away.
First of all you send a wrong data to the server. You have to rewrite part of your code like this:
data: {ostan: 77},
or
data: "ostan=77",
The second problem is you get a result as a json object on the success method, but you try to parse it again to the json object. So remove this line from your code:
var d = $.parseJSON(result);
Finally your ajax function should looks like this:
$.ajax({
type: "POST",
url: "www.mysite.net/index.php?task=shahrestan.getvalueshahr",
data: {ostan: 77},
dataType: "json",
success: function (result)
{
$.each(result, function (i, field)
{
$("#output").append("id: " + field.id + " title: " +
field.title+"br/>");
});
},
error: function (e)
{
alert("error:" + e.responseText);
}
});
If you are loading data from you mysql-database you eventually have to convert these data because it is not stored as utf-8 converted. This piece of code convert given object to UTF-8: mb_convert_encoding(database-entry,'UTF-8');
I would like to onsubmit, fetch the positioning of all the divs on the page and return the ids of the divs and the absolute positioning values.
This is the result I get in the console of how the array looks:
[Object { name="app1", coord="10,10"}, Object { name="app2", coord="60,10"}]
One, am I populating the array correctly? Two, how can I print it and return a string via result?
HTML page:
var apps = $(".block"),
positions = [];
$.each(apps, function (index, app) {
var positionInfo = $(app).position();
var input = $(this).attr('id');
var lines = input.split('_');
var appname = lines[0];
positions.push({value: appname + "," + positionInfo.top + "," + positionInfo.left});
console.log(appname + ":" + positionInfo.top + ":" + positionInfo.left);
});
$.ajax({
type: 'post',
cache: false ,
url: 'result_savechange.php',
data: {result:positions},
success: function(resp) {
$('#mainCanvas').html(''); // Clear #content div
$('#mainCanvas').append(resp);
}
});
PHP page:
<?php
$string = '';
foreach($_POST["result"] as $position){
$string .= $position['value'];
}
echo $string;
?>
There's no reason to use JSON here. It's overkill for this scenario. Just do this:
$.ajax({
type: 'post',
cache: false ,
url: 'result_savechange.php',
data: {result: positions},
success: function(resp) {
alert(resp);
}
});
Then in PHP, $_POST["result"] will be an array.
foreach($_POST["result"] as $position){
echo $position['name'];
echo $position['coord'];
}
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'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! &
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.