0

This has been rattling my brains for 2 days now, and without any luck I thought I would ask here. The problem I'm having is that after getting a string from a ajax request and saving it to a variable, the alert shows correctly "1/2/3/4" however when i split by using var array = variable.split('/'); and try and access the array content it only shows that the array length is 1 and that one is blank. Have used an alert before the string to ensure that the string is holding the required content. Any help would be appreciated.

My code is as followed:

HTML:

var Cid = calEvent.Cid;
            $(".titleU").val(calEvent.title);
            $('.startDateU').val($.fullCalendar.formatDate(calEvent.start, "yyyy-MM-dd HH:mm"));
            $('.endDateU').val($.fullCalendar.formatDate(calEvent.end, "yyyy-MM-dd HH:mm"));
            $(".descriptionU").val(calEvent.description);
            $(".clientU").val(calEvent.clientName);
            $(".workOrderU").val(calEvent.workOrderID);
            $(".jobLocationU").val(calEvent.eventLocation);
            // For loop for adding employee to update panel
            var workOrderEmployeesA = ""; // Update form
            $.ajax({
                type: "POST", 
                url: "<?php echo base_url('scripts/phpScripts/getEmployeesCID.php');?>", 
                data: {Cid: Cid},
                datatype: "html",
                success: function(json){
                    workOrderEmployeesA = $.trim(json).substring(0, json.length -1);
                    alert(workOrderEmployeesA);
                }
            });         
            var workOrderEmployeesB = workOrderEmployeesA.toString().split('/');
            for(var i=0;i<workOrderEmployeesB.length;i++){
                alert(workOrderEmployeesB[i]);
            }

PHP:

    $result = mysqli_query($con,"SELECT * FROM employeeCalendar WHERE CalendarID='$_POST[Cid]'");
$counter = 0;
while($row = mysqli_fetch_array($result)){
    echo $row['EmployeeID'];
    if($counter == mysqli_num_rows($result)){
    }else{
        echo "/";
    }
        $counter++;
}
mysqli_close($con);

The following code works correctly, but not sure how its different in any way that would prevent proper functionality.

var workOrderEmployeesB = "1/2/3/4/5".split('/');
            for(var i=0;i<workOrderEmployeesB.length;i++){
                alert(workOrderEmployeesB[i]);
            }

This is probably something silly that I've over looked, Any suggestions would be much appreciated.

Thank-you in advance,

Chris

Chris.H
  • 45
  • 1
  • 6
  • Ah now i feel silly. Forgot to put the ' async: false, ' in. Thanks for the helpful and fast response. – Chris.H Mar 17 '14 at 21:36
  • That would be the wrong approach, even if it works. Try working around the asynchronous nature of ajax instead. – adeneo Mar 17 '14 at 21:47

2 Answers2

0

Try to call the loop inside success call of Ajax

$.ajax({
            type: "POST", 
            url: "<?php echo base_url('scripts/phpScripts/getEmployeesCID.php');?>", 
            data: {Cid: Cid},
            datatype: "html",
            success: function(json){
                workOrderEmployeesA = $.trim(json).substring(0, json.length -1);
                alert(workOrderEmployeesA);
                 var workOrderEmployeesB = workOrderEmployeesA.toString().split('/');
                 for(var i=0;i<workOrderEmployeesB.length;i++){
                  alert(workOrderEmployeesB[i]);
                 }
            }
        });         
DevelopmentIsMyPassion
  • 3,541
  • 4
  • 34
  • 60
0

It's because you're calling split outside of the AJAX call -- but your AJAX call is asynchronous, so the workOrderEmployeesA variable doesn't exist yet when you're trying to run the split method on it.

You could make the call synchronous (which might not be a good idea depending on how the code is implemented), or you could throw the workOrderEmployeesB variable inside of the AJAX call with everything else, and do everything inside of the AJAX call, if you still want it to run asynchronously and do what you want it to do.

Josh Beam
  • 19,292
  • 3
  • 45
  • 68