1

I have a this variable:

var order = $('#listadoImages').sortable('serialize');

When send the order var from JSON to PHP, shows "id[]=18&id[]=19", my problem is I can not read that in php. I tried with foreach but returns error.

My ajax code:

 $("#mainAction").click(function(){
         //alert("New position: " + ui.item.index());
            var order = $('#listadoImages').sortable('serialize');
            var rutas = [];
            $('#listadoImages li').each(function(index, li){
                var bg = $(this).find('span').css('background-image');
                bg = bg.replace('url(','').replace(')','').replace(/\"/gi, "");
                var res = bg.substring(42);

                if(index == 0){
                    rutas += res;
                }else{
                    rutas += ";" + res;
                }

            });


            //alert(rutas);
            $.ajax({
              type: "POST",     
              url: "functions/guardar.php",
              data: {
              orden : order,
              path : rutas
              },
              "success":function(data){

              }
            });
        });

My PHP code:

$paths =  $_POST['path'];
$orden = $_POST['orden'];
$rutas = explode(";", $paths);

if(isset($paths)){

    $sql = "SELECT * FROM imagenes";
    $respuesta = mysql_query($sql);

    if($respuesta){
    $i=0;
        while( $rs = mysql_fetch_assoc($respuesta) ){
            $rutaBD = $rs['ruta_imagen'];
          foreach ($orden as $k => $value) {
            foreach ($rutas as $key => $rut) {
                        for($i=0;$i<count($rut);$i++){
                            if($rutaBD == $rut){
                               //Here Update Statment
                            }
                        }
                }       
            }


         }      

    }

Thank you for readme. I hope someone can help me. Thank you! Regards.

4
  • I don't see you trying to use $orden anywhere in your PHP? (it should contain an array ['id => [first, second, third, ...etc...]]. Dump the `$_POST? variable and see what it contains. Commented Oct 3, 2016 at 18:18
  • Hi @Magnus Eriksson the JSON returns "id[]=18&id[]=19" and cant read from php Commented Oct 3, 2016 at 18:20
  • JSON is a format and can't "return" anything. Are you talking about the Javascript? Commented Oct 3, 2016 at 18:22
  • @MagnusEriksson my problem its in "orden : order" in $.ajax call this shows me id[]=18&id[]=19 in php $_POST['orden']. And i need the value id for update statment. Understand me? Commented Oct 3, 2016 at 18:27

1 Answer 1

1

You need to use parse_str to turn the string into an array. It will then create an array $id that you can iterate through.

parse_str($_POST['orden']);
var_dump($id);

To iterate through the array, you can use foreach, or better yet, use implode to put it into an IN clause.

$sqlUpdate = "UPDATE imagenes SET ruta_imagen='".$rut."', orden=".$i." WHERE id_imagen IN (".implode(',',$id).")";

Please be aware that this sql query is open to SQL injection. I'd highly recommend moving to PDO or mysqli, and using prepared statements. You also need a comma between your ruta_imagen and order assignments.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi @aynber! Yes its serves me but how do I go these data and use the id in update statment??
Hard to tell without your update statement. You can use a foreach loop to iterate through the values.
Sorry @aynber my update its "$sqlUpdate = "UPDATE imagenes SET ruta_imagen='".$rut."'orden=".$i."WHERE id_imagen=" $ord;" where "ord" is the id that was in $ _ POST [ 'order']. When use foreach foreach ($newarray as $k => $ord) give me error "Invalid argument suppled for foreach()". My newarray definition its: $newarray = parse_str($_POST['orden']);
parse_str uses the array keys for the assignment, and does not return anything. $newarray would just be a null variable. Please see the linked php.net page.
Thank you for your help! Can resolve one part! I dont use prepared statment because its a test! ;-) Regards!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.