Javascript : Uncaught TypeError - javascript

<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.

Related

How to calculate discount, TAX (VAT), Subtotal and Grand Total In a dynamic Table

I just started with jQuery so im a still a newbie. Could somebody please help me with how to calculate the new price in a dynamic table. I have a dynamic table with 3 columns, Qty, Discount and Price. I need to recalculate the new price after a user enter a discount(%) in the same textbox the current price is, the subtotal and the grand total.
Here is my table:
<table class="table">
<thead><tr><th style="width:60%">Description</th><th class="centrer" style="width:10%">Qty</th><th class="centrer" style="width:10%">Discount</th><th class="centrer" style="width:20%">Price</th></tr></thead>
<tbody>
<?php
if(isset($listeArticlePourUnDossier) && count($listeArticlePourUnDossier) > 0)
{
$sousTotal = 0;
$symbol = '';
$VAT = 0;
$TTC = 0;
$iArticles = 0;
while($iArticles < count($listeArticlePourUnDossier))
{
$ARTICLE_CURRENCY = $listeArticlePourUnDossier[$iArticles]['ARTICLE_CURRENCY'];
$sousTotal = $sousTotal + ($listeArticlePourUnDossier[$iArticles]['ARTICLE_PRIX']);
?>
<tr>
<td><?php echo ($listeArticlePourUnDossier[$iArticles]['ARTICLE_NOM']); ?></td>
<td class="centrer">1</td>
<td class="centrer"><input type="text" name="txtdiscount" id="txtdiscount_<?php echo $iArticles; ?>" onkeyup="calc()"/></td>
<td class="centrer"><input type="text" name="txtPrice" id="txtPrice_<?php echo $iArticles; ?>" readonly value="<?php echo (number_format($listeArticlePourUnDossier[$iArticles]['ARTICLE_PRIX'],2)); ?>"/></td>
</tr>
<?php
$iArticles++;
}
?>
<tr>
<td class="nocolor"></td>
<td></td>
<td class="centrer bold">Subtotal</td>
<td class="centrer bold"><?php echo (number_format($sousTotal,2)); ?></td>
</tr>
<tr>
<td class="nocolor"></td>
<td></td>
<td class="centrer bold">VAT</td>
<td class="centrer"><?phpecho (number_format($VAT,2)); ?></td>
</tr>
<tr>
<td class="nocolor"></td>
<td></td>
<td class="centrer bold">G.Total</td>
<td class="centrer bold">
<?php
$TTC = $sousTotal - $VAT;
echo (number_format($TTC,2));
?>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
How to fill up the new price in the current price textbox as soon as a user enter a discount say 10%?
Thank you for your assistance.
You can fill the discounted amount in the price field like this, please check the below code, Hope it will help,
Here, updated td's, which you are using and js code.
Thanks.
<td class="centrer">
<input type="text" name="txtdiscount" value ="" id="txtdiscount_<?php echo $iArticles; ?>" onkeyup="calc(this.value,'<?php echo $iArticles; ?>')"
/>
</td>
<td class="centrer">
<input type="text" name="txtPrice" id="txtPrice_<?php echo $iArticles; ?>" readonly value="<?php echo (number_format($data['price'],2)); ?>" />
<input type="hidden" name="hidden_txtPrice" id="hidden_txtPrice_<?php echo $iArticles; ?>" readonly value="<?php echo (number_format($data['price'],2)); ?>" />
</td>
<tr>
<td class="nocolor"></td>
<td></td>
<td class="centrer bold">VAT</td>
<td class="centrer">
<select name="vatDropdown" id="vatDropdown">
<option value="-1">Select VAT</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
</td>
</tr>
<tr>
<td class="nocolor"></td>
<td></td>
<td class="centrer bold">G.Total</td>
<td class="centrer bold" id="grandtotal">
<?php echo (number_format($TTC,2)); ?>
</td>
<input type="hidden" name="hidden_total" id="hidden_total" value="<?php echo (number_format($TTC,2)); ?>" />
</tr>
<script>
function calc(discount, article_id)
{
var price = 0;
var dis_amt = 0;
var calculatedAmt = 0;
var discount_amt = 0;
price = $("#hidden_txtPrice_"+article_id).val();
if(discount != '')
{
dis_amt = parseFloat(discount) / 100;
calculatedAmt = parseFloat(price) * parseFloat(dis_amt);
discount_amt = parseFloat(price) - parseFloat(calculatedAmt);
$("#txtPrice_"+article_id).val(discount_amt);
}
else
{
$("#txtPrice_"+article_id).val(price);
}
}
$("#vatDropdown").on('change', function(){
var dropdown_val = $(this).val();
var grandtotal = $("#hidden_total").val();
if(dropdown_val == '1')
{
var vat_dis = 15 / 100;
var vat_amt = parseFloat(grandtotal) * parseFloat(vat_dis);
var gTotal = parseFloat(grandtotal) + parseFloat(vat_amt);
$("#grandtotal").html(gTotal.toFixed(2));
}
else
{
$("#grandtotal").text(grandtotal);
}
});
</script>

incorrect run php by button click

I have php function which update all records in data table and need it run by click button in html.
My php function look like this:
<?php
try {
$sql = 'SELECT id_data, date_record, value1, value2, value3 FROM data ';
$s = $pdo->prepare($sql);
$s->execute();
} catch (PDOException $e) {
$error = 'Error with select data' . $e->getMessage();
include 'error.html.php';
exit();
}
while ($row = $s->fetch()) {
$dane[] = array(
'id_data' => $row['id_data'],
'date_record' => $row['date_record'],
'value1' => $row['value1'],
'value2' => $row['value2'],
'value3' => $row['value3']
);
}
if (isset($_GET['edytion'])) {
foreach ($data as $data2) {
try {
$sql = 'UPDATE data SET date_record = :date_record, value1 = :value1, value2 = :value2, value3 = :value3 WHERE id_data= :id_data';
$s = $pdo->prepare($sql);
$s->bindValue(':date_record', $_POST['date_record']);
$s->bindValue(':value1', $_POST['value1']);
$s->bindValue(':value2', $_POST['value2']);
$s->bindValue(':value3', $_POST['value3']);
$s->bindValue(':id_data', $_POST['id_data']);
$s->execute();
} catch (PDOException $e) {
$error = 'Edit data error ' . $e->getMessage();
include 'error.html.php';
exit();
}
}
Header("Location: theme3.php");
}
?>
And my form in html where i try run this look:
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>Date</th>
<th>Value 1</th>
<th>Value 2</th>
<th>Value 3</th>
</tr>
</thead>
<?php if (isset($data)): ?>
<?php foreach ($data as $data1): $a = 0 ?>
<form action="?edytion" method="post" id='ed'>
<tr class="bg-primary">
<input type="hidden" name="id_data" id="id_data" value="<?php echo $data1['id_data']; ?>">
<td><input type="date" name="date_record" id="date_record" value="<?php echo $data1['date_record']; ?>"> </td>
<td><input type="text" name="value1" id="value1" value="<?php echo $data1['value1']; ?>"> </td>
<td><input type="text" name="value2" id="value2" value="<?php echo $data1['value2']; ?>"> </td>
<td><input type="text" name="value3" id="value3" value="<?php echo $data1['value3']; ?>"> </td>
<!-- <input type="hidden" ondblclick="default" id="id_buttona" value="Edit"/> -->
</tr>
</form>
<?php $a++;
endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<input type="submit" id="id_buttona" onclick="document.getElementById('ed').submit();" value="Edit"/>
</div>
Ultimately when i try update data it update only first record in table, rest of them is invariable.
Anybody know what is wrong and have idea how correct it? I will be grateful for help!
You create too many forms with the same id. Surround the table with the <form> and do foreach on <tr> only.
Something like this
<div class="table-responsive">
<form action="?edytion" method="post" id='ed'>
<table class="table table-bordered">
<thead>
<tr>
<th>Date</th>
<th>Value 1</th>
<th>Value 2</th>
<th>Value 3</th>
</tr>
</thead>
<?php if (isset($data)): ?>
<?php $a = 0; foreach ($data as $data1): ?>
<tr class="bg-primary">
<input type="hidden" name="id_data<?php echo $a; ?>" id="id_data" value="<?php echo $data1['id_data']; ?>" />
<td><input type="date" name="date_record<?php echo $a; ?>" value="<?php echo $data1['date_record']; ?>"> </td>
<td><input type="text" name="value1<?php echo $a; ?>" value="<?php echo $data1['value1']; ?>"> </td>
<td><input type="text" name="value2<?php echo $a; ?>" value="<?php echo $data1['value2']; ?>"> </td>
<td><input type="text" name="value3<?php echo $a; ?>" value="<?php echo $data1['value3']; ?>"> </td>
</tr>
<?php $a++;
endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<input name="row_count" value="<?php echo isset($a) ? $a : 0; ?>" type="hidden"/>
</form>
<input type="submit" id="id_buttona" onclick="document.getElementById('ed').submit();" value="Edit"/>
</div>
The way you created each form for each row will not work well because: (1) all your forms have the same javascript id, when you do getElementById only the first form is affected, (2) when you submit that one form the page reloads and all the changes to other rows are lost.
One solution is to make only 1 form and have different name to all fields. Forms fields are sent by name and value so you need different names for all fields and you don't really need ids.
You can add row count somewhere in the form and then change php to something like this:
$row_count = $_POST['row_count'];
for($i = 0; i < $row_count; i++) {
try {
$sql = 'UPDATE data SET
date_record = :date_record,
value1 = :value1,
value2 = :value2,
value3 = :value3
WHERE id_data= :id_data';
$s = $pdo->prepare($sql);
$s->bindValue(':date_record', $_POST['date_record' . $i]);
$s->bindValue(':value1', $_POST['value1' . $i]);
$s->bindValue(':value2', $_POST['value2' . $i]);
$s->bindValue(':value3', $_POST['value3' . $i]);
$s->bindValue(':id_data', $_POST['id_data' . $i]);
$s->execute();
}
catch (PDOException $e) {
$error = 'Edit data error ' . $e->getMessage();
include 'error.html.php';
exit();
}
}
It is all html file
<!DOCTYPE HTML PUBLIC>
<html>
<head>
<title>Tematyka2</title>
<link rel="stylesheet" href="style.css" type="text/css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"/>
</head>
<body>
<div id="container">
<div id="heading"></div>
<table border="0" width="766" cellpadding="0" cellspacing="0" align="center">
<tr>
<td class="t1"><p>Example title</p></td><td class="t2"> <b class="linkW" style="padding-left: 60%">Logout</b></td>
</tr>
<tr>
<td class="top1"><div class="inscription">Your Page</div></td>
<td class="top2" valign="top"></td>
</tr>
</table>
<table align="center" cellpadding="0">
<tr>
<td valign="top">
<table cellpadding="0" cellspacing="0">
<tr><td class="topm">Main menu</td></tr>
<tr><td class="tlom">
:: Main page <br>
</td></tr>
<tr><td class="dolm"></td></tr
</table>
<table cellpadding="0" cellspacing="0">
<tr><td class="topm">Them</td></tr>
<tr><td class="tlom">
:: Them 1 <br>
:: <b>Them 2</b> <br>
:: Them 3 <br>
</td></tr>
<tr><td class="dolm"></td></tr
</table>
<table cellpadding="0" cellspacing="0">
<tr><td class="topm">Charts</td></tr>
<tr><td class="tlom">
:: Chart1 <br>
:: Chart2 <br>
</td></tr>
<tr><td class="dolm"></td></tr>
</table>
<br>
</td>
<td width="1"></td>
<td valign="top">
<table cellpadding="0" cellspacing="0">
<tr><td class="topn"></td></tr>
<tr><td class="tlon">
<div class="span7 center">
<h2>
EDYTION
</h2>
</div>
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>Date</th>
<th>Value 1</th>
<th>Value 2</th>
<th>Value 3</th>
</tr>
</thead>
<?php if(isset($data)): ?>
<?php foreach($data as $data1): $a=0?>
<form action="?edytion" method="post" id='ed'>
<tr class="bg-primary">
<input type="hidden" name="id_data" id="id_data" value="<?php echo $data1['id_data']; ?>">
<td><input type="date" name="date_record" id="date_record" value="<?php echo $data1['date_record']; ?>"> </td>
<td><input type="text" name="value1" id="value1" value="<?php echo $data1['value1']; ?>"> </td>
<td><input type="text" name="value2" id="value2" value="<?php echo $data1['value2']; ?>"> </td>
<td><input type="text" name="value3" id="value3" value="<?php echo $data1['value3']; ?>"> </td>
<!-- <input type="hidden" ondblclick="default" id="id_buttona" value="Edit"/> -->
</tr>
</form>
<?php $a++; endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<input type="submit" id="id_buttona" onclick="document.getElementById('ed').submit();" value="Edit"/>
</div>
</td></tr>
<!-- <tr><td class="doln"></td></tr> -->
</table>
</td>
</tr>
</table>
<table align="center" cellpadding="0" cellspacing="0">
<tr>
<td class="foot1"> My page</td>
<td class="foot2"><img src="images/dol2.jpg" alt="anything"></td>
</tr>
</table>
And Php part:
<?php
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
include $_SERVER['DOCUMENT_ROOT'] . '/connection_to_database.php';
include $_SERVER['DOCUMENT_ROOT']. '/check_login.php'; try {
$sql = 'SELECT id_data, date_record, value1, value2, value3 FROM data ';
$s = $pdo->prepare($sql);
$s->execute();
} catch (PDOException $e) {
$error = 'Error with select data' . $e->getMessage();
include 'error.html.php';
exit();
}
while ($row = $s->fetch()) {
$dane[] = array(
'id_data' => $row['id_data'],
'date_record' => $row['date_record'],
'value1' => $row['value1'],
'value2' => $row['value2'],
'value3' => $row['value3']
);
}
if (isset($_GET['edytion'])) {
foreach ($data as $data2) {
try {
$sql = 'UPDATE data SET
date_record = :date_record,
value1 = :value1,
value2 = :value2,
value3 = :value3
WHERE id_data= :id_data';
$s = $pdo->prepare($sql);
$s->bindValue(':date_record', $_POST['date_record']);
$s->bindValue(':value1', $_POST['value1']);
$s->bindValue(':value2', $_POST['value2']);
$s->bindValue(':value3', $_POST['value3']);
$s->bindValue(':id_data', $_POST['id_data']);
$s->execute();
}
catch (PDOException $e) {
$error = 'Edit data error ' . $e->getMessage();
include 'error.html.php';
exit();
}
}
Header("Location: theme3.php");
}
?>
And that's all about update data. Php and html are in one file, I know it's look unprofessionally and i shoudln't do it in this way.

Cannot get radio button value on while loop

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

javascript won't run inside php

I have the following php page,
which has a dropdown box and when the value closed is chosen it is meant to open a text box.
I have embdedded the javascript inside php but for some reason it is not working.
<?php
require_once "/var/www/users/user.php";
$user = new CHUser(2);
require_once "/var/www/template/template.php";
$template = new CHTemplate();
require_once "/var/www/ENGINEER/DisplayWIPOnLocation.php";
$BundleProgress= new CHWIPProgress();
$todays_date = date('Y-m-d H:i:s');
$ID = $_GET['id'];
//$ID = $_POST['ID'];
$ChurchNo = $_POST['ChurchNo'];
$SerialNo = $_POST['SerialNo'];
$MachineType= $_POST['MachineType'];
$Model = $_POST['Model'];
$Location= $_POST['Location'];
$Priority = $_POST['Priority'];
$Issue = $_POST['Issue'];
$Status = $_POST['Status'];
$Call_Opened = $_POST['Call_Opened'];
//echo "test component $Component";
$wip = $BundleProgress-> ListTicketById($_GET['id']);
foreach($wip as $x) {
$ChurchNo = $x['ChurchNo'];
$SerialNo = $x['SerialNo'];
$MachineType= $x['MachineType'];
$Model = $x['Model'];
$Location= $x['Location'];
$Priority = $x['Priority'];
$Issue = $x['Issue'];
$Status = $x['Status'];
$Call_Opened = $x['Call_Opened'];
$Operator = $x['Operator'];
$Engineer = $x['Engineer'];
$Notes = $x['Notes'];
}
$content .="
<html>
<head>
<title></title>
<script language='JavaScript' type='text/javascript'>
function ShowTB(obj,id){
txt=obj.options[obj.selectedIndex].text;
if (txt.match('Pending')){
document.getElementById(id).style.visibility='hidden';
}
if (txt.match('Closed')){
document.getElementById(id).style.visibility='visible';
}
}
</script>
</head>
<body>";
$content .="
<p><b>You are changing the status of call no: $ID </b></p>
<table>
<tr><td> <b>ChurchNo</b>:$ChurchNo </td> </tr>
<tr><td> <b>SerialNo</b>: $SerialNo</td> </tr>
<tr><td> <b>MachineType</b>: $MachineType</td> </tr>
<tr><td> <b>Model</b>: $Model </td> </tr>
<tr><td> <b>Location</b>:$Location </td> </tr>
<tr><td> <b>Priority</b>: $Priority </td> </tr>
<tr><td> <b>Issue</b>: $Issue </td> </tr>
<tr><td> <b>Status</b>: $Status </td> </tr>
<tr><td> <b>Date Opened</b>:$Call_Opened </td> </tr>
<tr><td> <b>Operator</b>:$Operator </td> </tr>
<tr><td> <b>Logged By</b>:$Engineer </td> </tr>
</table>
</br>
</br>
<table>
<form method='post' action='UpdateTicket2.php'>
<tr><td><b>Assign to: </b></td> <td> <select name='Assignedto'>
<option value='David'>David</option>
<option value='Ashley'>Ashley</option>
<option value='Pete'>Pete</option>
<option value='Mark'>Mark</option>
<option value='Rory'>Rory</option>
</select>
</td>
</tr>
";
$content .="
<tr><td><b>Status </b></td>
<td>
<select onchange=\"ShowTB(this,'Date');\" >
<option >Open</option>
<option >Closed</option>
<option >Pending</option>
</select>
<br>
<input id='Date' style='visibility:hidden;' >
</td>
<tr> ";
$content .="
<tr><td><b>Notes:</b></td><td><textarea name='Notes' cols='25' rows='5'>
$Notes
</textarea></td></tr>
<tr><td><b>Current Time: $todays_date</b></td></tr>
<tr><td><input type='hidden' name='ID' id='ID' value='$ID' /></td></tr>
<tr><td><input type='submit' name ='add'/></td></tr>
</form>
</table>
";
$content .="
</br>
<p> Warning.... </p>
<form action='delete.php' method='post'>
<input type='submit' value='Delete Record' />
<p><input type='hidden' name='ID' id='ID' value='$ID' /></p>
</form>";
$template->SetTag("content", $content);
echo $template->Display();
?>
For testing purposes:
comment all until your program begin in
/*
*/
$content .="
then change:
$template->SetTag("content", $content);
echo $template->Display();
for:
echo $content;
finally, inside the script tags write:
alert('Javascript is working');
I made it and got the program running. You problem is in the $template class, not in this code.
Cheers,

EDIT: add an identifier to a dynamically generated row

I'm pulling down two tables from the db. There's a Javascript function that adds a row from Table2 into the Table1. What i've been trying to do is get the data from Table1 on the confirm_trade.php page so I can put it into the db but can't figure out how to pull that row(item_id). I thought putting the table in a form would allow me to access them through the $_POST but that's not working. How to add an identifier to the jquery row so I can grab on the confirm page?
The appended rows coming from the jQuery function are the rows I need the item_id from.
function addto(obj)
{
var $row = $(obj).parents("tr");
var $table = $("#tradee");
var item_id = $row.find(".item-id").text();
var item_name = $row.find(".item-name").text();
var item_desc = $row.find(".item-desc").text();
var newTR = $("<tr><td>"+item_id+"</td><td>"+item_name+
"</td><td>"+item_desc+"</td></tr>");
$table.append(newTR);
}
Table2:
<div id='fixedDiv'>
<form action="confirm.php" method="post">
<table align="center" id="Table2">
<tr><td>Other Item</td></tr>
<?php while($rowi =$item->fetch_assoc())
{?>
<tr>
<td>
<?php echo $rowi['item_id']; ?>
</td>
<td>
<?php echo $rowi['item_name']; ?>
</td>
<td><?php echo $rowi['item_description'];?></td>
</tr>
<?php } ?>
<tr>
<td><input type="submit" name="submit" value="Continue"/></td>
</tr>
</table>
</form>
</div>
<br>
Table 1:
<table align="center" id="Table1">
<tr><th>item_id</th>
<th>Cat_id</th>
<th>Name</th>
<th>Description</th>
</tr>
<?php while($row =$item_results->fetch_assoc())
{?>
<tr></tr>
<tr>
<td class="item-id"> <?php echo $row['item_id']; ?> </td>
<td><?php echo $cat_id = $row['cat_id']; ?> </td>
<td class="item-name"><?php echo $item_id = $row['item_name'];?></td>
<td class="item-desc"><?php echo $item_descrip = $row['item_description'];?>
</td>
<td><input type="button" class="added" onclick="addto(this)"
value="Add" />
<td><input type="button" class="removed" onclick="remove()"
value="Remove" >
</td>
</tr>
<?php } ?>
</table>
Judging by your code, you can use <input type="hidden" name="some_hidden_data" value="field-value">. be sure that you also wrap your table with <form method="post"></form>
the hidden field will be then included as $_POST['hidden_field_name'];

Categories

Resources