I have some conditional variables in the following code. For example, if the var shipping == “true”, then shippingAddress1, shippingAddress2, etc exist. But, if shipping == “false”, these variables are undefined, and left blank. My problem is that when I try to post, I get a js error because some of the vars are undefined. Is there any way send these through blank?
$.post( 'reserveAPickupAppointmentCreatePickup.php', {
'location': appointmentLocation,
'appointmentAddress1': appointmentAddress1,
'appointmentAddress2': appointmentAddress2,
'serviceType': serviceType,
'shipping': shipping,
'shippingAddress1': shippingAddress1,
'shippingAddress2': shippingAddress2,
'shippingCity': shippingCity,
'shippingState': shippingState,
'shippingZip': shippingZip,
'startTime': startTime,
'endTime': endTime,
'date': date,
'estimatedSize': appointmentSize,
'highValueItems': highValueItems,
'insuredItems': insuredItems,
'tools': tools,
'presentAtAppointment': presentAtAppointment,
'contactName': contactName,
'contactPhone': contactPhone,
'contactEmail': contactEmail,
'itemList': itemList,
'studentPhone': studentPhone
},
Set reasonable defaults for the variables you’re using. If it’s a string, default it to an empty string. In general don’t use variables if they haven’t been defined at some point.