douqing5981 2016-10-08 07:58
浏览 70

如何在php中使用javascript函数?

There are two tables in a page. One is for Friday and other is for Saturday. I want to set the choice limit of radio button selection from a table. User can not select more than 3 slots out of 4 slots from Friday table. How can I call a javascript function for a table? How can I do this? Here is my php code and javascript function. Output looks like this

This javascript function is working for both table. When I select 3 radio button from any table it shows "You can not add more than 3". But I want this only for Friday table not for Saturday.

  1. PHP code:

                  for($time=0; $time<4; $time++){
                    $q.= "<td colspan='1' valign='top' width='25%'>";
                        $q.= "<table id='ash'>";
                        $q.= "<tr id='grey'>";
                        if($day==1){
                            if($time==0){
                                $q.= "<th width='50%'></th></tr>";
                            }else if($time==1){
                                $q.= "<th width='50%'></th></tr>";
                            }else if($time==2){
                                $q.= "<th>Reg.</th>";
                                $q.= "<th width='50%'>2:30 pm - 5:00 pm</th>";
                                $q.= "<th>Sec</th><th>Remaining</th></tr>";
                            }else{
                                $q.= "<th>Reg.</th>";
                                $q.= "<th width='50%'>5:00 pm - 7:30 pm</th>";
                                $q.= "<th>Sec</th><th>Remaining</th></tr>";
                            }
                        } 
                      else{
                            $q.= "<th>Reg.</th>";
                            if($time==0){
                                $q.= "<th width='50%'>8:30 am - 11:00 am</th>";
                            }else if($time==1){
                                $q.= "<th width='50%'>11:00 am - 1:30 pm</th>";
                            }else if($time==2){
                                $q.= "<th width='50%'>2:30 pm - 5:00 pm</th>";
                            }else{
                                $q.= "<th width='50%'>5:00 pm - 7:30 pm</th>";
                            }
                            $q.= "<th>Sec</th><th>Remaining</th></tr>";
                        }
                        $query1 = "SELECT tab1.*, tab2.course_name, tab2.course_type FROM routine AS tab1, course_info AS tab2"; 
                        $result = mysql_query($query1);
                        if ($result && mysql_num_rows($result) > 0) {
                            while($row = mysql_fetch_assoc($result)){
                                $course = $row["course_code"];
                                $sec = $row["section"];
                                $type = $row["course_type"];
                                if(course_completed($std_id, $course)){
                                    if(already_selected($course, $sec, $std_id, $year, $sem)){
                                        $q.= "<tr bgcolor='#00FF33'><td><input type='radio' id='course_".$day."_".$time."' name='".$day.$time."' value='".$course.",".$sec.",".$type."' checked='checked' onclick='changeRadioValue(\"".$day."_".$time."\")' /></td>";
                                    }
                                    else{
                                        $q.= "<tr bgcolor='#00FF33'><td><input type='radio' id='course_".$day."_".$time."' name='".$day.$time."' value='".$course.",".$sec.",".$type."' onclick='changeRadioValue(\"".$day."_".$time."\")' /></td>";
                                    }
                                }
    
                           else{
    
                                    if(already_selected($course, $sec, $std_id, $year, $sem)){
                                        $q.= "<tr><td><input type='radio' id='course_".$day."_".$time."' name='".$day.$time."' value='".$course.",".$sec.",".$type."' checked='checked' onclick='changeRadioValue(\"".$day."_".$time."\")' /></td>";
                                    }else{
                                        $q.= "<tr><td><input type='radio' id='course_".$day."_".$time."' name='".$day.$time."' value='".$course.",".$sec.",".$type."' onclick='changeRadioValue(\"".$day."_".$time."\")' /></td>";
                                    }
                                }
    
  2. JavaScript function

    $("input[type='radio']").change(function(){
        var count = $("input[type='radio']:checked").length;
        if(count>3){
            $(this).prop('checked', false);
          alert("You cannot add more than 3");
        }
    });
    
  • 写回答

2条回答 默认 最新

  • dongou3158 2016-10-08 09:51
    关注

    Try this:

    <?php
    if(condition) {
        ?>
        <script>//JS Code</script>
        <?php
    }
    ?>
    

    When you page loads, if the condition is true the js code between the script tags will run, you can also link another js file instead of writing code.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀