Here is the code:

Js:

function regUtmInDB(){ var done = ""; var utm_name = $("#utm_name").val(); var manager = $("#manager").val(); var site = $("#site").val(); var prod_id = $("#prod_id").val(); var age_min = $("#age_min").val(); var age_max = $("#age_max").val(); var gender = $("#gender").val(); var subids = $('.subid'); // var s = Array; // for(i=0;i<subids.length;i++){ // if($("#subid_"+i+"_value").val()!=""){ // s[i] = $("#subid_"+i+"_value").val(); // } // } $.ajax({ url: "cart/subidReg.php", type: "POST", data: ({ utm_name : utm_name, manager : manager, site : site, prod_id : prod_id, age_min : age_min, age_max : age_max, gender : gender, subids : subids }), processData: false, success: function(data){ done = data; }, }); return done; } 

PHP:

 <?php if (isset($_POST["utm_name"])){ $utm_name = $_POST["utm_name"]; $manager = $_POST["manager"]; $site = $_POST["site"]; $prod_id = $_POST["prod_id"]; $age_min = $_POST["age_min"]; $age_max = $_POST["age_max"]; $gender = $_POST["gender"]; $subids = $_POST["subids"]; if($utm_name!=""){echo true;} else{echo false;} }else{ echo true; } ?> 
  • done - Returns the value of "" - i.e. Empty - Adrian Golub
  • 2
    Recently asked a similar question, but for clarity I repeat. Callback ajax is not executed instantly. First, ajax will wait for a response from the server, and then it will perform a callback, but everything below the ajax request code will be executed without waiting for an answer (in your case, return done; ). You will immediately try to return a variable that is not yet defined (in a callback). If you want to work with this variable - work in a callback, this is a common practice. - Misha Saidov
  • Thanks - It helped! - Adrian Golub

0