posting dynamic arrays as distinct variables using java script

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;

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

                     $sql="CALL sp_getSubjects()"; 

                                //Initiate and Call Stored Procedure Using PDO
                                $pdo = new PDOConfig();
                                $resultsSubject = $pdo->query($sql);
                                foreach($resultsSubject as $rowSubject)
                        <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>

                    $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 } ?>
                                 <?php } ?>

the form looks like this

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

the java script code is as follows;

                     //if invalid do nothing
                     return false;
                    var vgrades = $("#grades").val();
                    var vsubject = $("#subject").val();

                            grades : vgrades,
                            subject : vsubject
                        /*Handles response from server*/
                    alert("You are here");

the PHP code "sendInitialApplication.php" is as follows

                $method = $_SERVER['REQUEST_METHOD'];

                function connect(){
                        $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();

                        //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]);


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

                    } catch (PDOException $e) {

                    echo "Oops! Make sure Method is POST";
Sign In or Register to comment.

Howdy, Stranger!

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