Cannot get radio button value on while loop - javascript

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++;
}

Related

Quotes Cutting Off Display Data

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>

Add Checked Table Rows to Email Body

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

Provide Validation for JavaScript Spinner

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;
}

Javascript : Uncaught TypeError

<script>
function more(){
var MoreDetails = document.getElementById('MoreDetails');
MoreDetails.style.display ="block";
}
function same(){
var SameProduct = document.getElementById('SameProduct');
SameProduct.style.display = "block";
}
</script>
<!DOCTYPE HTML>
<?php
$con = mysqli_connect('localhost', 'root', '', 'test') or die("Cannot connect to datbase");
// $seller = $_SESSION['sellMail'];
?>
<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
<script>tinymce.init({ selector:'textarea' });</script>
<html>
<head>
</head>
<body>
<div id = "ProductForm">
<form id="product" method="POST" enctype = "multipart/form-data" novalidate>
<table align="center" width="750" border ="2">
<tr align="center">
<td colspan="7"><h2>Insert Product</h2></td>
</tr>
<tr>
<td align="right"><b>Type of Product: </b></td>
<td>
<input list="text" name="type" placeholder = "Select a type" required>
<datalist id="text">
<?php
$run = mysqli_query($con, "Select * from typeproduct");
while($row = mysqli_fetch_array($run)){
$type_title = $row['type_title'];
echo "<option>$type_title</option>";
}
?>
</datalist>
</td>
</tr>
<tr>
<td align="right"><b>Product Category: </b></td>
<td>
<select name = "prd_cat" required>
<option>Select a Category</option>
<?php
$get_cat = "Select * from categories";
$run_cat = mysqli_query($con, $get_cat);
while($row = mysqli_fetch_array($run_cat)){
$cat_id = $row['cat_id'];
$cat_name = $row['cat_name'];
echo "<option>$cat_name</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td align="right"><b>Product Brand: </b></td>
<td>
<select name = "prd_brand" required>
<option>Select a Brand</option>
<?php
$get_brn = "Select * from brands";
$run_brn = mysqli_query($con, $get_brn);
while($row = mysqli_fetch_array($run_brn)){
$brn_id = $row['brand_id'];
$brn_name = $row['brand_title'];
echo "<option>$brn_name</option>";
}
?>
</select>
</td>
</tr>
<tr align="center">
<td><input type="submit" name="prdDetails" value="Insert Product"></td>
</tr>
</table>
</form>
</div>
<?php
if(isset($_POST['prdDetails'])){
$brand = $_POST['prd_brand'];
$type = $_POST['type'];
$cat = $_POST['prd_cat'];
$select = mysqli_query($con, "Select * from products where type = '$type' AND cat='$cat' AND brand = '$brand'");
if(mysqli_num_rows($select)==0){
echo "<script>more();</script>";
}else{
echo "<script>same();</script>";
$i=1;
?>
<table id="SameProduct" style="display:none">
<tr>
<td>S.No</td>
<td>Product Image</td>
<td>Product Title</td>
<td>Product Description</td>
<td>Same Product</td>
</tr>
<?php
while($row = mysqli_fetch_array($select)){
$img = $row['img'];
$title = $row['title'];
$desc = $row['p_desc'];
?>
<tr>
<td><?php echo $i;?></td>
<td><img src="Product_image/<?php echo $img;?>"</td>
<td><?php echo $title;?></td>
<td><?php echo $desc;?></td>
<td><button>Got the Same Product</button></td>
</tr>
<?php
$i++;
}
?>
<tr>
<td><button>Product Not in list</button></td>
</tr>
</table>
<?php
}
?>
<?php
}
?>
<form method="POST" id="MoreDetails" enctype="multipart/form-data">
<table style="display:none">
<tr>
<td align="right"><b>Product Image: </b></td>
<td><input type="file" name="prd_img" required/></td>
</tr>
<tr>
<td align="right"><b>Product Price: </b></td>
<td><input type="text" name="prd_price" required/></td>
</tr>
<tr>
<td align="right"><b>Product Description: </b></td>
<td><textarea name="prd_desc" cols = "20" rows="10" required></textarea></td>
</tr>
<tr>
<td align="right"><b>Product Keywords: </b></td>
<td><input type="text" name="prd_keyword" required/></td>
</tr>
<tr align="center">
<td colspan="7"><input type="submit" name="insertPrd" id="insertPrd" value="Insert Now"/></td>
</tr>
</table></form>
</body></html>
<?php
if(isset($_POST['insertPrd'])){
$title = $_POST['prd_title'];
$categ = $_POST['prd_cat'];
$brand = $_POST['prd_brand'];
$price = $_POST['prd_price'];
$desc = $_POST['prd_desc'];
$keywords = $_POST['prd_keyword'];
$img = $_FILES['prd_img']['name'];
$img_temp = $_FILES['prd_img']['tmp_name'];
move_uploaded_file($img_temp, "Product_image/$img");
$insert_prd = "INSERT INTO products(cat, brand, title, price, p_desc, img, keyword, seller) VALUES ('$categ', '$brand', '$title', '$price', '$desc', '$img', '$keywords', '$seller')";
$insert = mysqli_query($con, $insert_prd) or die("Cannot Insert");
if($insert){
echo "<script>alert('Product has been added');</script>";
echo "<script>window.open('Seller.php','_self')</script>";
}else{
echo "<script>alert('Sorry! The Product cannot be added');</script>";
echo "<script>window.open('Seller.php','_self')</script>";
}
}
?>
Whenever I am clicking on the prdDetails button, an error is there that
"Uncaught TypeError: Cannot read property style of 'null'" whether more() is called or same() is called.
I am currently working on the XAMPP server.
Other than that everything is working fine.
Your calls to more() and same() occur before the elements which they reference in the DOM.
Move the calls to more() and same() to somewhere after the elements MoreDetails and SameProduct.
This is because it document.getElementById('MoreDetails'); does not receive an DOM element and variable MoreDetails is null.
You should call function more() after DOM elemenet with id MoreDetails, but you call it before so you can't get this DOM element.

Filter an echoed table using javascript

I need help with regards to filtering my echoed table using javascript. I'm just using a as a refresher of my form. Take a look on my code below.
Here's my code for the filtering
<div align="center">
<?php
// Connect to server and select database.
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("leavecalendar") or die(mysql_error());
$i=0;
$result = mysql_query("SELECT fullname FROM employee");
$storeArray = Array();
echo '<label>Filter By Name:</label><select name="fullname" style="width: 200px">';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<option >".$row['fullname']."</option>";
}
?>
</select>
<!--Filter by Date -->
<label>Filter By Date:</label>
<input class="datepicker">
click
</div>
Here's my code for the echoed table
<div style="height:70%;">
<?php
$con=mysqli_connect("localhost","root","","leavecalendar");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = mysqli_query($con,"SELECT * FROM timekeeping WHERE id='$row[fullname]'");
?>
<table width="10%" border="0" cellspacing="1" cellpadding="0" style="width: 897px;">
<tr>
<td>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC" class="form-table">
<tr>
<td align="center" bgcolor="#FFFFFF" class="tdclass"><strong>Full Name</strong></td>
<td align="center" bgcolor="#FFFFFF" class="tdclass"><strong>Clock In</strong></td>
<td align="center" bgcolor="#FFFFFF" class="tdclass"><strong>Clock Out</strong></td>
<?php
while($row = mysqli_fetch_array($sql))
{
?>
<tr>
<td bgcolor="#FFFFFF"><?php echo $row['fullname']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['actualstart']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['actualend']; ?></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
</div>
All in all there 2 codes are under 1 form only. So below is the summary of the codes for your reference.
<form name="form1" method="post" action="deleteleave.php">
<div align="center">
<?php
// Connect to server and select database.
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("leavecalendar") or die(mysql_error());
$i=0;
$result = mysql_query("SELECT fullname FROM employee");
$storeArray = Array();
echo '<label>Filter By Name:</label><select name="fullname" style="width: 200px">';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<option >".$row['fullname']."</option>";
}
?>
</select>
<!--Filter by Date -->
<label>Filter By Date:</label>
<input class="datepicker">
click
</div>
<div class="gridbody">
<div style="height:70%;">
<?php
$con=mysqli_connect("localhost","root","","leavecalendar");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = mysqli_query($con,"SELECT * FROM timekeeping WHERE id='$row[fullname]'");
?>
<table width="10%" border="0" cellspacing="1" cellpadding="0" style="width: 897px;">
<tr>
<td>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC" class="form-table">
<tr>
<td align="center" bgcolor="#FFFFFF" class="tdclass"><strong>Full Name</strong></td>
<td align="center" bgcolor="#FFFFFF" class="tdclass"><strong>Clock In</strong></td>
<td align="center" bgcolor="#FFFFFF" class="tdclass"><strong>Clock Out</strong></td>
<?php
while($row = mysqli_fetch_array($sql))
{
?>
<tr>
<td bgcolor="#FFFFFF"><?php echo $row['fullname']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['actualstart']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['actualend']; ?></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
</div>
</div>
<input name="endorse" type="submit" id="endorse" value="Endorse Selected Leave/s">
And here's my javascript for refreshing the page. Pratically, I dont want to refresh the whole page but instead I want to refresh the form only so as to display the echoed table filtere by the dropdown or the datepicker. Hope you get my point there.
Code for refreshing the page.
<script>
$(function() {
$("#refresh").click(function() {
$("#Container").load()
})
})
</script>
Thank you for all the answers you will give. Much appreciated.
Based on your code do something like this:
Make a file echoed_table.php and put in (your code):
<div style="height:70%;">
<?php
$con=mysqli_connect("localhost","root","","leavecalendar");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = mysqli_query($con,"SELECT * FROM timekeeping WHERE id='$row[fullname]'");
?>
<table width="10%" border="0" cellspacing="1" cellpadding="0" style="width: 897px;">
<tr>
<td>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC" class="form-table">
<tr>
<td align="center" bgcolor="#FFFFFF" class="tdclass"><strong>Full Name</strong></td>
<td align="center" bgcolor="#FFFFFF" class="tdclass"><strong>Clock In</strong></td>
<td align="center" bgcolor="#FFFFFF" class="tdclass"><strong>Clock Out</strong></td>
<?php
while($row = mysqli_fetch_array($sql))
{
?>
<tr>
<td bgcolor="#FFFFFF"><?php echo $row['fullname']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['actualstart']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['actualend']; ?></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
</div>
Than in order to load/update the table make the ajax call (maybe including additional (filter) url-parameters):
$.ajax('echoed_table.php').success(function(data) {
$('.gridbody').innerHtml(data); // better use an id to identify the container div, instead of the class here
});

Categories

Resources