我必須返回折扣券一個JSON Ajax代碼...代碼工作正常,但只要可能,我運行AJAX頁面查詢。該響應停止工作MySQL查詢在JSON代碼不工作
JavaScript代碼:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
$(document).ready(function() {
$("#submit_btn").click(function() {
//get input field values
var user_name = $('input[name=discountBox]').val();
//simple validation at client's end
//we simply change border color to red if empty field using .css()
var proceed = true;
if(user_name==""){
$('input[name=discountBox]').css('border-color','red');
proceed = false;
}
//everything looks good! proceed...
if(proceed) {
//data to be sent to server
post_data = {'discountBox':user_name};
//Ajax post data to server
$.post('checking-discount.php', post_data, function(response){
//load json data from server and output message
if(response.type == 'error')
{
output = '<div class="error">'+response.text+'</div>';
}else{
output = '<div class="success_2">'+response.text+'</div>';
//reset values in all input fields
$('#contact_form input').val('');
}
$("#result").hide().html(output).slideDown();
}, 'json');
}
});
//reset previously set border colors and hide all message on .keyup()
$("#contact_form input, #contact_form textarea").keyup(function() {
$("#contact_form input, #contact_form textarea").css('border-color','');
$("#result").slideUp();
});
});
Ajax頁面: 檢查,discount.php
if($_POST){
include_once("db.php");
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
//exit script outputting json data
$output = json_encode(
array(
'type'=>'error',
'text' => 'Request must come from Ajax'
));
die($output);
}
//check $_POST vars are set, exit if any missing
if(!isset($_POST["discountBox"]))
{
$output = json_encode(array('type'=>'error', 'text' => 'Please enter the code!!'));
die($output);
}
//Sanitize input data using PHP filter_var().
$user_Name = filter_var($_POST["discountBox"], FILTER_SANITIZE_STRING);
//additional php validation
if(strlen($user_Name)<4) // If length is less than 4 it will throw an HTTP error.
{
$output = json_encode(array('type'=>'error', 'text' => 'Discount Code is too short or empty!'));
die($output);
}
$coupQuery = "select * tableName where field = '$user_Name'";
$result = mysql_query($coupQuery) or die('MySql Error' . mysql_error());
$row = mysql_fetch_array($result);
//print_r($couRow);
$discountAmt = 10;
//proceed with PHP email.
//if(count($couRow)>0){
$output = json_encode(array('type'=>'message', 'text' => $coupQuery));
die($output);
//}else{
//$output = json_encode(array('type'=>'error', 'text' => 'Could not find the Discount Code! Please check your code.'));
//die($output);
//}
}
每當我刪除$結果和$行代碼其查詢工作正常,但它不允許提取的行..
請讓我知道我錯在哪裏..?
check-discount.php的代碼在哪裏? – Niklas
首先,你在查詢中有一個錯誤,即select * from tablename not select * tablename,那麼你爲什麼要將mysqlquery作爲關聯數組值? – NaveenThally