html使用ajax报500,javascript - Ajax: 500 Internal Server Error - Stack Overflow

Let's tidy this up and simplify it a bit...

function updateResults() {

query = {"town_id": $('#town_id').val(),

"value_type": $('#filter_type').val(),

"value_date": $('#filter_date').val()

}

$.ajax({

type: "POST",

url: "system/live_filter.php",

data: query,

cache: false,

success: function(html){

$("#results").html(html);

}

});

};

and then in the PHP:

require_once 'db.php';

// Define Output HTML Formating

$html = '';

$html .= '

';

$html .= '

titleString

';

$html .= '

typeString

';

$html .= '

dateString

';

$html .= '

';

$town_id = $_REQUEST['town_id'];

$type = $_REQUEST['value_type'];

$date = $_REQUEST['value_date'];

// Prepare values for database results query

$town_id = $db->real_escape_string($town_id);

$type = $db->real_escape_string($type);

$date = $db->real_escape_string($date);

// Build Query

$query = "SELECT * FROM events WHERE towns_id='$town_id' AND type='$type' AND date>='$date'";

/*Should it definitely be towns_id and not town_id?*/

// Do Search

$results = $db->query($query);

while($result = $results->fetch_assoc()) {

// Insert title

$output = str_replace('titleString', $result['title'], $html);

// Insert type

$output = str_replace('typeString', $result['type'], $output);

// Insert date

$output = str_replace('dateString', $result['date'], $output);

// Output

echo($output);

}

Of course if you're willing to move the template, it gets even simpler...

require_once 'db.php';

$town_id = $db->real_escape_string($_REQUEST['town_id']);

$type = $db->real_escape_string($_REQUEST['value_type']);

$date = $db->real_escape_string($_REQUEST['value_date']);

$query = "SELECT * FROM events WHERE towns_id='$town_id' AND type='$type' AND date>='$date'";

$results = $db->query($query);

while($result = $results->fetch_assoc()) {

$html = '

';

$html .= '

{$result['title']}

';

$html .= '

{$result['type']}

';

$html .= '

{$result['date']}

';

$html .= '

';

echo $html;

}

As to why it's erroring...

Firstly, are you sure the variables are being populated? The error you gave in comments would occur if town_id was missing. Since you're not quoting that field, it would result in broken SQL. It also makes the escaping pointless as the output expects to be in quotes.

I'd also check that the format of the date coming from your form is one that your database understands...

Try changing the PHP to be as follows:

require_once 'db.php';

$town_id = $db->real_escape_string($_REQUEST['town_id']);

$type = $db->real_escape_string($_REQUEST['value_type']);

$date = $db->real_escape_string($_REQUEST['value_date']);

$query = "SELECT * FROM events WHERE towns_id=$town_id AND type='$type' AND date>=$date";

echo $query;

Then take the SQL it gives you and copy/paste it into your database admin tool and see what happens. Once you've fixed the syntax errors there, you'll know how to fix the query in your PHP

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值