473,749 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

posting dynamic arrays as distinct variables using java script

2 New Member
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 "sendInitialApp lication.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 1396
Dormilich
8,658 Recognized Expert Moderator Expert
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
1507
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, Hubatka
3
10264
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
24190
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 mouseout are working perfect but only the source is not setting using java script. Please clarify me regarding this. PS: This problem is only for safari browser. <tr>
8
2391
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 page will be viewed using Microsoft's IE browser (version 6.x ....). Webapge is self-contained. i.e., it does not refer to any links outside the webpage, however, it uses bookmarks inside the same page. I have a long table to display, table has...
4
3624
by: gsuns82 | last post by:
hi friends,can any one give the coding for finding file size using java script???
1
3610
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 language="javascript"> var len = 0; var i = 0; var t; function printAll(){ len = document.frames.length; for(var i=0;i<len;i++){ frames.focus();
7
2146
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"; document.myform.submit(); } <select name="xyz" onchange=func()>
6
2805
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
2354
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 possible ? and those changed images should be work in all pages. so help me ???? bye
0
8996
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9566
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.