PHP 从 HTML5 获取数据属性

标签 php jquery mysql ajax html

我遇到了一些问题!

I want to show both news-evt and news-ntc content in my web page. The JS can POST "news-evt, news-ntc", but the PHP cannot process with this result, how can I do? I am new of php and ajax... Thanks!

我的表:

**id**   **category**
  0        news-evt
  1        news-ntc

我的 HTML:

<ul id="blog-filters">
    <li data-vid="news-evt, news-ntc"><a>All</a></li>
    <li data-vid="news-evt"><a>Event</a></li>
    <li data-vid="news-ntc"><a>Notice</a></li>
</ul>

我的JS:

    $(document).ready(function() {
    $(document).on('click', '#blog-filters li', function(){  
        var last_id = $(this).data("vid"); 
        $.ajax(
        {
            url:"load_data.php",  
            method:"POST",
            data:{last_id:last_id},
            dataType:"text",
            success:function(data) {
                if(data !== '') {
                    $('.post-item').remove();
                    $('.btn-load-more').remove();
                    $('.blog-content-rows').append(data);  
                }  else  {  
                    $('.btn-load-more a').html("No More...");  
            }},
        });
    });
});

我的PHP:

if (isset($_POST['last_id'])){

    require_once('datalogin.php');
    $last_id = ($_POST['last_id']);
    $showLimit = 3;
    $output = '';  
    $id = '';
    sleep(1);

    $raw_results = mysql_query("SELECT * FROM article WHERE (category LIKE '%".$last_id."%') ORDER BY date DESC LIMIT ".$showLimit) or die(mysql_error());

    if(mysql_num_rows($raw_results) > 0)  {
        while($results = mysql_fetch_array($raw_results)) {
            $id = $results['id'];
            $output .= 'Something Code...';
        }
    }
}

Solved: Simply change$last_id = $_POST['last_id'] to $last_id = explode(',',$_POST['last_id']) and then add foreach ($last_id as $item) {$raw_results = mysql_query............}

New Problem: But there is a Order problem...the foreach loop output 'evt' result first...

最佳答案

您始终可以使用 PHP 的 echo

<div class="blog-content-rows"> 
<?php
    if (isset($_POST['last_id'])){
        require_once('datalogin.php');
        $last_id = ($_POST['last_id']);
        $showLimit = 3;
        $output = '';  
        $id = '';
        sleep(1);

        $raw_results = mysql_query("SELECT * FROM article WHERE (category LIKE '%".$last_id."%') ORDER BY date DESC LIMIT ".$showLimit) or die(mysql_error());

        if(mysql_num_rows($raw_results) > 0)  {
            while($results = mysql_fetch_array($raw_results)) {
                $id = $results['id'];
                $output .= 'Something Code...';

                //new code here:
                echo "<div>ID: ". $id ."<br/> Output: ". $output ."</div>";
            }
        }
    }
?>
</div>

结果是这样的:

<div class="blog-content-rows">
    <div>ID: 1<br/> Output: Some_text</div>
    <div>ID: 2<br/> Output: Some_other_text</div>
    <div>ID: 3<br/> Output: Another_text</div>
</div>

请随时提出任何其他要求。

关于PHP 从 HTML5 获取数据属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44545819/

相关文章:

javascript - 带有选择类的语义 UI 多级下拉列表不会展开

java - SQL 一行等于两行合并

php - 避免延迟加载 Doctrine Symfony2

php - Moodle主题升级使网站无法使用

php - 在 ajax 请求中使用 JSON 响应更新 JQuery Progressbar

javascript - 使用 for 循环切换 div

javascript - jquery中的灯箱居中

mysql - 如何确定我在 Linux、MyISAM 和 InnoDB 中使用的是哪种类型的 mysql?

mysql - 使用 where & CONCAT 问题更新 Select

PHP XSL 条件语句(CHOOSE、WHEN、IF、OTHERWISE)导致 500 内部服务器错误