473,386 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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;

Expand|Select|Wrap|Line Numbers
  1.                 <tr> <td colspan="6"><div align="center"><strong> <p style="color:#930">Academic Qualification</p> </strong></div></td> </tr> <?php
  2.                          require_once("connection/connectPDO.php");
  3.                          $sql="CALL sp_getSubjects()"; 
  4.  
  5.                                     //Initiate and Call Stored Procedure Using PDO
  6.                                     $pdo = new PDOConfig();
  7.                                     $resultsSubject = $pdo->query($sql);
  8.                                     foreach($resultsSubject as $rowSubject)
  9.                                             {
  10.                         ?> <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
  11.  
  12.                         $sql="CALL sp_grabGrades()"; 
  13.  
  14.                                     //Initiate and Call Stored Procedure Using PDO
  15.                                     $pdo = new PDOConfig();
  16.                                     $resultset = $pdo->query($sql);
  17.                                     foreach($resultset as $row)
  18.                                             {
  19.  
  20.                         ?> <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;

Expand|Select|Wrap|Line Numbers
  1. $(document).ready(function(){
  2.                     $("#submit").click(function(){
  3.                          //if invalid do nothing
  4.                          if(!$("#formD").validationEngine('validate')){
  5.                          return false;
  6.                           }    
  7.                         var vgrades = $("#grades").val();
  8.                         var vsubject = $("#subject").val();
  9.  
  10.                         $.post("sendInitialApplication.php", 
  11.                             {
  12.                                 grades : vgrades,
  13.                                 subject : vsubject
  14.                             /*Handles response from server*/
  15.                             function(response){
  16.                                 alert(response);
  17.                             });
  18.                         alert("You are here");
  19.                     });
  20.                 });
the PHP code "sendInitialApplication.php" is as follows
Expand|Select|Wrap|Line Numbers
  1.                 <?php
  2.                     $method = $_SERVER['REQUEST_METHOD'];
  3.  
  4.  
  5.                     function connect(){
  6.                         try{
  7.                             $dbConn = new PDO('mysql:host=localhost; dbname=student', 'root', 'root');
  8.                             $dbConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  9.                             return $dbConn;
  10.                         }
  11.                         catch(PDOException $e){
  12.                             echo $e->getMessage();
  13.                         }
  14.                     }
  15.  
  16.                     /*Checks if method is HTTP POST*/
  17.                     if(strtolower($method) == 'post'){
  18.  
  19.                         $grades = addslashes($_POST['grades']);
  20.                         $subjects = addslashes($_POST['subject']);
  21.  
  22.                         try {
  23.  
  24.                             $dbHandler = connect();
  25.                             $dbHandler->beginTransaction();
  26.  
  27.                             //Saving Various subjects with distinct grade obtained
  28.                             foreach($subjects as $key => $subject)
  29.                             {
  30.  
  31.                             $setIndexSubject = 'CALL sp_sendIndexSubject(:vSubjectID,:vGradeObtainedID)';
  32.                             $stmt_subject = $dbHandler->prepare($setIndexSubject);
  33.                             $stmt_subject->bindValue(':vSubjectID', $subject);
  34.                             $stmt_subject->bindValue(':vGradeObtainedID', $grades[$key]);
  35.                             $stmt_subject->execute();
  36.                             $stmt_subject->closeCursor();
  37.                             }
  38.  
  39.                             $dbHandler->commit();
  40.  
  41.                             echo "The Operation was Successful!!!!!!";
  42.  
  43.                         } catch (PDOException $e) {
  44.                             $dbHandler->rollback();
  45.                             die($e->getMessage());
  46.                         }
  47.  
  48.                     }else{
  49.                         echo "Oops! Make sure Method is POST";
  50.                     }
  51.                 ?>
Apr 18 '15 #1
1 1374
Dormilich
8,658 Expert Mod 8TB
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.
Apr 19 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Alen Hubtka | last post by:
Hello All, Can any one share the code to compare two date data using Java script?Please. I would like to compare two dates and findout there are greater than or less than each other. Thanks,...
3
by: Srinu | last post by:
How do i hide status bar in firefox using java script only Plz help me iam new to this group.
6
by: abdullah1983 | last post by:
Hi Guys, I need some clarification regarding the problem with safari browser. Please find my code below. I'm setting the image src, mouseover and mouseout using javascript. The mouseover and...
8
by: dbaplusplus | last post by:
I worked on web development using java script many many years, so I am now a newbie to javascript. I have a single html page, which is generated dynamically using some programming language. Web...
4
by: gsuns82 | last post by:
hi friends,can any one give the coding for finding file size using java script???
1
by: harshavardhan | last post by:
Hi, here is the problem: Consider there are three frames in a HTML page. When I try to print these frames using java script, only the third document is being printed thrice. <HTML><HEAD> <script...
7
by: kamalaswaminathan | last post by:
Hi all, I want to pass the value of the select box to another form using java script. my code looks like this.. <script> function func { document.myform.action = "abc.cgi";...
6
by: DhananjayPatki | last post by:
Hello, I wnat the code , how to disable the Close(x) icon script using java script . is there any event that can be used to prompt the user before closing the new window.
2
by: shashank narayan | last post by:
i m using a master page in my project and that page contains an image tag . i want to change images in that image tag by using java script after fixed time interval. my technology is asp.net. is it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.