PHP-цикл с jquery на динамических полях выбора - PullRequest
0 голосов
/ 06 ноября 2011

Любая помощь по этому вопросу была бы великолепна. Я схожу с ума и не могу найти ничего в сообщениях, которые я могу понять. Я пытаюсь создать форму, которая работает так же, как Excel в цикле php do. JQuery для динамического изменения оценки в зависимости от того, что выбрано в теме. Я получил это далеко, но оценка меняется во всех 10 рядах, когда первый предмет выбран. http://www.gogettersgo -learn.com / ggluser / gglcandidatemulti_form.php

Любая помощь будет отличной !!!

Вот код, который я использую:

ФОРМА СТРАНИЦЫ:

<?PHP include_once dirname(__FILE__).'/gglprotectedmain.php'; ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>GoGetters Go Learn Candidate Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="../css/style.css" media="screen" />
<link type="text/css" href="../css/ui-lightness/jquery-ui-1.8.16.custom.css" rel="stylesheet" />
<meta name="ROBOTS" content="NOINDEX,NOFOLLOW"> 
<script type="text/javascript" src="../js/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../js/jquery-ui-1.8.16.custom.min.js"></script>
<script src="../js/jquery.js" type="text/javascript"></script>
<script src="../js/custom.js" type="text/javascript"></script>
<script type="text/javascript">

            $(function() {
                $( ".datepicker[]" ).datepicker({ 
                            minDate: new Date(<?php echo $startyear = 1920; ?>, <?php echo $startmonth = 1; ?> - 1, <?php echo $startday = 1; ?>), maxDate: new Date(<?php echo $endyear = 1994; ?>, <?php echo $endmonth = 12; ?> - 1, <?php echo $endday = 31;?>),
                            dateFormat: 'dd/mm/yy',
                            changeMonth: true,
                            changeYear: true,
                            yearRange: '1920:1994'


                            });
                 filter('.datepicker').removeClass('hasDatepicker').datepicker();
            });

</script>
<style type="text/css">
            /*demo page css*/
            body{ font: 62.5% "Trebuchet MS", sans-serif; margin: 50px;}
            .demoHeaders { margin-top: 2em; }
            #dialog_link {padding: .4em 1em .4em 20px;text-decoration: none;position: relative;}
            #dialog_link span.ui-icon {margin: 0 5px 0 0;position: absolute;left: .2em;top: 50%;margin-top: -8px;}
            ul#icons {margin: 0; padding: 0;}
            ul#icons li {margin: 2px; position: relative; padding: 4px 0; cursor: pointer; float: left;  list-style: none;}
            ul#icons span.ui-icon {float: left; margin: 0 4px;}
        </style>    
</head>
<body>
<img src="../images/GoGettersGo-Learn-Logo.png" width="412" height="119">
<?PHP require_once ('dbphp.php'); $ID = $_SESSION['email'];?>

<table width="3000px" border="0" cellspacing="0" cellpadding="0">
    <tr> 
        <td>           
            <form name="candidateform" method="post" action="../ggluser/gglcandidate_form_code.php">  
            <input type="hidden" value="<?PHP echo $ID;?>" name="ID">            
            <table border="1" cellspacing="0" cellpadding="5">
                <tr> 
                    <td valign="top" width="230px"> 
                        <font color="#000000" size="2" face="Arial, Helvetica, sans-serif">family name:</font><br> 
                    </td>
                    <td valign="top" width="230px"> 
                         <font color="#000000" size="2" face="Arial, Helvetica, sans-serif">full names:</font><br>
                    </td>
                    <td valign="top" width="170px">
                         <font color="#000000" size="2" face="Arial, Helvetica, sans-serif">date of birth:</font><br>
                    </td>
                    <td valign="top" width="110px"> 
                        <font color="#000000" size="2" face="Arial, Helvetica, sans-serif">gender:</font><br>
                    </td>
                    <td valign="top" width="470px">
                        <font color="#000000" size="2" face="Arial, Helvetica, sans-serif">Subject:</font><br>                  
                    </td>
                    <td valign="top" width="230px">
                        <font color="#000000" size="2" face="Arial, Helvetica, sans-serif">Grade:</font><br>
                    </td>
                    <td width="100px">
                        <font color="#000000" size="2" face="Arial, Helvetica, sans-serif">Teacher:</font> 
                    </td>
                    <td width="100px">
                        <font color="#000000" size="2" face="Arial, Helvetica, sans-serif">Special needs candidate:</font> 
                    </td>
                    <td width="100px">
                        <font color="#000000" size="2" face="Arial, Helvetica, sans-serif">Publish results:</font> 
                    </td>
                </tr>                           
            </table>


 <?php

 $i = 0;

    do {
           echo" <table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">";
                echo"<tr>";
                    echo"<td valign=\"top\" width=\"230px\">";
                        echo"<input class=\" \" name=\"familyname[]\" id=\"familyname\" type=\"text\" maxlength=\"250\" size=\"30\" />";
                    echo"</td>";
                    echo"<td valign=\"top\" width=\"230px\">";
                    echo"<input name=\"fullname[]\" id=\"fullname\" type=\"text\" maxlength=\"250\" size=\"30\" />";
                    echo"</td>";
                    echo"<td valign=\"top\" width=\"170px\">";
                    echo"<p><input type=\"text\" class=\"datepicker\" id=\"datepicker\" name=\"datepicker[]\"></p>";
                    echo"</td>";
                    echo"<td valign=\"top\" width=\"110px\">";
                      echo"<label>";
                        echo"<input type=\"radio\" name=\"gender[]\" value=\"male\" id=\"gender_0\">";
                        echo"m</label>";
                      echo"<label>";
                        echo"<input type=\"radio\" name=\"gender[]\" value=\"female\" id=\"gender_1\">";
                        echo"f</label>";
                      echo"<br>";
                    echo"</td>";
                    echo"<td valign=\"top\" width=\"470px\">";

                        $subject_title = subject_subject();
                    echo"<div class=\"subject_title\" id=\"subject_title\">";

                            echo "<select class=\"s_title\" id=\"s_title\" name=\"subject[]\">";
                                echo "<option value=\"\"></option>";
                                foreach($subject_title as $subject_title):

                                $subject_title = $subject_title['subject_title'];
                                echo "<option value=\"$subject_title\">";
                                echo $subject_title;
                                echo "</option>";
                                endforeach;
                            echo "</select>";   
                            echo $i;
                        echo"</div>";

                    echo"</td>";
                    echo"<td width=\"230px\">";
                        echo"<div class=\"subject_grade\" id=\"subject_grade\"> ";
                        echo $i;
                        echo "</div>";
                    echo"</td>";
                    echo"<td width=\"100px\">";
                        echo"<input name=\"teacherchk[]\" id=\"teacherchk\" type=\"checkbox\" value=\"yes\"><br>";
                        echo"<div id=\"teacherd\">";
                        include_once ('../ggluser/gglteachers_details.php');
                        echo"</div>";
                    echo"</td>";
                    echo"<td width=\"100px\">";
                        echo"<input name=\"spcchk[]\" id=\"spcchk\" type=\"checkbox\" value=\"yes\"><br>";
                        echo"<div id=\"spcd\">";
                        include_once ('../ggluser/gglspecial_details.php');
                        echo"</div>";
                    echo"</td>";
                    echo"<td width=\"100px\">";
                        echo"<input name=\"pubres[]\" id=\"publres\" type=\"checkbox\" value=\"yes\"><br>";
                    echo"</td>";
                echo"</tr>";                        
            echo"</table>";

++$i;
echo $i;
} while ($i < 10);

?>   

            <table width="300">
                <tr> 
                    <td width="50">
                    <input type="submit" name="subok" value=" submit " />
                    </td>
                    <td width="50" align="left">
                    <input name="reset" type="reset" value=" reset " />
                    </td>
                </tr>
            </table>
            </form>
        </td>
    </tr>
</table>

</body>
</html>

СТРАНИЦА JQUERY:

$(document).ready(function(){
        $('.s_title').change(function(){
                    $.post(
                            '../ggluser/gglpopulate_subjectphp.php',
                            { subject_title:$('.s_title').val()},
                            function(res){
                                    $('.subject_grade').html(res);

                            }
                           );

            });

});


$(document).ready(function(){

        //Hide div w/id extra
       $(".subject_title").css("display","yes");
        // Add onclick handler to checkbox w/id checkme
       $(".subjectchk").click(function(){

        // If checked
        if ($(".subjectchk").is(":checked"))
        {
            //show the hidden div
            $(".subject_title").show("fast");
            $('.subject_grade').html(res);
        }
        else
        {      
            //otherwise, hide it 
            $(".subject_title").hide("fast");
            $(".subject_grade").hide("fast");
        }
      });
  });

СТРАНИЦА ФУНКЦИЙ PHP:

function subject_subject(){
    $con = dbConnect();
    $qry = "SELECT * FROM tblgosubjectdetails GROUP BY subject_title";
    if(!$result = mysql_query($qry)){
        echo mysql_error();
        return false;
    }
    $result = tablearray($result);

    mysql_close();

    return $result;

}

function subject_grade($subject_title){
    $con = dbConnect();
    $qry = "SELECT * FROM tblgosubjectdetails WHERE subject_title = '$subject_title' GROUP BY subject_grade";
    if(!$result = mysql_query($qry)){
        echo mysql_error();
        return false;
    }
    $result = tablearray($result);

    mysql_close();

    return $result;

}

СПАСИБО ЗА ПРЕДЕЛА !!! :)

1 Ответ

0 голосов
/ 06 ноября 2011

Для проблемы, которую вы упомянули, вы, вероятно, захотите, чтобы ваша change функция выглядела примерно так

 $('.s_title').change(function(){
     var $self = $(this);
     $.post(
           '../ggluser/gglpopulate_subjectphp.php',
           { subject_title: $self.val() },
           function(res){
               $self.closest('tr').find('.subject_grade').html(res);
           }
     );
 });

При наличии она сопоставляет все элементы с классом subject_grade

Еще несколько предложений

  • Я думаю, $('.datepicker[]') выдаст вам ошибку.Возможно, вы захотите $('.datepicker')
  • Убедитесь, что вы не присваиваете один и тот же id более чем одному элементу HTML, что вы часто делаете в цикле do...while.
  • Подумайте хотя бы о том, чтобы положить весь свой код JavaScript в одном месте, либо в отдельном файле, либо внизу вашей страницы.
  • Вы можете обернуть весь свой код jQuery в один $(document).ready(function() { ... }) блок.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...