How to send variables via ajax/php

Asked

Viewed 2,091 times

-1

Dear I need to send more than one variable via ajax/php, I performed some tests without success, how should I proceed?

PHP (this works but I can’t insert another variable)

$("#colPasta").focus(function(){
    var val = new Array();
    $('.check:checked').each(function(){
        val.push($(this).val());
    });
    $.ajax({
        url:'colPasta.php',
        type:'GET',
        data:'col=<?php print $colar; ?>',
        success:function(data){
            $('.exibeColPasta').html(data);
        }
    });
    return false;
});

I have tried using the following PHP command without success.

data:{ 'col'= <?php echo $colar; ?>, 'ovo'= <?php echo $ovo; ?>},

I stand by.

  • I was able to apply more than one variable for sending, but still unsuccessful for arrays data:{col:'<? php print $paste; ? >',mov:'<? php print $colar2; ?>'},

4 answers

2

Have you tried that?

 var dadosajax = {
    'campo1' : 'valor',
    'campo2' : 'valor'
}

and then to recover

$campo1 = $_REQUEST['campo1'];
$campo2 = $_REQUEST['campo2'];

and ajax would look like this...

 pageurl = 'php/finalizou.php';
$.ajax({
    url: pageurl,
    data: dadosajax,
    type: 'POST',
    cache: false,
    error: function(){
        alert('Erro: Inserir Registo!!');
    },
    success: function(result)
    { 
        if($.trim(result) == '1')
        {
            console.log('certo')
        }
        //se foi um erro
        else
        {
           console.log('erro')
        }

    }
});
  • So Yuri, in case I need to send 3 arrays separately, so send more than one variable.

  • I will apply a multidimensional array and treat internally, is not the best solution but I believe it serves for the moment.

  • despite sending a multidimensional it takes as a string :(

  • Yes because we used in the string 'string', but you can put any value, for example take the value of an input. $('#inputid'). attr('value');

2

That’s the way it is:

$.ajax({
    url:'colPasta.php',
    type:'GET',
    data: { col: <?php print $colar; ?>, col_2: <?php print $colar2; ?>, col3: <?php print $colar3; ?> }
    success:function(data){
        $('.exibeColPasta').html(data);
    }
});
  • So Zoom, so in my code I couldn’t rescue, I’ve seen some applications like this but I think I’m doing something wrong, even though vlw.

1

I am always in favor of practices that facilitate the visualization of the code.

Then, in this case considering the variables "loose" $ovo and $colar, I would use the function compact to turn a list of variables into array and code for json, to make it easier to pass parameters to date.

$.ajax({
    url:'colPasta.php',
    type:'GET',
    data: <?php echo json_encode(compact('colar', 'ovo')) ?>,
    success:function(data){
        $('.exibeColPasta').html(data);
    }
});

So echo json_encode(compact('colar', 'ovo')) returns the following JSON string:

{"ovo" : "valor da variável $ovo", "colar" : "valor da variável $colar"}
  • Sorry ignorance Wallace but to recover json in ajax is normal or has some instruction before?

  • Recover you say the answer or the "Compact" asked in the question?

  • Recover I say ... $var = $_request['egg'];

  • do not use the request use get or post

1

good here you are

search is a form accao is the one you can send more if you want

    var accao = {"action": "enviadados"};
var data = $("#pesquisa").serialize() + "&" + $.param(accao);
$.ajax({
    type: "POST",
    url: ".php",
    dataType: 'json',
    data: data,success: function (data) {} });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.