I have a multiple column table with one of the columns being a checkbox. Whenever a checkbox is checked, it displays an extra column called Quantity # which contains a spinner where the user can either type in a number or use the up/down arrows to select a number. Currently, if you use the up/down arrows, it can go no lower than 1 and no higher than the quantity number associated with that row in the table.
However, for example, if the quantity is 80 and the user types in 85 themselves, then that will also be accepted. I do not want this accepted as you can not request 85 when there is only 80 quantity available.
So how can I provide validation to the spinner so that a user can not enter a number higher than the quantity number? Also, is there a way to make it so that the spinner will not allow any alphabetical characters and will only allow numerical characters?
HTML:
<section id="checkout-btn">
<button id="checkout" name="order" onclick="sendMail(); return false">Checkout</button>
</section>
<br>
<table id="merchTable" cellspacing="5" class="sortable">
<thead>
<tr class="ui-widget-header">
<th class="sorttable_nosort"></th>
<th class="sorttable_nosort">Loc</th>
<th class="merchRow">Report Code</th>
<th class="merchRow">SKU</th>
<th class="merchRow">Special ID</th>
<th class="merchRow">Description</th>
<th class="merchRow">Quantity</th>
<th class="sorttable_nosort">Unit</th>
<th style="display: none;" class="num">Quantity #</th>
</tr>
</thead>
<tbody>
<?php foreach ($dbh->query($query) as $row) {?>
<tr>
<td class="ui-widget-content"><input type="checkbox" class="check" name="check" id="checkid-<?php echo intval ($row['ID'])?>"></td>
<td class="loc ui-widget-content" data-loc="<?php echo $row['Loc'] ?>"><input type="hidden"><?php echo $row['Loc'];?></td>
<td class="rp-code ui-widget-content" align="center" data-rp-code="<?php echo $row['Rp-Code'] ?>" id="rp-code-<?php echo intval ($row['Rp-Code'])?>"><?php echo $row['Rp-Code'];?></td>
<td class="sku ui-widget-content" data-sku="<?php echo $row['SKU'] ?>" id="sku-<?php echo intval ($row['SKU'])?>"><?php echo $row['SKU'];?></td>
<td class="special-id ui-widget-content" data-special-id="<?php echo $row['Special-ID'] ?>" align="center" id="special-id-<?php echo intval ($row['Special-ID'])?>"><?php echo $row['Special-ID'];?></td>
<td class="description ui-widget-content" data-description="<?php echo htmlspecialchars($row['Description']) ?>" id="description-<?php echo intval ($row['Description'])?>"><?php echo $row['Description'];?></td>
<td class="quantity ui-widget-content" data-quantity="<?php echo $row['Quantity'] ?>" align="center" id="quantity-<?php echo intval ($row['Quantity'])?>"><?php echo $row['Quantity'];?></td>
<td class="unit ui-widget-content" data-unit="<?php echo $row['Unit'] ?>" id="unit-<?php echo intval ($row['Unit'])?>"><?php echo $row['Unit'];?></td>
<td style="display: none;" class="quantity_num ui-widget-content"><input type="textbox" style="width: 100px;" class="spinner" id="spin-<?php echo intval ($row['ID'])?>"></td>
</tr>
<?php } ?>
</tbody>
</table>
JavaScript for spinner:
$(function () {
$(".check").change(function(){
$(this).closest('tr').find('td.quantity_num').toggle(this.checked);
console.log($('input.check').is(':checked'));
var quantity = $(this).closest('tr').find('td.quantity').data('quantity');
console.log(quantity);
if($('input.check').is(':checked'))
$(this).closest('table').find('th.num').toggle(true);
else
$(this).closest('table').find('th.num').toggle(false);
$(this).closest("tr").find(".spinner" ).spinner({
spin: function( event, ui ) {
if ( ui.value > quantity ) {
$( this ).spinner( "value", quantity );
return false;
} else if ( ui.value <= 1 ) {
$( this ).spinner( "value", 1 );
return false;
}
var test = ui.value;
sessionStorage.setItem("test", JSON.stringify(test));
var testtrue = sessionStorage.getItem("test");
console.log(JSON.parse(testtrue));
}
});
});
});
JavaScript that gets info from a checked row and displays it in my email client once the "Checkout" button is clicked:
function sendMail() {
var dataItems = [
{ 'clss':'.loc', 'prop':'loc' },
{ 'clss':'.rp-code', 'prop':'rpCode' },
{ 'clss':'.sku', 'prop':'sku' },
{ 'clss':'.special-id', 'prop':'specialId' },
{ 'clss':'.description', 'prop':'description' },
{ 'clss':'.quantity', 'prop':'quantity' },
{ 'clss':'.unit', 'prop':'unit' }
];
var link = "mailto:me#example.com" + "?subject=" + encodeURIComponent("Order") + "&body=";
link += $('#merchTable tr input[name="check"]:checked').closest('tr').get().map(function(tr) {
var str = dataItems.map(function(item) {
return encodeURIComponent($(tr).find(item.clss).data(item.prop)) + '\xa0\xa0';
}).join('');
str += encodeURIComponent($(tr).find('.spinner').spinner('value')) + '%0D%0A';
return str;
}).join('') + '%0D%0A';
console.log(link);
window.location.href = link;
}
Related
I have a multiple column table with one of the columns being a checkbox. I can check different rows and then hit a "Checkout" button and it will display the corresponding selected table row data in the body of an email.
However, I noticed that some of my data contains quotation marks " so the data displayed in the email is only the data before the quotes. So for example, if I had the entry 76" bedframe the only data that would be displayed in the email would be 76.
How can I fix this?
Here is my javascript code for getting all of the information based on which checkbox is checked. I am mainly concerned about the quotes in the Description column:
function sendMail() {
var link = "mailto:me#example.com"
+ "?subject=" + encodeURIComponent("Order")
+ "&body=";
var body = '';
$('table tr input[name="check"]:checked').each(function(){
var current_tr = $(this).parent().parent();
var data = current_tr.find('.loc').data('loc');
body += encodeURIComponent(data) + '\xa0\xa0';
var data =current_tr.find('.rp-code').data('rp-code');
body += encodeURIComponent(data) + '\xa0\xa0';
var data =current_tr.find('.sku').data('sku');
body += encodeURIComponent(data) + '\xa0\xa0';
var data =current_tr.find('.special-id').data('special-id');
body += encodeURIComponent(data) + '\xa0\xa0';
var data =current_tr.find('.description').data('description');
body += encodeURIComponent(data) + '\xa0\xa0';
var data =current_tr.find('.quantity').data('quantity');
body += encodeURIComponent(data) + '\xa0\xa0';
var data =current_tr.find('.unit').data('unit');
body += encodeURIComponent(data) + '\xa0\xa0';
var data =current_tr.find('.spinner').data('spinner');
body += encodeURIComponent(data) + '\xa0\xa0';
body += '%0D%0A';
});
body += '';
link += body;
console.log(link);
window.location.href = link;
}
HTML:
<section id="checkout-btn">
<button id="checkout" name="order" onclick="sendMail(); return false">Checkout</button>
</section>
<br>
<table id="merchTable" cellspacing="5" class="sortable">
<thead>
<tr class="ui-widget-header">
<th class="sorttable_nosort"></th>
<th class="sorttable_nosort">Loc</th>
<th class="merchRow">Report Code</th>
<th class="merchRow">SKU</th>
<th class="merchRow">Special ID</th>
<th class="merchRow">Description</th>
<th class="merchRow">Quantity</th>
<th class="sorttable_nosort">Unit</th>
<th style="display: none;" class="num">Quantity #</th>
</tr>
</thead>
<tbody>
<?php foreach ($dbh->query($query) as $row) {?>
<tr>
<td class="ui-widget-content"><input type="checkbox" class="check" name="check" id="checkid-<?php echo intval ($row['ID'])?>"></td>
<td class="loc ui-widget-content" data-loc="<?php echo $row['Loc'] ?>"><input type="hidden"><?php echo $row['Loc'];?></td>
<td name="rows[0][0][rp-code]" class="rp-code ui-widget-content" align="center" data-rp-code="<?php echo $row['Rp-Code'] ?>" id="rp-code-<?php echo intval ($row['Rp-Code'])?>"><?php echo $row['Rp-Code'];?></td>
<td name="rows[0][0][sku]" class="sku ui-widget-content" data-sku="<?php echo $row['SKU'] ?>" id="sku-<?php echo intval ($row['SKU'])?>"><?php echo $row['SKU'];?></td>
<td name="rows[0][0][special-id]" class="special-id ui-widget-content" data-special-id="<?php echo $row['Special-ID'] ?>" align="center" id="special-id-<?php echo intval ($row['Special-ID'])?>"><?php echo $row['Special-ID'];?></td>
<td name="rows[0][0][description]" class="description ui-widget-content" data-description="<?php echo $row['Description'] ?>" id="description-<?php echo intval ($row['Description'])?>"><?php echo $row['Description'];?></td>
<td name="rows[0][0][quantity]" class="quantity ui-widget-content" data-quantity="<?php echo $row['Quantity'] ?>" align="center" id="quantity-<?php echo intval ($row['Quantity'])?>"><?php echo $row['Quantity'];?></td>
<td name="rows[0][0][unit]" class="unit ui-widget-content" data-unit="<?php echo $row['Unit'] ?>" id="unit-<?php echo intval ($row['Unit'])?>"><?php echo $row['Unit'];?></td>
<td name="rows[0][0][quant]" style="display: none;" class="quantity_num ui-widget-content"><input type="textbox" style="width: 100px;" class="spinner" id="spin-<?php echo intval ($row['ID'])?>"></td>
</tr>
<?php } ?>
</tbody>
</table>
I have a table where each row can be selected by checking a checkbox. Whatever rows are selected, I am wanting those rows to be pasted to the body of an email that pops up when the "Checkout" button is pressed.
Currently, whenever I hit the "Checkout" button, the email pops up and it will display the first row in the table, whether selected or not, but nothing more.
How can I get multiple selections displayed based on if it is checked?
Table:
<section id="checkout-btn">
<button id="checkout" name="order" onclick="sendMail(); return false">Checkout</button>
</section>
<br>
<table id="merchTable" cellspacing="5" class="sortable">
<thead>
<tr class="ui-widget-header">
<th class="sorttable_nosort"></th>
<th class="sorttable_nosort">Loc</th>
<th class="merchRow">Report Code</th>
<th class="merchRow">SKU</th>
<th class="merchRow">Special ID</th>
<th class="merchRow">Description</th>
<th class="merchRow">Quantity</th>
<th class="sorttable_nosort">Unit</th>
<th style="display: none;" class="num">Quantity #</th>
</tr>
</thead>
<tbody>
<?php foreach ($dbh->query($query) as $row) {?>
<tr>
<td class="ui-widget-content"><input type="checkbox" class="check" name="check" id="checkid-<?php echo intval ($row['ID'])?>"></td>
<td class="loc ui-widget-content" data-loc="<?php echo $row['Loc'] ?>"><input type="hidden"><?php echo $row['Loc'];?></td>
<td name="rows[0][0][rp-code]" class="rp-code ui-widget-content" align="center" data-rp-code="<?php echo $row['Rp-Code'] ?>" id="rp-code-<?php echo intval ($row['Rp-Code'])?>"><?php echo $row['Rp-Code'];?></td>
<td name="rows[0][0][sku]" class="sku ui-widget-content" data-sku="<?php echo $row['SKU'] ?>" id="sku-<?php echo intval ($row['SKU'])?>"><?php echo $row['SKU'];?></td>
<td name="rows[0][0][special-id]" class="special-id ui-widget-content" data-special-id="<?php echo $row['Special-ID'] ?>" align="center" id="special-id-<?php echo intval ($row['Special-ID'])?>"><?php echo $row['Special-ID'];?></td>
<td name="rows[0][0][description]" class="description ui-widget-content" data-description="<?php echo $row['Description'] ?>" id="description-<?php echo intval ($row['Description'])?>"><?php echo $row['Description'];?></td>
<td name="rows[0][0][quantity]" class="quantity ui-widget-content" data-quantity="<?php echo $row['Quantity'] ?>" align="center" id="quantity-<?php echo intval ($row['Quantity'])?>"><?php echo $row['Quantity'];?></td>
<td name="rows[0][0][unit]" class="unit ui-widget-content" data-unit="<?php echo $row['Unit'] ?>" id="unit-<?php echo intval ($row['Unit'])?>"><?php echo $row['Unit'];?></td>
<td name="rows[0][0][quant]" style="display: none;" class="quantity_num ui-widget-content"><input type="textbox" style="width: 100px;" class="spinner" id="spin-<?php echo intval ($row['ID'])?>"></td>
</tr>
<?php } ?>
</tbody>
</table>
Javascript that adds data to email:
function sendMail() {
var link = "mailto:me#example.com"
+ "?subject=" + encodeURIComponent("Order")
+ "&body=" + encodeURIComponent($('.loc').data('loc')) + '\xa0\xa0' + encodeURIComponent($('.rp-code').data('rp-code')) + '\xa0\xa0' + encodeURIComponent($('.sku').data('sku')) + '\xa0\xa0' + encodeURIComponent($('.special-id').data('special-id')) + '\xa0\xa0' + encodeURIComponent($('.description').data('description')) + '\xa0\xa0' + encodeURIComponent($('.quantity').data('quantity')) + '\xa0\xa0' + encodeURIComponent($('.unit').data('unit')) + '\xa0\xa0' + encodeURIComponent($('.quantity_num').data('quantity_num'));
window.location.href = link;
}
While this is terrbile idea to send emails via JS like this, You can try:
function sendMail(){
var link = "mailto:me#example.com"
+ "?subject=" + encodeURIComponent("Order")
+ "&body=";
var body = '';
$('table tr input[name="check"]:checked').each(function(){
var current_tr = $(this).parent().parent();
var loc = current_tr.find('.loc').data('loc');
body += encodeURIComponent(loc) + '\xa0\xa0';
var loc =current_tr.find('.rp-code').data('rp-code');
body += encodeURIComponent(loc) + '\xa0\xa0';
});
body += '"';
link += body;
console.log(link);
}
You need to iterate over every TR that contains checkbox and check if it's :checked .
Then add avery data that You need into your's body var.
You are calling array elements like rows[0][0][rp-code] outside of PHP tags.
You could create the tables more easily by keeping all nested inside the PHP tags and by echoing each table elements.
Like so:
echo <tr>;
echo <td name="rows[0][0][rp-code]">Some Text</td>;
echo </tr>;
You could also do everything inside javascript, but this depends on your preferences
I have a table with multiple columns (index.php). One column is a checkbox. Whenever the checkbox is checked, it displays another column where you can select a quantity.
I want to be able to hit a button called "Add to Order" and have it use AJAX to add any selected rows to the index-order.php page. After all selections have been made, I want to be able to click the "Checkout" button and have it take me to the index-order.php page where it should be displaying all of the added selections in a table.
I know how to navigate to the index-order.php page so my main question is how can I add the selected rows to the index-order.php page by using ajax when pressing the button?
Index.php code:
<form name="form1" action="index-order.php">
<section id="checkout-btn">
<button type="submit" id="checkout" name="order">Checkout</button>
</section>
</form>
<form name="form2" method="POST" action="index-order.php">
<section id="addToOrder">
<button type="submit" class="order" id="order" name="order" value="AddToOrder">Add to Order</button>
</section>
<br>
<table id="merchTable" cellspacing="5" class="sortable">
<thead>
<tr class="ui-widget-header">
<th class="sorttable_nosort"></th>
<th class="sorttable_nosort">Loc</th>
<th class="merchRow">Report Code</th>
<th class="merchRow">SKU</th>
<th class="merchRow">Special ID</th>
<th class="merchRow">Description</th>
<th class="merchRow">Quantity</th>
<th class="sorttable_nosort">Unit</th>
<th style="display: none;" class="num">Quantity #</th>
</tr>
</thead>
<tbody>
<?php foreach ($dbh->query($query) as $row) {?>
<tr>
<td class="ui-widget-content"><input type="checkbox" class="check" name="check"></td>
<td name="rows[0][0][loc]" class="loc ui-widget-content" id="loc-<?php echo intval ($row['Loc'])?>"><?php echo $row['Loc'];?></td>
<td name="rows[0][0][rp-code]" class="rp-code ui-widget-content" align="center" id="rp-code-<?php echo intval ($row['Rp-Code'])?>"><?php echo $row['Rp-Code'];?></td>
<td name="rows[0][0][sku]" class="sku ui-widget-content" id="sku-<?php echo intval ($row['SKU'])?>"><?php echo $row['SKU'];?></td>
<td name="rows[0][0][special-id]" class="special-id ui-widget-content" align="center" id="special-id-<?php echo intval ($row['Special-ID'])?>"><?php echo $row['Special-ID'];?></td>
<td name="rows[0][0][description]" class="description ui-widget-content" id="description-<?php echo intval ($row['Description'])?>"><?php echo $row['Description'];?></td>
<td name="rows[0][0][quantity]" class="quantity ui-widget-content" data-quantity="<?php echo $row['Quantity'] ?>" align="center" id="quantity-<?php echo intval ($row['Quantity'])?>"><?php echo $row['Quantity'];?></td>
<td name="rows[0][0][unit]" class="unit ui-widget-content" id="unit-<?php echo intval ($row['Unit'])?>"><?php echo $row['Unit'];?></td>
<td name="rows[0][0][quant]" style="display: none;" class="quantity_num ui-widget-content"><input type="textbox" style="width: 100px;" class="spinner" name="value" id="test"></td>
</tr>
<?php } ?>
</tbody>
</table>
</form>
<div id="log"></div>
Index-order.php:
<?php if(isset($_POST['rows'])): ?>
<table cellspacing="20">
<tr align="center">
<th>Loc</th>
<th>Report Code</th>
<th>SKU</th>
<th>Special ID</th>
<th>Description</th>
<th>Quantity</th>
<th>Unit</th>
<th>Quantity #</th>
</tr>
<?php
foreach($_POST['rows'][0] as $row):
?>
<tr align="center">
<td><?php echo $row['loc']; ?></td>
<td><?php echo $row['rp-code']; ?></td>
<td><?php echo $row['sku']; ?></td>
<td><?php echo $row['special-id']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['unit']; ?></td>
<td><?php echo $row['quant']; ?></td>
</tr>
<?php
endforeach;
?>
</table>
<?php endif; ?>
JavaScript:
$(function () {
$("#order").click(function() {
//reset the logger
$('#log').empty();
$('#merchTable input:checkbox:checked').each(function() {
//for each checked checkbox, iterate through its parent's siblings
var array = $(this).parent().siblings().map(function() {
return $(this).text().trim();
}).get();
console.log(array);
//to print the value of array
$('#log').append(JSON.stringify(array))
var request = $.ajax({
type: 'post',
url: 'index-order.php',
data: array,
success: function(){
alert('success');
},
error: function(){
alert('failure');
}
});
})
});
});
To pass data from one page to another using php you will have to use a session.
So in your ajax call just save your data into the session like so:
add-to-cart.php
session_start();
if (!isset($_SESSION['shopping_cart'])) {
$_SESSION['shopping_cart'] = [];
} else {
$_SESSION['shopping_cart'][] = [
'id' => $_POST['item_id'],
'amount' => $_POST['amount']
];
}
If you then click your checkout button and navigate via a page reload to your index-order.php, you could then access the data via the same session index.
session_start();
if (isset($_SESSION['shopping_cart'])) {
// display your data
} else {
echo "Your shopping cart is empty.";
}
I have a table with multiple columns (index.php). One column is a checkbox. Whenever the checkbox is checked, it displays another column where you can select a quantity.
I want to then be able to hit a button called "Add to Order" and have it use AJAX to add any selected rows to the index-order.php page. After all selections have been made, I want to be able to click the "Checkout" button and have it take me to the index-order.php page where it should display all of the added selections in a table.
I know how to navigate to the index-order.php page so my main question is how can I add the selected rows to the page while using ajax and staying on the current page when doing so?
Index.php code:
<form name="form1" action="index-order.php">
<section id="checkout-btn">
<button type="submit" id="checkout" name="order">Checkout</button>
</section>
</form>
<form name="form2" method="POST" action="index-order.php">
<section id="addToOrder">
<button type="submit" class="order" id="order" name="order" value="AddToOrder">Add to Order</button>
</section>
<br>
<div id="my-div2" class="ui-widget">
<div class="ui-widget">
<table id="merchTable" cellspacing="5" class="sortable">
<thead>
<tr class="ui-widget-header">
<th class="sorttable_nosort"></th>
<th class="sorttable_nosort">Loc</th>
<th class="merchRow">Report Code</th>
<th class="merchRow">SKU</th>
<th class="merchRow">Special ID</th>
<th class="merchRow">Description</th>
<th class="merchRow">Quantity</th>
<th class="sorttable_nosort">Unit</th>
<th style="display: none;" class="num">Quantity #</th>
</tr>
</thead>
<tbody>
<?php foreach ($dbh->query($query) as $row) {?>
<tr>
<td class="ui-widget-content"><input type="checkbox" class="check" name="check"></td>
<td name="rows[0][0][loc]" class="loc ui-widget-content" id="loc-<?php echo intval ($row['Loc'])?>"><?php echo $row['Loc'];?></td>
<td name="rows[0][0][rp-code]" class="rp-code ui-widget-content" align="center" id="rp-code-<?php echo intval ($row['Rp-Code'])?>"><?php echo $row['Rp-Code'];?></td>
<td name="rows[0][0][sku]" class="sku ui-widget-content" id="sku-<?php echo intval ($row['SKU'])?>"><?php echo $row['SKU'];?></td>
<td name="rows[0][0][special-id]" class="special-id ui-widget-content" align="center" id="special-id-<?php echo intval ($row['Special-ID'])?>"><?php echo $row['Special-ID'];?></td>
<td name="rows[0][0][description]" class="description ui-widget-content" id="description-<?php echo intval ($row['Description'])?>"><?php echo $row['Description'];?></td>
<td name="rows[0][0][quantity]" class="quantity ui-widget-content" data-quantity="<?php echo $row['Quantity'] ?>" align="center" id="quantity-<?php echo intval ($row['Quantity'])?>"><?php echo $row['Quantity'];?></td>
<td name="rows[0][0][unit]" class="unit ui-widget-content" id="unit-<?php echo intval ($row['Unit'])?>"><?php echo $row['Unit'];?></td>
<td name="rows[0][0][quant]" style="display: none;" class="quantity_num ui-widget-content"><input type="textbox" style="width: 100px;" class="spinner" name="value" id="test"></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</form>
Index-order.php:
<?php if(isset($_POST['rows'])): ?>
<table cellspacing="20">
<tr align="center">
<th>Loc</th>
<th>Report Code</th>
<th>SKU</th>
<th>Special ID</th>
<th>Description</th>
<th>Quantity</th>
<th>Unit</th>
<th>Quantity #</th>
</tr>
<?php
foreach($_POST['rows'][0] as $row):
?>
<tr align="center">
<td><?php echo $row['loc']; ?></td>
<td><?php echo $row['rp-code']; ?></td>
<td><?php echo $row['sku']; ?></td>
<td><?php echo $row['special-id']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['unit']; ?></td>
<td><?php echo $row['quant']; ?></td>
</tr>
<?php
endforeach;
?>
</table>
<?php endif; ?>
JavaScript (obviously needs some work. Posting what I have so far):
$(function () {
$('#form2').on('submit', function (e) {
if($('input.check').is(':checked')) {
$(this).closest('tr');
e.preventDefault();
var request = $.ajax({
type: 'post',
url: 'index-order.php',
data: $('#form2').serialize(),
success: function(){
alert('success');
},
error: function(){
alert('failure');
}
});
}
});
});
What you can do is get all the siblings of the current checked checkbox and based on that get all the columns and separate the data to finally send it via ajax.
You can do something like this:
$('#form2').on('submit', function (e) {
$checkbox = $(this).find('input.check:checked').parent('td'); //Finds the checked checkbox's column inside #form2
$otherColumns = $checkbox.nextAll(); //Gets all the siblings
});
The above code will get you all the other columns next to the checked checkbox inside your form. What you an do next is travel across $otherColumns and get each individual value using $otherColums.eq(n).html() where n is the number of the column you are trying to get.
I am doing a simple quiz program using PHP and JavaScript(for its timer)
Here is my code
<!DOCTYPE html>
<html>
<head>
</head>
<body bgcolor="#CCFFCC">
<?php
session_start ();
$_SESSION['username'];
?>
<div id="wrap">
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="table"; // Database name
$tbl_name="q_quiz"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$id=$_GET['q_id'];
$sql="SELECT * FROM $tbl_name WHERE q_id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="100%" frame="box" cellpadding="3" cellspacing="1" bgcolor="#CCFFCC">
<tr>
<td bgcolor="#CCFFCC"><h3>Quiz Info : </h3></td>
</tr>
<tr>
<td bgcolor="#CCFFCC"><strong>Quiz Name : </strong><?php echo $rows['q_name']; ?></td>
</tr>
</table>
</td>
</tr>
</table>
<div style="font-weight: bold" id="quiz-time-left"></div>
<script type="text/javascript">
var max_time = <?php echo $rows['q_time'] ?>;
var c_seconds = 0;
var total_seconds =60*max_time;
max_time = parseInt(total_seconds/60);
c_seconds = parseInt(total_seconds%60);
document.getElementById("quiz-time-left").innerHTML='Time Left: ' + max_time + ' minutes ' + c_seconds + ' seconds';
function init(){
document.getElementById("quiz-time-left").innerHTML='Time Left: ' + max_time + ' minutes ' + c_seconds + ' seconds';
setTimeout("CheckTime()",999);
}
function CheckTime(){
document.getElementById("quiz-time-left").innerHTML='Time Left: ' + max_time + ' minutes ' + c_seconds + ' seconds' ;
if(total_seconds <=0){
setTimeout('document.quiz.submit()',1);
} else
{
total_seconds = total_seconds -1;
max_time = parseInt(total_seconds/60);
c_seconds = parseInt(total_seconds%60);
setTimeout("CheckTime()",999);
}
}
init();
</script>
</font>
Problem starts here
<br>
<?php
//so i will set a default score of 0.
$score = 0;
$tbl_name2="a_quiz";
$sql2="SELECT * FROM $tbl_name2 WHERE q_id='$id'";
$result2=mysql_query($sql2);
while($rows=mysql_fetch_array($result2)){
?>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr style='overflow:hidden; word-wrap:break-word;'>
<td bgcolor="lightgreen"><strong>Question:</strong></td>
<td bgcolor="lightgreen">:</td>
<td bgcolor="lightgreen" style="max-width: 1000px;"><?php echo $rows['q_question'] ?></td>
</tr>
<tr>
<td bgcolor="lightgreen"><strong>A</strong></td>
<td bgcolor="lightgreen">:</td>
<td bgcolor="lightgreen"><input type="radio" name="<?php echo $rows['q_question'] ?>" value="a"><?php echo $rows['a'] ?></td>
</tr>
<tr>
<td width="18%" bgcolor="lightgreen"><strong>B</strong></td>
<td width="5%" bgcolor="lightgreen">:</td>
<td width="77%" bgcolor="lightgreen"><input type="radio" name="<?php echo $rows['q_question'] ?>" value="b"><?php echo $rows['b'] ?></td>
</tr>
<tr>
<td width="18%" bgcolor="lightgreen"><strong>C</strong></td>
<td width="5%" bgcolor="lightgreen">:</td>
<td width="77%" bgcolor="lightgreen"><input type="radio" name="<?php echo $rows['q_question'] ?>" value="c"><?php echo $rows['c'] ?></td>
</tr>
<tr>
<td bgcolor="lightgreen"><strong>D</strong></td>
<td bgcolor="lightgreen">:</td>
<td bgcolor="lightgreen"><input type="radio" name="<?php echo $rows['q_question'] ?>" value="d"><?php echo $rows['d'] ?></td>
</tr>
<?php
In this part im getting the correct answer in the database (the answer is a)
$correct_answer == $rows['answer'];
This score = score + 1; is not working
if ($correct_answer==$_POST[$rows['q_question']]){
$score = $score + 1;
}
?>
<?php
$connection=mysql_connect('localhost', 'root','');
mysql_select_db('thesis');
$username= $_SESSION['username'];
$query6 = mysql_query("SELECT * FROM student WHERE username='$username'");
$row6 = mysql_fetch_assoc($query6);
$s_id = $row6['id'];
$name = $row6['name'];
$email = $row6['email'];
$position = $row6['position'];
?>
This is where I get all the data needed in the results
<p hidden>
<input type="text" name="s_id" value="<?php echo $s_id ?>">
<input type="text" name="q_id" value="<?php echo $rows['q_id'];?>">
<input type="text" name="score" value="<?php echo $score; ?>">
</p>
</table></td>
</tr>
<hr>
</table><br>
<?php
}
?>
<input type="submit" name="submit" value="Submit Answer" class="btn">
</form>
After hitting the submit button, im only getting s_id, q_id , but the score is still 0 even if I selected the correct answer
My problem here is getting the selected radio button and then comparing it to the answer(where is in the database)
I just did a quick mock up and this worked for me.
$answer = $_POST["'" . $rows['q_question'] . "'"];
if ($correct_answer == $answer)
{
$score++;
}