user3071859 user3071859 - 1 year ago 49
jQuery Question

jQuery add if statement to post function

I programmed the following:

$.post("helmscript.php", { voorhoofdomtrek: voorhoofdomtrek, leverancier: leverancier, categorie:categorie
}, func......

However, i need an if statement like this in the post function:

$.post("helmscript.php", { if (variableX == true) {voorhoofdomtrek: voorhoofdomtrek,} leverancier: leverancier, categorie:categorie
}, func......

But an if statement in the post function does not work. Could you guys help out?

Answer Source

Initialize an array and populate data in array based on your condition and then json stringfy the data and pass it in the post request.

Please check this code.

var data = [];
if (variableX == true) {data['voorhoofdomtrek'] = voorhoofdomtrek;}
data['leverancier'] = leverancier;
data['categorie'] = categorie;

stringData = JSON.stringify( data );

$.post("helmscript.php", stringData, func......