Marco
Marco

Reputation: 3

Pass data from $_POST to jQuery

I want to send a input data (code_bar ) at return.php page, which after a MySQL select callback 3 data ( prezzo;prodotto;descrizione ) but it doesn't work. I post here html file with JavaScript and PHP file which calls a data from select to my db.

HTML

<form action="" method="post" name="">
Codice a Barre <p>
<input id="code_bar" name"code_bar" />
<button onclick="button">Chiama</button><p>
Prodotto<p>
<input id="Prodotto" name="Prodotto" /><p>
Prezzo<p>
<input id="Prezzo" name="Prezzo"  /><p>
Descrizione <p>
<input id="Descrizione" name="Descrizione"  /

SCRIPT

<script type="text/javascript">
function invia(){
    var code_bar = $("input#code_bar").val();
     $.ajax({
      url:"return.php", 
      data: {code_bar: 'code_bar'},
      success:function(data) {
            $("#Prezzo").val(data.Prezzo);
            $("#Prodotto").val(data.Prodotto);
            $("#Descrizione").val(data.Descrizione);
           
        
       "json"}
     });
}
</script>

return.php

<?php
 if(isset($_POST['code_bar'])){
      $code_bar = $_POST['code_bar'];
   }
mysql_select_db($database_mydb, $mydb);
$query_estraggo = "SELECT * FROM prodotti WHERE code_bar = '$code_bar'";
$estraggo = mysql_query($query_estraggo, $mydb) or die(mysql_error());
$row_estraggo = mysql_fetch_assoc($estraggo);
$totalRows_estraggo = mysql_num_rows($estraggo);

      if ($row_estraggo = mysql_fetch_assoc($estraggo)){
          $ritorno = '{"Prezzo":'.$row_estraggo['Prezzo'].',"Prodotto":'.$row_estraggo['Prodotto'].',"Descrizione":'.$row_estraggo['Descrizione'].'}';
        

         $json = $JSON->encode($ritorno);
         echo $json;
         exit($ritorno);
          
      }
mysql_free_result($estraggo);
?>

Upvotes: 0

Views: 302

Answers (3)

Abhishek
Abhishek

Reputation: 691

        You should also define type of input
        <input id="code_bar" name"code_bar" type="text"/>
    In $.ajax default type is get if you send data with post method then add type:"post" in $.ajax method

    $.ajax({
          url:"return.php", 
          type:"post",
          data: {code_bar: code_bar},
          dataType:"json",
          success:function(data) {
                $("#Prezzo").val(data.Prezzo);
                $("#Prodotto").val(data.Prodotto);
                $("#Descrizione").val(data.Descrizione);


           }
         });

Upvotes: -1

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123438

you're passing in GET a string instead of the actual value

data: {code_bar: 'code_bar'},

write instead

data: {code_bar: code_bar},

anyway you should at least be able to understand where your call is failing (in you ajax call or in the server side script): e.g. Firebug has a xhr panel in which you can clearly see how data is passed

As a sidenote, in your php you should avoid all mysql_* function in favour of PDO statements

Upvotes: 3

xdazz
xdazz

Reputation: 160983

The default type of request by $.ajax is GET, you need to set it to POST, or use $.post instead. (Your $.ajax has syntax error).

And data: {code_bar: 'code_bar'}, will cause code_bar always be string code_bar, it should be data: {code_bar: code_bar},.

Upvotes: 1

Related Questions