How to send variables via ajax / php

-1

I need to send more than one variable via ajax / php, I did some unsuccessful tests, how should I proceed?

PHP (this works but I can not 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've tried using the following PHP command without success.

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

I'm waiting for you.

    
asked by anonymous 26.01.2016 / 12:43

4 answers

2

It's like this:

$.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);
    }
});
    
26.01.2016 / 12:53
2

Have you tried this?

 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')
        }

    }
});
    
26.01.2016 / 12:51
1

I'm always in favor of practices that make it easier to see the code.

So in this case considering the "loose" variables $ovo and $colar , I would use the compact function to transform a list of variables into array and would code to json , to be more easy 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);
    }
});

Then echo json_encode(compact('colar', 'ovo')) would return the following JSON string:

{"ovo" : "valor da variável $ovo", "colar" : "valor da variável $colar"}
    
26.01.2016 / 12:57
1

Good here you have

search is a form the action is what you send 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) {} });
    
26.01.2016 / 13:32