1

JS code:

function saveCategories(i){
    var categoriesList=JSON.parse(localStorage.getItem("categoriesList"));
    var code=localStorage.getItem("gameCode");

    if(i == categoriesList.length)
        return;

    var categoryName=categoriesList[i]['name'];
    var items=categoriesList[i]['items'];

    $.get('http://localhost:8080/yourfolder/newCategory.php',{GameCode:code,items:items,CategoryName:categoryName},function(resp)
        {
            saveCategories(i+1);
        });
}

PHP code:

<?php 
header("Access-Control-Allow-Origin");

$host="127.0.0.1";
$user="root";
$pass="password";
$databaseName="games";

$con=mysql_connect($host,$user,$pass,$databaseName);

if(!$con)
{
    die("Connection failed: ".mysqli_connect_error());
}

$code=$_GET["GameCode"];
$name=$_GET["CategoryName"];
$items=$_GET["items"];

$data=array();
foreach($items as $item)
    $data[]=addcslashes($item);
$data=implode(",",$data);

$sql="INSERT INTO games.categories(code,name,items) VALUES ('$code','$name','$data')";
$result=mysql_query($sql,$con);

echo $result;
?>

The php file (newCategory.php) should get code, category name and category items and insert them to MYSQL db.

The saveCategories recursive function should get the newCategory.php file for each category (in index i), but for some reason, it proceed to the next iteration without getting a result from the first GET request before.

The unwanted result is that the first category in index 0 is not saved in the MYSQL db.

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
  • Hi, just one query where it the saveCategories method called for the first time (i.e. saveCategories(0)), initially called. – A. A. Sebastian Aug 23 '17 at 07:10
  • I call the saveCategories function in this way:saveCategories(0) and the first category should be saved. –  Aug 23 '17 at 07:15
  • Hi, I would suggest you to first print the response of the API in JS before you call the saveCategories(i+1); method. Then post the response here that might help you and us to solve the problem. – A. A. Sebastian Aug 23 '17 at 07:34
  • The response is 1 for each category.That's because I print the result of mysql_query($sql,$con); in the php code. –  Aug 23 '17 at 08:18
  • Possible duplicate of [How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?](https://stackoverflow.com/questions/133310/how-can-i-get-jquery-to-perform-a-synchronous-rather-than-asynchronous-ajax-re) – Dharman Jul 05 '19 at 21:00

2 Answers2

1

use async: false in your data

example:

 $.ajax({
    url : "/your url",
    type : "get",
    async: false,
    success : function(userStatus) {
       //your logic
    },
    error: function() {

    }
 });
Nemani
  • 778
  • 5
  • 12
  • 1
    I get this warning:"jQuery has deprecated synchronous XMLHTTPRequest", and it still doesn't save the first category. –  Aug 23 '17 at 07:12
  • synchronous requests on the main thread have been deprecated due to the negative effects to the user experience. – Nemani Aug 23 '17 at 08:04
  • you can debug this code by putting breakpoint before calling saveCategory(i+1) you will have to check for `i` Value as well as the response data i.e. `resp'. – Nemani Aug 23 '17 at 08:10
  • I get a good response for each i, but the first category is not saved. –  Aug 23 '17 at 08:16
  • what, how and where are you trying to save? – Nemani Aug 23 '17 at 08:19
  • I am trying to save code, category name and category items in MYSQL db for each category with the newCategory php code above. –  Aug 23 '17 at 08:21
  • whats initial value of `i` you are passing when first time you are calling `saveCategories` – Nemani Aug 23 '17 at 08:24
  • The initial value is:0 –  Aug 23 '17 at 08:29
0

referring to this answer you can specify the asynchronous option to be false to get a synchronous Ajax request using jquery. You just need to specify

{async:false} 

this article shows how to do it using a XMLHttpRequest().

marvel308
  • 10,288
  • 1
  • 21
  • 32