Click here to Skip to main content
16,015,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;


Academic Qualification


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)
{
?>
:<input name="subject[]" type="hidden" value="" />
<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=""> </option>

</select>

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

$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";
}
?>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900