online quiz Question id not insert in to database - php

Hey Guys i am new here anyone help me ...my problem is that i am working on a online quiz exam with php and ajax ..problem is that i am inserting Question ID into database so anyone help me solve this problem ...i show you my codes..
<script>
$(document).ready(function(){
$('input[type="radio"]').click(function(){
var stu_ans=$(this).val();
var stu_id ="<?php echo $_GET['sid'];?>";
var q_id = $('#qname').val();
$.ajax({
url:"insert1.php",
method:"POST",
data:{stu_ans:stu_ans, stu_id:stu_id,q_id:q_id},
success:function(data){
$('#result').html(data);
}
});
});
});
</script>
<script>
$('.cont').addClass('hide');
$('#question_splitter_1').removeClass('hide');
$(document).on('click','.next',function(){
last=parseInt($(this).attr('id')); console.log( last );
nex=last+1;
$('#question_splitter_'+last).addClass('hide');
$('#question_splitter_'+nex).removeClass('hide');
});
$(document).on('click','.previous',function(){
last=parseInt($(this).attr('id'));
pre=last-1;
$('#question_splitter_'+last).addClass('hide');
$('#question_splitter_'+pre).removeClass('hide');
});
</script>
<style>
input[type="radio"] {
-ms-transform: scale(1.5); /* IE 9 */
-webkit-transform: scale(1.5); /* Chrome, Safari, Opera */
transform: scale(1.5);
}
</style>
<div class="container-fluied">
<div class="col-xs-12">
<hr>
<form role="form" id='formid' method="post" action="result.php?s_id=<?php echo $_GET['sid'];?>">
<?php
$number_question = 1;
$row = mysqli_query( $con, "select * from question where subject_id='$category' ORDER BY RAND()");
$rowcount = mysqli_num_rows( $row );
$remainder = $rowcount/$number_question;
$i = 0;
$j = 1; $k = 1;
?>
<h4>Total Question:-<?php echo $rowcount;?></h4>
<?php while ( $result = mysqli_fetch_assoc($row) ) {
if ( $i == 0) echo "<div class='cont' id='question_splitter_$j'>";?>
<div id='question<?php echo $k;?>' >
<p class='questions' id="qname<?php echo $j;?>" style="font-size:20px;font-weight:bold;color:green"> (<?php echo $k?>).<?php echo $result['que'];?></p>
<input type="hidden" value="<?php echo $result['que'];?>" name="qname<?php echo $j;?>" id="qname">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>(A)<?php echo trim($result['o1']);?></td>
<td colspan="5"><input type="radio" value="<?php echo $result['o1'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'></td>
</tr>
<tr>
<td>(B)<?php echo $result['o2'];?>
</td>
<td colspan="5"><input type="radio" value="<?php echo $result['o2'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/></td>
</tr>
<tr>
<td>(C)<?php echo $result['o3'];?></td>
<td colspan="5"><input type="radio" value="<?php echo $result['o3'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>
</td>
</tr>
<tr>
<td>(D)<?php echo $result['o4'];?></td>
<td colspan="5"><input type="radio" value="<?php echo $result['o4'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>
</td>
</tr>
</tbody>
</table>
</div>
<input type="radio" checked='checked' style='display:none' value="5" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>
<br/>
</div>
<?php
$i++;
if ( ( $remainder < 1 ) || ( $i == $number_question && $remainder == 1 ) ) {
echo "<button id='".$j."' class='next btn btn-success' type='submit'>Finish</button>";
echo "</div>";
} elseif ( $rowcount > $number_question ) {
if ( $j == 1 && $i == $number_question ) {
echo "<button id='".$j."' class='next btn btn-success' type='button'>Next</button>";
echo "</div>";
$i = 0;
$j++;
} elseif ( $k == $rowcount ) {
echo " <button id='".$j."' class='previous btn btn-success' type='button'>Previous</button>
<button id='".$j."' class='next btn btn-success' type='submit'>Finish</button>";
echo "</div>";
$i = 0;
$j++;
} elseif ( $j > 1 && $i == $number_question ) {
echo "<button id='".$j."' class='previous btn btn-success' type='button'>Previous</button>
<button id='".$j."' class='next btn btn-success' type='button' >Next</button>";
echo "</div>";
$i = 0;
$j++;
}
}
$k++;
} ?>
</form>
</div>
</div>
<?php
session_start();
include('db_connect.php');
include('db_connection.php');
$stu_id=$_SESSION['sid'];
echo $gender=$_POST['stu_ans'];
echo $que=$_POST['q_id'];
$q=mysqli_query($con,"SELECT * FROM question where id='$que'");
$ro=mysqli_fetch_array($q);
//$que=$ro['id'];
$date=date('d/m/y');
$right_ans=$ro['right_ans'];
////////////////////////Ajax Data Saved//////////////////////////////
$q1=mysqli_query($con,"SELECT * FROM student_ans where q_id='$que' AND stu_id='$stu_id'");
$ro1=mysqli_num_rows($q1);
if($ro1==''){
$sql = mysqli_query($con,"INSERT INTO student_ans(stu_ans,q_id,stu_id,date,right_ans) value('$gender','$que','$stu_id','$date','$right_ans')");
}else{
$sql1 = mysqli_query($con,"update student_ans set stu_ans='$gender' where q_id='$que' AND stu_id='$stu_id'");
}
?>
This code working like when i am click radio button so ajax fuction working and save the answer into database but i want that when inserting answer into database with answer insert Question ID

Related

how can i update multiple use php/ajax in datatable checkbox?

I have a datatable filled in by SALARIES with checkbox and button update_all and dropdownlist contains CHANTIER_IDs, I want to do multiple update of CHANTIER_IDs using php / ajax .CHANTIER_IDs of SALARIES cheked in datatable will be modified by value select in dropdownlist.
I want when I click on button update all the values of chantire_ids of cheked SALARIES will be modified by the value selected in dropdownlist
affectation.php
<div class="container" id="app">
<h3>affecter salarie a chantier</h3>
<div class="form-group col-md-3 ">
<select class="form-control" id="chantier">
<?php
include "../conn/connexion.php";
$query = "SELECT id,chantier FROM chantier";
$result = mysqli_query($con,$query);
$n = mysqli_num_rows($result);
for ($i=0; $i <$n ; $i++) {
$row = mysqli_fetch_array($result);
?>
<option value="<?php echo $row[0]; ?>" ><?php echo $row[1];
} ?></option>
</select>
</div>
<button class="btn btn-success update-all" data-url="">Update All</button>
<button style="margin: 5px;" class="btn btn-danger btn-xs delete-all" data-url="">Delete All</button>
<table class="table table-bordered">
<tr>
<th><input type="checkbox" id="check_all"></th>
<th>S.No.</th>
<th>nom & prenom</th>
<th>cin</th>
<th>matricule</th>
<th>chantier_ids</th>
</tr>
<?php
$query1 = "SELECT id,nom,prenom,cin,matricule,chantier_id
FROM salaries";
$result1 = mysqli_query($con,$query1);
$n1 = mysqli_num_rows($result1);
for ($i=0; $i <$n1 ; $i++) {
$row1 = mysqli_fetch_array($result1);
?>
<tr id="tr_<?php $row[0] ?>">
<td><input type="checkbox" class="checkbox" data-id="<?php $row[0] ?>"></td>
<td><?php echo $row1[0]; ?></td>
<td><?php echo $row1[1]; ?> <?php echo $row1[2]; ?></td>
<td><?php echo $row1[3]; ?></td>
<td><?php echo $row1[4]; ?></td>
<td><?php echo $row1[5]; ?></td>
</tr>
<?php
}
?>
</table>
</div>
javascript
<script type="text/javascript">
$(document).ready(function () {
$('#check_all').on('click', function(e) {
if($(this).is(':checked',true)) {
$(".checkbox").prop('checked', true);
} else {
$(".checkbox").prop('checked',false);
}
});
$('.checkbox').on('click',function(){
if($('.checkbox:checked').length == $('.checkbox').length){
$('#check_all').prop('checked',true);
}else{
$('#check_all').prop('checked',false);
}
});
$('.update-all').on('click', function(e) {
if(confirm("Are you sure you want to delete this?")){
var id = [];
}
$(':checkbox:checked').each(function(i){
id[i] = $(this).val();
});
if(id.length === 0) //tell you if the array is empty
{
alert("Please Select atleast one checkbox");
}else{
$.ajax({
url:'delete.php',
method:'POST',
data:{id:id},
success:function(){
for(var i=0; i<id.length; i++){
$(this).parents("tr")
.children("td:last-child")
.text($('#app')
.children("option:selected")
.text());
}
}
});
}
});
});
</script>
updateall.php
<?php
include "conn/connexion";
if(isset($_POST["id"])) {
foreach($_POST["id"] as $id) {
$query = "UPDATE salaries
SET chantier_id = ". $id ."
WHERE id = '".$id."'";
mysqli_query($con, $query);
}
}
?>

Insert data by AJAX checkbox value and radio button value in PHP

Everything is working, but I want to insert $result['id']; into the database with a radio button value.
JavaScript:
<script>
$(document).ready(function() {
$('input[type="radio"]').change(function() {
var stu_ans = $(this).val();
var stu_id = "<?php echo $_GET['sid'];?>";
var q_id = "<?php echo $result['id'];?>";
$.ajax({
url: "insert1.php",
method: "POST",
data: {
stu_ans: stu_ans,
stu_id: stu_id,
q_id: q_id
},
success: function(data) {
$('#result').html(data);
}
});
});
});
</script>
HTML:
<form role="form" id='formid' method="post" action="result.php?s_id=<?php echo $_GET['sid'];?>">
<?php
$number_question = 1;
$row = mysqli_query( $con, "select * from question where subject_id='$category' ORDER BY RAND()");
$rowcount = mysqli_num_rows( $row );
$remainder = $rowcount/$number_question;
$i = 0;
$j = 1; $k = 1;
?>
<h4>Total Question:-<?php echo $rowcount;?></h4>
<?php
while ( $result = mysqli_fetch_assoc($row) ) {
if ( $i == 0) echo "<div class='cont' id='question_splitter_$j'>";
?>
<div id='question<?php echo $k;?>' >
<p class='questions' id="qname<?php echo $j;?>" style="font-size:20px;font-weight:bold;color:green"> (<?php echo $k?>).<?php echo $result['que'];?></p>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<style>
input[type="radio"] {
-ms-transform: scale(1.5); /* IE 9 */
-webkit-transform: scale(1.5); /* Chrome, Safari, Opera */
transform: scale(1.5);
}
</style>
<tr>
<td>(A)<?php echo trim($result['o1']);?></td>
<td colspan="5"><input type="radio" value="<?php echo $result['o1'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'></td>
</tr>
<tr>
<td>(B)<?php echo $result['o2'];?>
</td>
<td colspan="5"><input type="radio" value="<?php echo $result['o2'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/></td>
</tr>
<tr>
<td>(C)<?php echo $result['o3'];?></td>
<td colspan="5"><input type="radio" value="<?php echo $result['o3'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>
</td>
</tr>
<tr>
<td>(D)<?php echo $result['o4'];?></td>
<td colspan="5"><input type="radio" value="<?php echo $result['o4'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>
</td>
</tr>
</tbody>
</table>
</div>
<input type="radio" checked='checked' style='display:none' value="5" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>
<br/>
</div>
<?php
$i++;
if ( ( $remainder < 1 ) || ( $i == $number_question && $remainder == 1 ) ) {
echo "<button id='".$j."' class='next btn btn-success' type='submit'>Finish</button>";
echo "</div>";
} elseif ( $rowcount > $number_question ) {
if ( $j == 1 && $i == $number_question ) {
echo "<button id='".$j."' class='next btn btn-success' type='button'>Next</button>";
echo "</div>";
$i = 0;
$j++;
} elseif ( $k == $rowcount ) {
echo " <button id='".$j."' class='previous btn btn-success' type='button'>Previous</button>
<button id='".$j."' class='next btn btn-success' type='submit'>Finish</button>";
echo "</div>";
$i = 0;
$j++;
} elseif ( $j > 1 && $i == $number_question ) {
echo "<button id='".$j."' class='previous btn btn-success' type='button'>Previous</button>
<button id='".$j."' class='next btn btn-success' type='button' >Next</button>";
echo "</div>";
$i = 0;
$j++;
}
}
$k++;
}
?>
</form>
living aside SQLinjections and security stuff in your code...
Change
var stu_ans=$(this).val();
for:
var stu_and =$("input[name='<name_of_radio>']:checked").val();
this is the way to take values from selected radio buttons.
and please!!! take a look around security issues.
hope this code help you!

Multiple Checkboxes Columns Updating Multiple SQL Rows

I've never had issues programming PHP SQL updates when updating multiple rows of data with multiple columns, but for some reason this section of coding is killing me as it is nothing more than checkboxes for the end user to check and/or uncheck. I've include portions of the code below as the full code is over 50+ columns wide of checkboxes and can range from 1-200 rows.
Everything displays/runs the way its supposed to but the updating isnt functioning properly ... example: if there is 5 rows (each with a unique trackingid) and I check the checkbox for the 4th row on update it says that the 1st checkbox was checked and NOT the 4th row.
<?php if (!isset($_POST['trackingid'])) { ?>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post" enctype="multipart/form-data" class="form-horizontal" id="form"><input type="hidden" name="id" value="<?php echo $id; ?>">
<table class="table table-striped">
<thead>
<tr>
<td class="rotate-alt"></td>
<td class="rotate-alt2"><div><span></span></div></td>
<td class="rotate-alt2"><div><span></span></div></td>
<td class="rotate-alt2"><div><span></span></div></td>
<td class="rotate-alt2"><div><span></span></div></td>
<td class="rotate-alt2"><div><span></span></div></td>
<td class="rotate-alt"><div><span>Glass</span></div></td>
<td class="rotate-alt"><div><span>Glass - SDL</span></div></td>
<td class="rotate-alt"><div><span>Mill - S&R</span></div></td>
</tr>
</thead>
<tbody>
<?php $result = sqlsrv_query($conn, "SELECT id, [Order Number], [Door Number], [Qty Display], [Interior or Exterior], [Style], Species_DD_Desc, [Mill Batch Nbr],
[Glass Completed], [Glass Completed By], [Glass STD Hrs], [SDL Completed], [SDL Completed By], [SDL STD Hrs],
[Mill Completed], [Mill Completed By], [Mill STD Hrs]
FROM PD_Tracking
WHERE [Order Number] = '$id'
ORDER BY [Door Number], [Qty Display], [Mill Batch Nbr]");
$i=0;
while ($list = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){ ?>
<tr>
<td class="center">
<input type="checkbox" onclick="$('input[id=\'selid<?php echo $i; ?>\']').attr('checked', this.checked);">
<input type="hidden" name="trackingid[]" value="<?php echo $list['id']; ?>">
</td>
<td class="center"><?php echo $list['Door Number']; ?></td>
<td class="center"><?php echo $list['Qty Display']; ?></td>
<td class="center"><?php echo $list['Interior or Exterior']; ?></td>
<td class="center"><?php echo $list['Style']; ?></td>
<td class="center"><?php echo $list['Species_DD_Desc']; ?></td>
<td class="center">
<?php if (($list['Glass STD Hrs']>0) && (!isset($list['Glass Completed']))){ ?>
<input type="checkbox" name="glass[]" id="selid<?php echo $i; ?>">
<?php } else if (($list['Glass STD Hrs']>0) && ($list['Glass Completed'] > ' ')){ ?>
<input type="checkbox" name="glass[]" checked="checked" id="selid<?php echo $i; ?>">
<?php } else { ?>
NR
<?php } ?>
</td>
<td class="center">
<?php if (($list['SDL STD Hrs']>0) && (!isset($list['SDL Completed']))){ ?>
<input type="checkbox" name="sdl[]" id="selid<?php echo $i; ?>">
<?php } else if (($list['SDL STD Hrs']>0) && ($list['SDL Completed'] > ' ')){ ?>
<input type="checkbox" name="sdl[]" checked="checked" id="selid<?php echo $i; ?>">
<?php } else { ?>
NR
<?php } ?>
</td>
<td class="center">
<?php if (($list['Mill STD Hrs']>0) && (!isset($list['Mill Completed']))){ ?>
<input type="checkbox" name="mill[]" id="selid<?php echo $i; ?>">
<?php } else if (($list['Mill STD Hrs']>0) && ($list['Mill Completed'] > ' ')){ ?>
<input type="checkbox" name="mill[]" checked="checked" id="selid<?php echo $i; ?>">
<?php } else { ?>
NR
<?php } ?>
</td>
</tr>
<?php ++$i;
} ?>
</tbody>
</table>
<?php } // END if (!isset($_POST['trackingid'])) {
if (isset($_POST['trackingid'])) {
$query = sqlsrv_query($conn, "SELECT * FROM users WHERE id=".$_SESSION['auid']."");
$row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC);
$employee_num = $row['Employee Number'];
$size = count($_POST['trackingid']);
for($i=0; $i < $size; $i++){
$trackingid = $_POST['trackingid'][$i];
$query = "UPDATE PD_Tracking SET ";
if(isset($_POST['glass'][$i])){
$query .= "[Glass Completed]='$date_time_entered', [Glass Completed By]='$employee_num', ";
} else {
$query .= "[Glass Completed]= NULL, [Glass Completed By]= NULL, ";
}
if(isset($_POST['sdl'][$i])){
$query .= "[SDL Completed]='$date_time_entered', [SDL Completed By]='$employee_num', ";
} else {
$query .= "[SDL Completed]= NULL, [SDL Completed By]= NULL, ";
}
if(isset($_POST['mill'][$i])){
$query .= "[Mill Completed]='$date_time_entered', [Mill Completed By]='$employee_num' ";
} else {
$query .= "[Mill Completed]= NULL, [Mill Completed By]= NULL ";
}
$query .= "WHERE id='$trackingid'";
$query = sqlsrv_query($conn, $query);
} // END for($i=0; $i < $size; $i++){?>
<SCRIPT LANGUAGE='JavaScript'>
<!-- Begin
window.location = '<?php echo $_SERVER['REQUEST_URI']; ?>&success=edit';
// End -->
</script>
<?php } // END if (isset($_POST['trackingid'])) { ?>

Ajax Mysql PHP How to show $_SESSION arrays result into DIV

My friends,
I´m facing some hard trouble with Ajax & PHP integration. I have a simple code as below that returns the data from database and shows into div class="item-list". When the user puts the quantity and clicks on submit button, the selected item is uploaded into div class="returned", like the "add item to cart" function. The code is working as well (add & remove from list), but only with refresh on page.
I saw many examples on web and I tried to adapt them to my code (today is my third day of fighting), but with no success. Could you please show to me a way to load the selected items into div class="returned" without refresh on page?
Best regards & thanks a lot!
<?php
session_start();
if(isset($_POST["add_to_table"])){
if(isset($_SESSION["dynamic_list"])){
$item_array_id = array_column($_SESSION["dynamic_list"], "item_id");
if(!in_array($_GET["idproduct"], $item_array_id)){
$count = count($_SESSION["dynamic_list"]);
$item_array = array(
'item_id' => $_GET["idproduct"],
'model' => $_POST["got_model"],
'price' => $_POST["got_price"],
'item_quantity' => $_POST["quantity"]
);
$_SESSION["dynamic_list"][$count] = $item_array;
}else{
echo'<script>alert("Item added!")</script>';
echo'<script>window.location="intransit.php"</script>';
}
}else{
$item_array = array(
'item_id' => $_GET["idproduct"],
'model' => $_POST["got_model"],
'price' => $_POST["got_price"],
'item_quantity' => $_POST["quantity"]
);
$_SESSION["dynamic_list"][0] = $item_array;
}
}
if(isset($_GET["action"])){
if($_GET["action"] == "delete"){
foreach($_SESSION["dynamic_list"] as $list => $values){
if($values["item_id"] == $_GET["idproduct"]){
unset($_SESSION["dynamic_list"][$list]);
echo'<script>alert("Item removed!")</script>';
echo'<script>window.location="intransit.php"</script>';
}
}
}
}
?>
<?php include"header.php" ?>
<div id="main_box">
<div id="moviment">
<div class="item_list">
<?php
$query = "SELECT * FROM products ORDER BY model ASC LIMIT 3";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
?>
<div id="product_table">
<form method="post" action="intransit.php?action=add&idproduct=<?php echo $row["idproduct"]; ?>">
<span><img src="images/<?php echo $row["image"]; ?>" alt="motor" width="80" height="80" /></span>
<span>Model:&nbsp<?php echo $row["model"]; ?></span>
<span>Price:&nbsp<?php echo $row["unitprice"]; ?></span>
<input type="text" name="quantity" value="1" class="qty" />
<input type="hidden" name="got_model" value="<?php echo $row["model"]; ?>" />
<input type="hidden" name="got_price" value="<?php echo $row["unitprice"]; ?>" />
<input type="submit" name="add_to_table" value="Add Item" />
</form>
</div><!--end "product_table"-->
<?php
}
}
?>
</div><!--end "item_list"-->
<div class="returned">
<table>
<tr>
<th>Model</th>
<th>Qty</th>
<th>Price</th>
<th>Total</th>
<th>Action</th>
</tr>
<?php
if(!empty($_SESSION["dynamic_list"])){
$total = 0;
foreach($_SESSION["dynamic_list"] as $list => $values){
?>
<tr>
<td><?php echo $values["model"]; ?></td>
<td><?php echo $values["item_quantity"]; ?></td>
<td>$<?php echo $values["price"]; ?></td>
<td><?php echo number_format($values["item_quantity"] * $values["price"], 2); ?></td>
<td>Remove</td>
</tr>
<?php
$total = $total + ($values["item_quantity"] * $values["price"]);
}
?>
<tr>
<td colspan="3" align="right">Total</td>
<td align="right">$ <?php echo number_format($total, 2);?></td>
<td></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</div>
<?php include"footer.php" ?>
I tried to make example less complicated and to make it work with code you already created.
if (isset($_POST['got_model'])) {
$_SESSION["dynamic_list"][$_GET["idproduct"]] = array(
'item_id' => $_GET["idproduct"],
'model' => $_POST["got_model"],
'price' => $_POST["got_price"],
'item_quantity' => $_POST["quantity"]
);
return show_dynamic_list();
}
if (isset($_POST["action"]) && $_POST["action"] == "delete") {
unset($_SESSION["dynamic_list"][$_POST["idproduct"]]);
return show_dynamic_list();
}
function show_dynamic_list(){
?>
<table>
<tr><th>Model</th><th>Qty</th><th>Price</th><th>Total</th><th>Action</th></tr>
<?php
$total = 0;
foreach ($_SESSION["dynamic_list"] as $list => $values) {
?>
<tr>
<td><?php echo $values["model"]; ?></td>
<td><?php echo $values["item_quantity"]; ?></td>
<td>$<?php echo $values["price"]; ?></td>
<td><?php echo number_format($values["item_quantity"] * $values["price"], 2); ?></td>
<td>
<form action="" method="post" class="delete_form">
<input type="hidden" name="idproduct" value="<?php echo $values["item_id"]; ?>"/>
<input type="hidden" name="action" value="delete"/>
<input type="submit" value="Remove">
</form>
</td>
</tr>
<?php
$total = $total + ($values["item_quantity"] * $values["price"]);
}
?>
<tr>
<td colspan="3" align="right">Total</td>
<td align="right">$ <?php echo number_format($total, 2); ?></td>
<td></td>
</tr>
</table>
<?php
}
include"header.php" ?>
<div id="main_box">
<div id="moviment">
<div class="item_list">
<?php
$query = "SELECT * FROM products ORDER BY model ASC LIMIT 3";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
?>
<div id="product_table_<?php echo $row["idproduct"]; ?>">
<form method="post" action="?action=add&idproduct=<?php echo $row["idproduct"]; ?>">
<span>Model:&nbsp<?php echo $row["model"]; ?></span>
<span>Price:&nbsp<?php echo $row["unitprice"]; ?></span>
<input type="text" name="quantity" value="1" class="qty"/>
<input type="hidden" name="got_model" value="<?php echo $row["model"]; ?>"/>
<input type="hidden" name="got_price" value="<?php echo $row["unitprice"]; ?>"/>
<input type="submit" name="add_to_table" value="Add Item"/>
</form>
</div><<!--end "product_table"-->
<?php
}
}
?>
</div><!--end "item_list"-->
<div class="returned">
<?php show_dynamic_list(); ?>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>
<script type="text/javascript">
$(function () {
$('body').on('submit','#moviment form,.delete_form',function (event) {
event.preventDefault(); // Prevent the form from submitting via the browser
var form = $(this);
$.ajax({
type:'post',
url: form.attr('action'),
data: form.serialize(),
dataType:'html'
}).done(function (data) {
$('.returned').html(data);
}).fail(function (data) {
// error
});
});
});
</script>
<?php include"footer.php";

I'm not getting values from session variable set for results

I have created the following pages.
1. questions.php
2. functions.php
3. result.php
i have set the values of session variables on page function.php when the question form on questions.php form is submitted the functions.php code runs and the i have echo the values of session variables on result.php page. but i'm unable to get the session values result.the sessions values are resulting empty .anyone help please.
functions.php
if(isset($_POST['question_form']))
{
global $conn;
if (!isset($_SESSION['result'])) {
$result = 0;
$_SESSION['result'] = 0;
}
if (!isset($_SESSION['attempted'])) {
$attempted = 0;
$_SESSION['attempted'] = 0;
}
$resArray = array();
$resArray['message'] = '';
$resArray['status'] = '';
$no = $_POST['no'];
$postedAnswer = $_POST['answer_'.$no];
$question_id = $_POST['question_id'];
$subject_id = $_POST['subject_id'];
$sql = "SELECT True_answer FROM question WHERE QuestionId = '$question_id' AND SubjectId = '$subject_id'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$True_answer = $row['True_answer'];
if($postedAnswer === $True_answer)
{
$result = $_SESSION['result'];
$result++;
$_SESSION['result'] = $result;
}
$attempted = $_SESSION['attempted'];
$attempted++;
$_SESSION['attempted'] = $attempted;
$resArray['status'] = true;
//$resArray['q_id'] = $no;
$resArray['message'] = 'Submitted Successfully';
echo json_encode($resArray);
exit();
}
=====questions.php========
<form action="includes/functions.php" id="question_form_<?php echo $j; ?>" >
<div class='container' >
<div class='row'>
<div class='col-lg-12'>
<div class='thumbnail'>
<p id="question_description">Q.<?php echo $i; ?><br><?php echo $row['QuestionDescription']; ?></p>
<div class="questions_options">
<label><input type="radio" id="btn_radio" name="answer_<?php echo $j; ?>" value="<?php echo $row['Option1']; ?>"><?php echo $row['Option1']; ?></input></label><br>
<label><input type="radio" id="btn_radio" name="answer_<?php echo $j; ?>" value="<?php echo $row['Option2']; ?>"><?php echo $row['Option2']; ?></input></label><br>
<label><input type="radio" id="btn_radio" name="answer_<?php echo $j; ?>" value="<?php echo $row['Option3']; ?>"><?php echo $row['Option3']; ?></input></label><br>
<label><input type="radio" id="btn_radio" name="answer_<?php echo $j; ?>" value="<?php echo $row['Option4']; ?>"><?php echo $row['Option4']; ?></input></label><br><br>
<input type="hidden" name="question_id" value="<?php echo $row['QuestionId'] ?>">
<input type="hidden" name="subject_id" value="<?php echo $row['SubjectId'] ?>">
<input type="hidden" name="question_form" value="question_form">
<input type="hidden" name="no" value="<?php echo $j; ?>">
<button class="btn btn-primary btn-sm" id="btn_submit">Submit<i class="glyphicon glyphicon-arrow-right" >
</i></button>
</div>
</div>
</div>
</div>
</div>
</form>
============result.php===============
<table class="table table-striped table-hover" id="result_table" >
<thead>
<tr>
<th style="text-align: center;">Total Questions</th>
<th style="text-align: center;">Attempted Questions</th>
<th style="text-align: center;">Total Marks</th>
<th style="text-align: center;">Obtained Marks</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center;"><?php echo $number_of_questions;?></td>
<td style="text-align: center;"><?php echo $_SESSION['attempted']; ?></td>
<td style="text-align: center;"><?php if(!isset($_SESSION['result']))
{
echo "empty";
}
else
{
echo $_SESSION['result'];
}
?></td>
<td style="text-align: center;"><?php echo $marks_obtained;?></td>
</tr>
</tbody>
</table>
Please start the session using session_start() function

Categories