duancaoqin6683 2019-02-18 15:57 采纳率: 0%
浏览 65

通过jQuery和PHP发出设置元素的html

I have a td with a class name of "seminar_date_time" that I'm trying to set the inner html of after converting a datetime into a string format that I want. Here's my code...

if($stmt->execute()) {
    $stmt->bind_result($id, $seminar_date, $am_pm, $room, $building, $speaker_lname, $speaker_fname, $title);

    while($stmt->fetch()) {
        ?>
            <script>
                var date_object = new Date("<?php echo $seminar_date; ?>");

                var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                var month = monthNames[date_object.getMonth()];
                var day = date_object.getDay();
                var year = date_object.getFullYear();
                var hour = date_object.getHours();
                var mins = date_object.getMinutes();
                var time_string = null;
                var ampm = null;

                if(hour > 12) {
                    hour -= 12;
                    ampm = "PM";
                }
                else {
                    ampm = "AM";
                }

                if(mins === 0) {
                    mins = "00";
                }

                time_string = `${hour}:${mins} ${ampm}`;

                // using ES6 string template literal for date string
                var date_string = `${month} ${day}, ${year} at ${time_string}`;

                console.log(`date and time: ${date_string}`);

                                $(".seminar_date_time").html(date_string);
            </script>
            <tr class="clickable_row" data-href="seminar_attendance.php?id=<?php echo $id; ?>">
                <td class="seminar_date_time"></td>
                <td><?php echo $room; ?></td>
                <td><?php echo $building; ?></td>
                <td><?php echo "$speaker_lname, $speaker_fname"; ?></td>
                <td><?php echo $title; ?></td>
            </tr>
        <?php
    }
}

When I render this to the page the date string in that td is the same for every row. When I console.log() my newly formed date string, it logs it perfect every time. The next line after that console.log() is setting the html via jQuery on the td with class name "seminar_date_time".

I know the php code is being set server side, so I'm guessing there's a racing problem between setting the server side content and setting the jQuery client side?

How can I get my new date string into each row's td correctly?


EDIT:

It sounds like the consensus here is that I need to be formatting the date in PHP instead of doing it through JS. That is what I plan to do now, but maybe these answers could still help someone else that needs to mix PHP and JS in this way. Thank you to all that answered.

  • 写回答

3条回答 默认 最新

  • dsovc00684 2019-02-18 16:06
    关注

    The variables you declare in the <script> tag are global variables, they are not scoped to the current entry in the loop. This means that when you add several such script tags you end up changing the variable, not creating a new one. What you need to do is create a scope for the variables to live in that isn't global.

    if($stmt->execute()) {
        $stmt->bind_result($id, $seminar_date, $am_pm, $room, $building, $speaker_lname, $speaker_fname, $title);
    
        while($stmt->fetch()) {
            ?>
                <script>
                (function(){
                    var date_object = new Date("<?php echo $seminar_date; ?>");
    
                    var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                    var month = monthNames[date_object.getMonth()];
                    var day = date_object.getDay();
                    var year = date_object.getFullYear();
                    var hour = date_object.getHours();
                    var mins = date_object.getMinutes();
                    var time_string = null;
                    var ampm = null;
    
                    if(hour > 12) {
                        hour -= 12;
                        ampm = "PM";
                    }
                    else {
                        ampm = "AM";
                    }
    
                    if(mins === 0) {
                        mins = "00";
                    }
    
                    time_string = `${hour}:${mins} ${ampm}`;
    
                    // using ES6 string template literal for date string
                    var date_string = `${month} ${day}, ${year} at ${time_string}`;
    
                    console.log(`date and time: ${date_string}`);
    
                    $(".seminar_date_time").html(date_string);
                }());
                </script>
                <tr class="clickable_row" data-href="seminar_attendance.php?id=<?php echo $id; ?>">
                    <td class="seminar_date_time"></td>
                    <td><?php echo $room; ?></td>
                    <td><?php echo $building; ?></td>
                    <td><?php echo "$speaker_lname, $speaker_fname"; ?></td>
                    <td><?php echo $title; ?></td>
                </tr>
            <?php
        }
    }
    

    This should work for you.

    But, I have to comment that this is not the best way to mix php and JavaScript. You should try to do all of this in php only, rather than mix it on the frontend and backend.

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记