0

i know how to validate form using it for 1 field. But i would like enter code and take back multiple values from my db. exmp: price, quantity etc. It is posible to do using ajac?

php file:

$sql = "SELECT * FROM database WHERE code='$field_code'";
$params = array();
$options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );

$query=sqlsrv_query($conn, $sql, $params, $options);

$row = sqlsrv_fetch_array($query);

  if ($row == true) 
  {
      $code = ($row['code']);
      $life = ($row['life']);
      $agree = ($row['agree']);
  }
echo $code;
echo $life;
echo $agree;
?>

And script is:

$("#field_code").change(function() {
$("#message").html("<img src='pictures/ajax_loader.gif' width='26px' height='26px' /> checking...");


    var data1 = $("#field_code").val();
    $.ajax({
        type: 'POST',
        url: 'validation.php',

        data: $('form').serialize(),
        success: function validate(code) {
            if (data == ? ) {

               to do something    
} else {


to do something


            }

How to receive all 3 values from php file? } })

1
  • echo the $row using json encode in php then access it as object in js. Commented Jun 6, 2014 at 8:37

1 Answer 1

4

you need to use json encode for this

$values[]= array('code'=>$row['code'], 
                   'life'=>$row['life'],
                   'agree'=>$row['agree']);

echo json_encode($values);

and in ajax

var data = jQuery.parseJSON(data);

and access values like data.life,data.code and data.agree

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.