Here, I have this jQuery code that passes values to a php file that handles database queries.
$("#item-input-form__complete-form-btn").click(function(){
var invoiceInfo = {
"date" : $("[name=datepicker]").val(),
"op_id" : 11111,
"invoice_number" : $("[name=invoice]").val(),
"vendor_id" : 33333
};
var jsonResult = $('#added-items__table').tableToJSON();
$.ajax({
type: "POST",
url: "insert_items_to_db.php",
data: {QUERY_TYPE:"INSERT",
INVOICE_INFO:invoiceInfo,
JSON_RESULT:jsonResult},
success: function(){alert("SUCCEEDED");},
error: function(xhr, textStatus, error){
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
});
And here I have the php file that handles the passed parameters and run queries.
insert_items_to_db.php::
<?php
include_once('models/dbapi.php');
$query_type = $_REQUEST["QUERY_TYPE"];
$invoice_info = $_REQUEST["INVOICE_INFO"];
$product_json = $_REQUEST["JSON_RESULT"];
$DB = new DBAPI();
$DB->connectTo('BBY');
$DB->insertDataIntoStockMasterTable($invoice_info, $product_json);
$DB->insertDataIntoStockDetailTable($invoice_info, $product_json);
$DB->disconnect();
?>
And the method in DBAPI class that runs queries looks something like this.
dbapi.php ::
function insertDataIntoStockMasterTable($invoice_info, $product_json)
{
$invoice_date = $invoice_info['date'];
$invoice_number = $invoice_info['invoice_number'];
$op_id = $invoice_info['op_id'];
$vendor_id = $invoice_info['vendor_id'];
$today_date = date("Y-m-d");
$invoice_date = date("Y-m-d", strtotime($invoice_date));
$total_amt = 0;
for($i = 0; $i < count($product_json); $i++)
{
$total_amt += $product_json[$i]['Extended Price'];
}
$query ="INSERT INTO stock_master (sm_date, sm_invoice_no, sm_vendor_cd, total_amt, op_id, op_date)
VALUES('$invoice_date', $invoice_number, $vendor_id, $total_amt, $op_id, '$today_date')";
$result = mssql_query($query, $this->selected_hannam_db_server);
return $result;
}
The problem here is... I can't figure out how to check whether queries have crashed. In such cases, jquery should pop up a msg box telling users that inserting data into db has failed.
How can I pass error msgs from php to jquery?