使用JSON通过ajax将表单数据发送到php,但是当没有

时间:2015-08-06 00:42:30

标签: php ajax json

我正在使用JSON / Ajax从数据库中查找产品以添加到购物车。我抬头看,当我点击添加到购物车时,屏幕和控制台中没有任何直观的信息显示"未捕获的语法错误:意外的标记<"但是没有错误,因为代码假设将产品添加到会话中,并且当我刷新它时,它会显示cos,购物车显示产品已添加,以及我在第一个后添加的任何后续产品永远不会收到此错误。只是为了第一个产品(如果购物车是空的)..任何人都可以解决一些问题..我已经显示了我在下面使用的代码..

JSON / AJAX代码:

$("form#addtocartform").submit(function( event ){


price = $("#price").val();
model = $("#model").val();
serial = $("#serialno").val();
stockid = $("#stock_id").val();



$.ajax({
  type: "GET",
  url: "ajax/addProductToBasket.php?action=add&price="+price+"&stockid="+stockid+"&model="+model+"&serialno="+serial,
  success: function( result ){

    var json = JSON.parse( result );

    if( json.response.status == "success" ) {
      // Show Success, by closing popup window

    // addtocartline();
    $('#stock_id').val("");
    $('#qty').val("");
    $('#price').val("");
    location.reload(true);      

    } else if ( json.response.status == "error" ) {
      // Display Error Message
      $("#responder").css('display','block');
      $("#responder").html( json.response.message );

    } else {
      // Display Unknown Error Message
      alert( "Just Quit in Life!" );
    }       
  }
});

// Prevent Submit Button from posting Traditional way.
event.preventDefault();
}); 

PHP代码:

if( !isset($_SESSION) ) {

    session_start();

}

include('../classes/functions.inc.php');


// Reset Errors
$errors = "";

$amount     = $_GET['amount'];
$payment_id = $_GET['payment_id'];


// Validation Form
if( $payment_id == "" )
   $errors .= "<li>No Payment Method was chosen</li>";

if( $amount == "" )
    $errors .= "<li>Enter a valid selling price </li>";

// If No Errors
if( $errors == "" ){

    $doPayment = process_payment($amount, $payment_id);


       if ($doPayment == true ) {
        $result = formHandler( "success", "1", "Payment has been accepted. (Close)");
        echo $result;   
        }
        else {
        $result = formHandler( "error", "0", "Payment Failed.  (Close)");
        echo $result;               
    }

  }
  else {
  $result = formHandler( "error", "0", "Correct the following errors:  (Close Message) <ul> $errors <ul>");
echo $result;   
  }

0 个答案:

没有答案