Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

posting dynamic arrays as distinct variables using java script

Emmal MbweEmmal Mbwe ZambiaMember Posts: 1

Am trying to dynamically add the fields for subjects and the grades obtained, but am getting an error "Undefined index: subject in..." when posting those variables using java script.
Could there be something am missing with my posting mechanism. Notice that in the form data am not puting id="subject" to avoid picking the id for only one subject, but I
dont seem to know how to represent this in the java script as we will see below.

form data as follows;

            <tr>
                        <td colspan="6"><div align="center"><strong>
                          <p style="color:#930">Academic Qualification</p>
                        </strong></div></td>
                      </tr>

                              <?php
                     require_once("connection/connectPDO.php");
                     $sql="CALL sp_getSubjects()"; 

                                //Initiate and Call Stored Procedure Using PDO
                                $pdo = new PDOConfig();
                                $resultsSubject = $pdo->query($sql);
                                foreach($resultsSubject as $rowSubject)
                                        {
                    ?> 
                              <tr>
                        <td width="35%" colspan="3"><div align="right"><?php echo $rowSubject['SubjectName']; ?>:<input name="subject[]" type="hidden" value="<?php echo $rowSubject['SubjectID']; ?>" /></div></td>
                        <td width="65%" colspan="3"><select name="grades[]" id="grades" class="validate[required]">
                          <option  value="">--Select Grade--</option>
                                     <?php

                    $sql="CALL sp_grabGrades()"; 

                                //Initiate and Call Stored Procedure Using PDO
                                $pdo = new PDOConfig();
                                $resultset = $pdo->query($sql);
                                foreach($resultset as $row)
                                        {

                    ?>
                       <option value="<?php echo $row['GradeObtainedID']; ?>"> <?php echo $row['Grade']; ?> </option>
                                <?php } ?>
                        </select></td>
                                 <?php } ?>
                      </tr>

the form looks like this

English <--select-->
Biology <--select-->
Science <--select-->

the java script code is as follows;

            $(document).ready(function(){
                $("#submit").click(function(){
                     //if invalid do nothing
                     if(!$("#formD").validationEngine('validate')){
                     return false;
                      } 
                    var vgrades = $("#grades").val();
                    var vsubject = $("#subject").val();

                    $.post("sendInitialApplication.php", 
                        {
                            grades : vgrades,
                            subject : vsubject
                        /*Handles response from server*/
                        function(response){
                            alert(response);
                        });
                    alert("You are here");
                });
            });

the PHP code "sendInitialApplication.php" is as follows

            <?php
                $method = $_SERVER['REQUEST_METHOD'];


                function connect(){
                    try{
                        $dbConn = new PDO('mysql:host=localhost; dbname=student', 'root', 'root');
                        $dbConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                        return $dbConn;
                    }
                    catch(PDOException $e){
                        echo $e->getMessage();
                    }
                }

                /*Checks if method is HTTP POST*/
                if(strtolower($method) == 'post'){

                    $grades = addslashes($_POST['grades']);
                    $subjects = addslashes($_POST['subject']);

                    try {

                        $dbHandler = connect();
                        $dbHandler->beginTransaction();

                        //Saving Various subjects with distinct grade obtained
                        foreach($subjects as $key => $subject)
                        {

                        $setIndexSubject = 'CALL sp_sendIndexSubject(:vSubjectID,:vGradeObtainedID)';
                        $stmt_subject = $dbHandler->prepare($setIndexSubject);
                        $stmt_subject->bindValue(':vSubjectID', $subject);
                        $stmt_subject->bindValue(':vGradeObtainedID', $grades[$key]);
                        $stmt_subject->execute();
                        $stmt_subject->closeCursor();
                        }

                        $dbHandler->commit();

                        echo "The Operation was Successful!!!!!!";

                    } catch (PDOException $e) {
                        $dbHandler->rollback();
                        die($e->getMessage());
                    }

                }else{
                    echo "Oops! Make sure Method is POST";
                }
            ?>
Tagged:
Sign In or Register to comment.