posting dynamic arrays as distinct variables using java script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mountain6464
    New Member
    • Apr 2015
    • 2

    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;

    Code:
    				<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;

    Code:
    $(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 "sendInitialApp lication.php" is as follows
    Code:
    				<?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";
    					}
    				?>
    Last edited by Dormilich; Apr 19 '15, 12:18 PM. Reason: Please use the code tags when posing code.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    there’s a missing comma in your JS code, which should prevent it from executing at all …

    Notice that in the form data am not puting id="subject" to avoid picking the id for only one subject,
    due to that you’re not passing any data to the ajax call. it might be that jQuery in that case removes the fields completely, which would explain the error.

    Comment

    Working...