2

I'm currently having some trouble posting a variable on jQuery for MySQLi SELECT on a PHP page.

Code of jQuery:

$("#carta1").click(function()
{
    cartaId = document.getElementById("carta1").value;
    console.log(cartaId);

    ajaxGetResults = $.ajax({
        context: this,
        type: "POST",
        url: "darResposta.php",
        data: {'cartaId' : cartaId},
        cache: false,
        dataType: "json"
    })
    .done(function(data){
        $('#3').html(data);
        console.log("Avançou para a terceira parte");
        $("#2").hide();
        $("#3").show();
    })
    .fail(function(){
        console.log('Erro ao buscar dados');
        $("#2").hide();
        $("#3").show();
        $('#3').html("Deu erro");
    });
});

Code of PHP:

if(!$conn)
{
    echo "Falhou a ligação à base de dados";
}
else
{
    if(isset($_POST['cartaId']))
    {
        $cartaId = $_POST['cartaId'];
        $res = mysqli_query($conn,"
            SELECT cartaNome, cartaDescricao
            FROM tarot_cartas
            WHERE cartaId = ".$cartaId
        );
        $data = array();
        while($row = mysqli_fetch_assoc($res))
        {
            $data=$row;
        }
        echo json_encode($data);
    }
}

Tried several approaches to this problem such as putting the $cartaId outside the if statement with a direct $_POST, and nothing happened.

Would appreciate if you could shed some light on this problem.

Thanks for taking the time to read and suggest a solution.

  • 2
    Your query is open to SQL Injections, use a prepared statement or at least escape the post data you pass in to the query. – Jite Jul 07 '15 at 10:00
  • How can I do that? Quite new on jQuery. – Simão Oliveira Jul 07 '15 at 10:02
  • You do it on the server side (php), all data passed from the client (for example any data in the $_POST array) is dirty, escape it or use [`Prepared statements`](http://php.net/manual/en/mysqli.prepare.php). – Jite Jul 07 '15 at 10:03
  • 1
    See [How can I prevent SQL-injection in PHP?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1) – Barmar Jul 07 '15 at 10:04
  • I've been finding some results via trial and error... But now I can't seem to show the array as it should... Gives me mainly "undefined undefined".. – Simão Oliveira Jul 07 '15 at 16:03

3 Answers3

2

use below code

  data: { 'cartaId' : cartaId },

instead of

   data: {"data":JSON.stringify({'cartaId' : JSON.stringify(this)})},
Nishit Maheta
  • 6,021
  • 3
  • 17
  • 32
1
Solution:
1.Remove this

  data: {"data":JSON.stringify({'cartaId' : JSON.stringify(this)})},

2.Replace This one

  data: { cartaId: cartaId }

Hope it works....

Prabhagaran
  • 3,620
  • 1
  • 19
  • 19
1

The jq you have will post a variable to the url. To debug you should first check in a console (i use firebug) for mozilla) if the request is sent. Using firebug you can see the names of the POST variables you send.

Following this you should check what values get received on the server side by doing

var_dump($_POST);

Finally get the correct variable into your query. You can also debug the query by viewing the log file or, depending on whether you are using a framework, something like CI:

db->last_query();

Lpgfmk
  • 391
  • 1
  • 4
  • 17