send parameter with jquery ajax - ajax

I've a php page and I would to send a jquery ajax request to another page for sending email. This new page accept one parameter and then it executes query ecc...
<div id="customer" name="customer" style="visibility: hidden;"><?php echo $customer; ?></div>
<?php echo '<input id="pulsante" type="image" src="includes/templates/theme485/buttons/english/button_confirm_order.gif" onclick="inoltra();"> '; ?>
this is the script
function inoltra(){
var jqxhr = $.ajax({
type: "POST",
url: "../gestione_di_canio/services.php",
datatype: "json",
data: ??????????????
async:true,
success:function() {
try{
alert("ok");
}catch (e){
alert("correggi");
}
}
});
}
how can I pass to data value of div "customer"?

On data: { varName: $('#customer').html() }
on Php:
$varName= $_POST['varName'];
For a simpler sintax, you use this (it the same):
$.post("../gestione_di_canio/services.php", { varName: $('#customer').html() } )
.success(function() {
try{
alert("ok");
}catch (e){
alert("correggi");
}
});

function inoltra(){
var jqxhr = $.ajax({
type: "POST",
url: "../gestione_di_canio/services.php",
datatype: "json",
data: { customer: $('#customer').html() },
async:true,
success:function() {
try{
alert("ok");
}catch (e){
alert("correggi");
}
}
});
}

I hope this will work for ya...
function inoltra(){
({var jqxhr = $.ajax
type: "POST",
url: "../gestione_di_canio/services.php",
datatype: "json",
data: {"parameter1": value1, "parameter2": value2},// e.i data: {"customer": $('div#customer').html() },
async:true,
success:function() {
try{
alert("ok");
}
catch (e)
{
alert("correggi");
}
}
});
}
as many parameters you want to send would be accessable.

function inoltra(){
var jqxhr = $.ajax({
type: "POST",
url: "../gestione_di_canio/services.php",
datatype: "json",
data: JSON.stringify({paramName: $('#customer').html()}),
async:true,
success:function() {
try{
alert("ok");
}catch (e){
alert("correggi");
}
}
});
}

Related

I am passing JSON data to dependent input field

GET http://localhost:8000/getguardrate/1?_token=kSrGyMIF7twomtmIShadxWbqhNpUKlrxH6lEwDO6 500 (Internal Server Error)
this is my ajax call
$(document).ready(function() {
$('#select2-3').on('change', function() {
var c_id = $(this).val();
console.log(c_id);
if(c_id) {
$.ajax({
url: 'getguardrate/' +c_id,
type: "GET",
data : {"_token":"{{ csrf_token() }}"},
dataType: "json",
success:function(data) {
// console.log(data);
if(data){
$('#GetsiteguardRate').empty();
$('#GetsiteguardRate').focus;
$("#GetsiteguardRate").val(data[0]['sbr_g']);
}else{
$('#GetsiteguardRate').empty();
}
}
});
}else{
$('#select2-3').empty();
}
});
});

Sending url using ajax on codeigniter

I have this Ajax script where I pass a link to the data variable link, but I
get a 412 error.
$(function() {
$(".check-multi").change(function() {
$.ajax({
type: "POST",
url: "<?php echo site_url('adm/updateproperty'); ?>",
async: true,
data: {
link: $(this).data('link')
},
success: function(msg) {
alert('Success');
if (msg != 'success') {
alert('Fail');
}
}
});
});
});
I have tried
link: encodeURI($(this).data('link'))
And
link: encodeURIComponent($(this).data('link'))
as is suggested on other threads but I still get the 412 error message.
Hope this will help you :
you have added newline character to json data that is why you got error
Do like this :
var link = $(this).data('link');
data: {"link" : link},
/*----OR do this -----*/
data: {"link" : $(this).data('link')},
instead of this :
data: {
link: $(this).data('link')
},
Whole code should be like this :
var link = $(this).data('link');
/*console.log(link)*/
$.ajax({
type: "POST",
url: "<?php echo site_url('adm/updateproperty'); ?>",
async: true,
data: {"link" : link },
success: function(msg) {
alert('Success');
if (msg != 'success') {
alert('Fail');
}
}
});
For More :https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/412
Please change your code in data options. Use this way
data: {"id": ID},
i.e. store the value in a variable and send that variable in data option. If we assume
ID=$(this).data('link');`
Then the code will be as follows:
$(function() {
$(".check-multi").change(function() {
$.ajax({
type: "POST",
url: "<?php echo site_url('adm/updateproperty'); ?>",
async: true,
data: {"id":ID},
success: function(msg) {
alert('Success');
if (msg != 'success') {
alert('Fail');
}
}
});
});
});
Please check it.

Ajax Form Submit with attachment

I have a Form on my Site thats submitted true ajax. This Form has a field where to attache .pdf files. How when submitting the form though the file is not send with the rest of data.
How can i get this to work?
Here is my ajax code:
$('#submit_btn').click(function () {
$.ajax({
type: 'POST',
url: '/contact.php',
dataType: "json",
data: $('#contactform').serialize(),
success: function (data) {
console.log(data.type);
console.log(data.msg);
var nClass = data.type;
var nTxt = data.msg;
$("#notice").attr('class', 'alert alert-' + nClass).text(nTxt).remove('hidden');
//reset fields if success
if (nClass != 'danger') {
$('#contactform input').val('');
$('#contactform textarea').val('');
}
}
});
return false;
});
On the php side i have phpmailer setup and am handling the file so:
if(!empty($_FILES['file'])) {
$_m->addAttachment($_FILES['file']['tmp_name'],$_FILES['file']['name']);
}
$('#submit_btn').click(function () {
var formData = new FormData($('#contactform'));
$.ajax({
type: 'POST',
url: '/contact.php',
// dataType: "json",
data: formData ,
processData: false,
contentType: false,
success: function (data) {
console.log(data.type);
console.log(data.msg);
var nClass = data.type;
var nTxt = data.msg;
$("#notice").attr('class', 'alert alert-' + nClass).text(nTxt).remove('hidden');
//reset fields if success
if (nClass != 'danger') {
$('#contactform input').val('');
$('#contactform textarea').val('');
}
}
});
return false;
});

Cannot pass data ajax in ci

$("body").on('click', '.name', function(e) {
//var valueofbutton = $(this).val();
$.ajax({
type: "POST",
url: "response",
data: "name=John&location=Boston",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
});
my response controller
Class Response extends CI_Controller{
public function index()
{
$data=$this->input->post('name');
echo $data;
}
}
it shows me some errors i dont know what kind of error is it !
my alert gives this information
Data Saved: <br />
<b>Warning</b>: Unterminated comment starting line 25 in <b>C:\xampp\htdocs\vacationgod\application\controllers\response.php</b> on line <b>25</b><br />
John
and i cant see any print information like john which i pass to response controller
Just change your ajax code.Controller is ok.
$("body").on('click', function(e) {
$.ajax({
type: "POST",
url: '<?=base_url("controller_name/function_name") ?>',
data: {name: "John",location:"Boston"},
success: function(response) {
alert("Data Saved: " + response);
}
});
});
Your data should be an object and remove .name. change that line in you ajax like this:
$("body").on('click', function(e) {
$.ajax({
type: "POST",
url: "response.php",
data: {name: "John",location:"Boston"},
success: function(msg) {
alert("Data Saved: " + msg);
}
});
});

asp.net mvc ajax crazy

From a page Index.cshtml, i have a very simple ajax call
controller1 and controller2 have the same action:
public ActionResult abc(string name)
{
return new JsonResult()
{
Data = new
{
success = true,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
}
};
}
$(document).ready(function ($) {
$.ajax({
url: '#Url.Action("abc", "controller1")',
type: 'POST',
data: { name: 'John' },
dataType: 'json',
success: function (result) {
if (result.success) {
alert("ok");
}
else {
alert(result.error);
}
}
});
});
not work
however using exactly the same syntax, just change the controller, it works !!!.
abc action is the same.
$.ajax({
url: '#Url.Action("abc","controller2")',
type: 'POST',
data: { name: 'John' },
dataType: 'json',
success: function (result) {
if (result.success) {
alert("ok");
}
else {
alert(result.error);
}
}
});
it drives me crazy, don't know what happen. On a new project, i tried the same code, it works perfectly, but not on my current work.

Resources