I want to submit the values of marks, grade, subject, and name of different students in PHP. While adding the marks the average and grade get automatically calculated in front end using jQuery of each student. But I am having trouble submitting array values through PHP. I tried several options but none of them worked.
<?php
$con=mysqli_connect('localhost','root','','student');
if($con){
echo "connected";
}
else{
echo "not connected";
}
if(isset($_POST['submit'])){
$stack = array();
$name=$_POST['name'];
$roll=$_POST['roll'];
$class=$_POST['class'];
$phy=$_POST['phy'];
$eng=$_POST['eng'];
$maths=$_POST['maths'];
$average=$_POST['average'];
$grade=$_POST['grade'];
array_push($stack, $name);
array_push($stack, $roll);
array_push($stack, $class);
array_push($stack, $phy);
array_push($stack, $eng);
array_push($stack, $maths);
array_push($stack, $average);
array_push($stack, $grade);
;
for ($i=0; $i < sizeof($stack); $i++) {
foreach ($stack[$i] as $key => $value) {
$query="INSERT INTO student_detail (id,name,roll,class,phy,eng,maths,avg,grade) VALUES('','$name[$value]','$roll[$value]','$class[$value]','$phy[$value]','$eng[$value]','$maths[$value]','$average[$value]','$grade[$value]')";
}
}
// print_r($stack);
$result=mysqli_query($con,$query);
if($result){
echo "data inserted";
}
else{
echo "data not".mysqli_error($con);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Registration</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<!-- jQuery library -->
<script>
$(document).ready(function() {
var s = 0;
$('.marks').keyup(function() {
var idx = $(this).attr('id').split('_')[1];
$(".active .score_" + idx).each(function() {
var x = $(this).val();
if (x) {
s += Number(x);
} else {
s += 0;
}
});
// alert(s);
// alert(s/3);
$('#average_' + idx).val(s / 3);
$('#grade_' + idx).val(grade(s / 3));
s = 0;
});
function grade(average) {
if (average > 90) {
return 'A';
} else if (average < 90 && average > 81) {
return 'B';
} else {
return 'C';
}
}
});
</script>
</head>
<body>
<div>
<form action="index.php" method="POST">
<div class="table-responsive">
<table class="table table-condensed table-striped">
<tr>
<th>Name</th>
<th>Roll</th>
<th>Class</th>
<th>Phy</th>
<th>Eng</th>
<th>Maths</th>
<th>Average</th>
<th>Grade</th>
</tr>
<?php for($i=1;$i<=2;$i++){ ?>
<tr class="active">
<td><input type="text" name="name[]" placeholder="Name"></td>
<td><input type="text" name="roll[]" placeholder="Roll"></td>
<td><input type="text" name="class[]" placeholder="Class"></td>
<td><input type="number" name="phy[]" placeholder="Physics" class="marks score_<?php echo ($i);?>" id="marks_<?php echo ($i);?>"></td>
<td><input type="number" name="eng[]" placeholder="Eng" class="marks score_<?php echo ($i);?>" id="marks_<?php echo ($i);?>"></td>
<td><input type="number" name="maths[]" placeholder="Maths" class="marks score_<?php echo ($i);?>" id="marks_<?php echo ($i);?>"></td>
<td><input type="text" name="average[]" placeholder="average" id="average_<?php echo ($i);?>" class="average"></td>
<td><input type="text" name="grade[]" placeholder="Grade" id="grade_<?php echo ($i);?>" class="grade"></td>
</tr>
<?php } ?>
</table>
</div>
<input class="btn btn-primary" type="submit" name="submit" value="Submit">
</form>
</div>
</body>
</html>
After editing
<?php
$con=mysqli_connect('localhost','root','','student');
if($con){
echo "connected";
}
else{
echo "not connected";
}
if(isset($_POST['submit'])){
$stack = array();
$name=$_POST['name'];
$roll=$_POST['roll'];
$class=$_POST['class'];
$phy=$_POST['phy'];
$eng=$_POST['eng'];
$maths=$_POST['maths'];
$average=$_POST['average'];
$grade=$_POST['grade'];
array_push($stack, $name);
array_push($stack, $roll);
array_push($stack, $class);
array_push($stack, $phy);
array_push($stack, $eng);
array_push($stack, $maths);
array_push($stack, $average);
array_push($stack, $grade);
;
// for ($i=0; $i < sizeof($stack); $i++) {
// foreach ($stack[$i] as $value) {
// // $query="INSERT INTO student_detail (id,name,roll,class,phy,eng,maths,avg,grade) VALUES('','$name[$value]','$roll[$value]','$class[$value]','$phy[$value]','$eng[$value]','$maths[$value]','$average[$value]','$grade[$value]')";
// // var_dump($stack);
// }
// }
var_dump($stack);
// print_r($stack);
// $result=mysqli_query($con,$query);
// if($result){
// echo "data inserted";
// }
// else{
// echo "data not".mysqli_error($con);
// }
}