Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Conditional statement inside ajax data

Tags:

jquery

ajax

Can I make something like this for example?

$.ajax({
  url:'ajax.php',
  type:'POST',
  data: {
    id: 3,
    device: $("#ipole4").val(),
    name: $("#ipole5").val(),
    ip: $("#ipole6").val(),
    method: $("#ipole7").val(),
    if (2 == 2) {
      'info':2
    }
  },
})

I just want to send something more in the special case.

like image 371
pavon147 Avatar asked Nov 25 '25 20:11

pavon147


1 Answers

You can do this, but not with the syntax you have. You need to create the object first, then use the condition statement separately, like this:

var data = {
    'id': 3,
    'device': $("#ipole4").val(),
    'name': $("#ipole5").val(),
    'ip': $("#ipole6").val(),
    'method': $("#ipole7").val() 
};

if (2 == 2)
    data.info = 2;

$.ajax({
    url:'ajax.php',
    type:'POST',
    data: data
}
like image 51
Rory McCrossan Avatar answered Nov 28 '25 15:11

Rory McCrossan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!