PHP从HTML5获取数据属性

时间:2017-06-14 13:10:32

标签: php jquery mysql ajax html5

我遇到了一些问题!

  

我想在我的网页上显示news-evt和news-ntc内容。   JS可以POST“news-evt,news-ntc”,但PHP无法处理这个结果,我该怎么办?我是php和ajax的新手......谢谢!

我的表:

**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...';
        }
    }
}
  

已解决:只需将$last_id = $_POST['last_id']更改为$last_id = explode(',',$_POST['last_id']),然后添加foreach ($last_id as $item) {$raw_results = mysql_query............}

     

新问题:但是有一个订单问题...... foreach循环输出'evt'结果首先......

1 个答案:

答案 0 :(得分:0)

您始终可以使用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>

随意提出其他要求。