retrieving radio button value if name is from database(lopped) - javascript

i am making a simple quiz
the code below is my code for echoing the questions in the database
i used while loop to output the values in the database
i used the q_question for the name of each radio button, because if i put (sample : name="question" which is fixed, when it loop i am only available to pick 1 radio button even if theres 2 or more questions )
<?php
$tbl_name2="a_quiz"; // Switch to table "forum_answer"
$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>
</table></td>
</tr>
<hr>
</table><br>
<?php
}
?>
my problem is i cannot get the values of the radio button
im trying $my_answer=$_POST['$q_question'];(and i think this is so wrong) + submit button
help me get the values of the radio button, using php
but if you got idea using javascript etc.. im happy to see it

Related

Passing parameters to [Javascript] functions

I have this php grid display from ms sql server.
<?php while ($record = sqlsrv_fetch_array($result1)) { ?>
<tr bgcolor="#db97c6">
<td nowrap="nowrap" align="center"><?php echo $record['ProductName']; ?></td>
<td nowrap="nowrap" align="center"><?php echo $record['BrandName']; ?></td>
<td nowrap="nowrap" align="center"><?php echo $record['ProductPrice']; ?></td>
<td nowrap="nowrap" align="center"><?php echo number_format($record['ProductQty'],0); ?></td>
<td nowrap="nowrap" align="center"><?php echo number_format($record['TotalValue'],2); ?></td>
<td nowrap="nowrap" align="center"><?php echo number_format($record['Discount'],2); ?></td>
<td nowrap="nowrap" align="center"><?php echo number_format($record['NetValue'],2); ?></td>
<td nowrap="nowrap" align="center"><?php echo number_format($record['QtySupplied'],0); ?></td>
<td align="center">
<img src="images/update.png" height="15" width="15">
</td>
</tr>
<?php
}
?>
I want to pass PurchaseOrderDetlID to a javascript function which will open a popup window to edit the selected record
this is the javascript function.
function openForm(ParamId) {
$MyPurchaseOrderDetlID = ParamId;
document.getElementById("popupForm").style.display="block";
}
this is the popup window
<div class="login-popup">
<div class="form-popup" id="popupForm">
<form action="ProfileSrc.php" class="form-container">
<input type="text" id="ParamId" name="ParamId" value = "<?php echo $MyPurchaseOrderDetlID;?>" style="width:60px" >
<br>
<label for="QtyIn"> <strong>Qty Supplied</strong></label>
<br><br>
<input type="number" id="QtyIn" name="QtyIn" value=0.00 style="width:60px; text-align: right" required>
<br><br>
<button type="submit" class="btn">Save</button>
<button type="button" class="btn cancel" onclick="closeForm()">Close</button>
</form>
</div>
</div>
I am unable to pass PurchaseOrderDetlID to the popup (edit) window
You can pass it through the js function parameter. The same way you change the style of the form (display = block), you can set the value of the input:
function openForm(ParamId) {
document.getElementById("popupForm").style.display = "block";
document.getElementById("ParamId").value = ParamId;
}
Also no need to add the value in the form using PHP:
<input type="text" id="ParamId" name="ParamId" value="" style="width:60px">
This way every time openForm gets called/triggered it will set the value of the input with the desired value.

Data Table search is giving error

I'm getting a warning saying requested unknown parameter, and after I press ok the search is not working correctly.
any solution for this
DataTable warning .......... Requested unknown parameter
Please find how i created the table
<table class="dataTable border bordered striped" data-role="datatable" data-auto-width="false" id="brandTable">
<thead>
<tr>
<td style="width: 20px">
</td>
<td class="sortable-column sort-asc">ID</td>
<td class="sortable-column">Brand Name</td>
<td class="sortable-column">Account Manager</td>
<td class="sortable-column">Date Updated</td>
</tr>
</thead>
<tbody>
<?php foreach ($fw->customer($_SESSION['USERID'])->getAllBrands() as $v) { ?>
<tr>
<td>
<label class="input-control checkbox small-check no-margin">
<input type="checkbox">
<span class="check"></span>
</label>
</td>
<td>
<?php echo $v['id']; ?>
</td>
<td>
<a href="updateBrands.html?id=<?php echo $v['id']; ?>">
<?php echo $v['brandName']; ?>
</a>
</td>
<td>
<?php echo $fw->customer($_SESSION['USERID'])->getAccountManagerbyId($v['accountManager_id']); ?></td>
<td>
<?php echo $v['dateCreated']; ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
You have an undefined or null value at 3 column of 35 row. You should pass some value at that place .

Warning: mysql_query() expects parameter 2 to be resource, null given in C:\xampp\htdocs\3s_shop\admin\pembelian_data.php on line 6 error paging: [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
This is my form distributor data code.
If I click the button before it would be an error
Warning: mysql_query() expects parameter 2 to be resource, null given in C:\xampp\htdocs\3s_shop\admin\pembelian_data.php on line 6
error paging:
What's wrong in the code?
<?php
# UNTUK PAGING (PEMBAGIAN HALAMAN)
$baris = 50;
$hal = isset($_GET['hal']) ? $_GET['hal'] : 0;
$pageSql = "SELECT * FROM pembelian";
$pageQry = mysql_query($pageSql, $koneksidb) or die ("error paging: ".mysql_error());
$jml = mysql_num_rows($pageQry);
$maksData = ceil($jml/$baris);
?>
<table width="800" border="0" cellpadding="2" cellspacing="1" class="table-border">
<tr>
<td colspan="2" align="right"><h1><b>DATA PEMBELIAN</b></h1></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2" align="right"><img src="../images/btn_add_data.png" height="30" border="0" /></td>
</tr>
<tr>
<td colspan="2"><table class="table-list" width="100%" border="0" cellspacing="1" cellpadding="2">
<tr>
<th width="35" align="center" bgcolor="#CCCCCC">No</th>
<th width="250" bgcolor="#CCCCCC">Nama Reseller</th>
<th width="100" bgcolor="#CCCCCC">Tanggal Pembelian</th>
<th width="250" bgcolor="#CCCCCC">Nama Produk</th>
<th width="150" bgcolor="#CCCCCC">Harga Produk </th>
<th width="50" bgcolor="#CCCCCC">Jumlah Produk </th>
<th width="150" bgcolor="#CCCCCC">Total Harga </th>
<td colspan="2" align="center" bgcolor="#CCCCCC"><strong>Tools</strong></td>
</tr>
<?php
$mySql = "SELECT * FROM pembelian ORDER BY nm_reseller, tgl_pembelian, nama_barang, hrg_barang, qty ASC LIMIT $hal, $baris";
$myQry = mysql_query($mySql, $koneksidb) or die ("Query salah : ".mysql_error());
$nomor = $hal;
while ($myData = mysql_fetch_array($myQry)) {
$nomor++;
$Kode = $myData['kd_pembelian'];
// Warna baris data
if($nomor%2==1) { $warna=""; } else {$warna="#F5F5F5";}
?>
<tr bgcolor="<?php echo $warna; ?>">
<td align="center"><?php echo $nomor; ?></td>
<td><?php echo $myData['nm_reseller']; ?></td>
<td><?php echo $myData['tgl_pembelian']; ?></td>
<td><?php echo $myData['nama_barang']; ?></td>
<td><?php echo $myData['hrg_barang']; ?></td>
<td><?php echo $myData['qty']; ?></td>
<td width="44" align="center">Edit</td>
<td width="44" align="center">Hapus</td>
</tr>
<?php } ?>
</table></td>
</tr>
<tr class="selKecil">
<td><b>Jumlah Data :</b> <?php echo $jml; ?> </td>
<td align="right"><b>Halaman ke :</b>
<?php
for ($h = 1; $h <= $maksData; $h++) {
$list[$h] = $baris * $h - $baris;
echo " <a href='?open=Pembelian-Data&hal=$list[$h]'>$h</a> ";
}
?> </td>
</tr>
</table>
I think you are not establishing connection to your database. The second input should be that connection variable. like this:
$link = mysql_connect("localhost","root","");
$koneksidb = mysql_select_db('databaseName', $link);
$pageSql = "SELECT * FROM pembelian";
$pageQry = mysql_query($pageSql, $koneksidb) or die ("error paging: ".mysql_error());

Sending a value to another page

Need your help again. I am trying to create a payroll system for a friend of mine and I am stuck with this line of code.
<table border=6 cellpadding=10 cellspacing=0>
<tr align=center valign=middle>
<td>Employee ID</td>
<td>13th Month</td>
<td>Date Updated</td>
<td>Options</td>
</tr>
<?php
mysql_connect("localhost","root","");
mysql_select_db("db_payroll");
$payroll_sql = mysql_query("SELECT * FROM tbl_payroll");
while ($payroll = mysql_fetch_array($payroll_sql))
{
?>
<tr align=center valign=middle>
<td><?=$payroll['emp_id']?></td>
<td><input type=checkbox name="13thmonth" value="<?=$payroll['emp_id']?>"></td>
<td><?=$payroll['datesaved']?></td>
<td><a href=generate_payroll.php?id=<?=$payroll['emp_id']?>>Generate Payroll</a>
</td>
</tr>
<?php
}
?>
</table>
Actually, this code is running without errors but I wonder how can I send the value of the checkbox to the other page specified by the anchor tag("generate_payroll.php?id=...")
How will I do this? Any answer related to this question is highly appreciated. Thank you!
You need a form first of all...
<form id='myForm' action='generate_payroll.php' method='POST'>
<input type='hidden' name=id value='<?php echo $payroll['emp_id']?;>'>
<tr align=center valign=middle>
<td><?=$payroll['emp_id']?></td>
<td><input type=checkbox name="13thmonth" value="<?=$payroll['emp_id']?>"></td>
<td><?=$payroll['datesaved']?></td>
<td><a href='#' class='nextPage'>Generate Payroll</a>
</td>
</tr>
</form>
Store you $payroll['emp_id'] in a hidden field.
And wire up some jquery so you dont have to have a button for Generate Payroll/
$('.nextPage').click(function(){
$('#myForm').submit();
});
Then on your generate_payroll.php page, simply retrieve...
$id = $_POST['id'];
$month = $_POST['13thmonth'];
Or whatever you wanna do with the POST results
Of course if you dont care about having a button, you can bypass jquery altogether and just do...
<form id='myForm' action='generate_payroll.php' method='POST'>
<input type='hidden' name=id value='<?php echo $payroll['emp_id']?;>'>
<tr align=center valign=middle>
<td><?=$payroll['emp_id']?></td>
<td><input type=checkbox name="13thmonth" value="<?=$payroll['emp_id']?>"></td>
<td><?=$payroll['datesaved']?></td>
<td><input type='submit' value='Generate Payroll'></td>
</tr>
</form>
for assigning value to id you are using ?id=<?=$payroll['emp_id']?> as per me you have to code like this :
?id=<? echo $payroll['emp_id']?>
Try following code and check if URL looks as you want.
<tr align=center valign=middle>
<td><?php echo $payroll['emp_id']?></td>
<td><input type=checkbox name="13thmonth" value="<?php echo $payroll['emp_id']?>"></td>
<td><?php echo $payroll['datesaved']?></td>
<td>Generate Payroll
</td>
</tr>

ajax not returning value

Here is a problem i am having.
Ajax is not willing to return me any values and display it in a message box
Here is my register.php
<?php
//register.php
$username = $_POST['txtusername'];
$response= array();
$response[]= $_POST['txtusername'];
$response[]= $_POST['txtusername'];
echo json_encode($response);
?>
My form is as follows
<div id="divregister">
<div id="display"></div>
<table>
<tr>
<td colspan="3" style='text-align: center;font-weight: bold;'>Register</td>
</tr>
<tr>
<td style='text-align: right;'>First Name</td>
<td class="registerinputfieldscolumn">
<input value= "<?php if(isset($_POST['txtfirstname'])) {echo $_POST['txtfirstname'];} ?>" type="text" name="txtfirstname" id ="txtfirstname" ></td>
<td class="registererrordisplay" id="registererrorfirstname"></td>
</tr>
<tr>
<td style='text-align: right;'>Last Name</td>
<td class="registerinputfieldscolumn">
<input value= "<?php if(isset($_POST['txtlastname'])) echo $_POST['txtlastname']; ?>" type="text" name="txtlastname" id="txtlastname"></td>
<td class="registererrordisplay" id="registererrorlastname"></td>
</tr>
<tr>
<td style='text-align: right;'>E-Mail Address</td>
<td class="registerinputfieldscolumn">
<input value= "<?php if(isset($_POST['txtemail'])) echo $_POST['txtemail']; ?>" type="text" name="txtemail" id="txtemail"></td>
<td class="registererrordisplay" id="registererroremail"></td>
</tr>
<tr>
<td style='text-align: right;'>Username</td>
<td class="registerinputfieldscolumn">
<input value= "<?php if(isset($_POST['txtusername'])) echo $_POST['txtusername']; ?>" type="text" name="txtusername" id ="txtusername"></td>
<td class="registererrordisplay" id="registererrorusername"></td>
</tr>
<tr>
<td style='text-align: right;'>Password</td>
<td class="registererrordisplay"><input type="password" name="txtpassword" id="txtpassword"></td>
<td id="registererrorpassword"></td>
</tr>
<tr>
<td style='text-align: right;'>Confirm</td>
<td class="registerinputfieldscolumn">
<input type="password" name="txtconfirmpassword" id="txtconfirmpassword"></td>
<td></td>
</tr>
<tr>
<td id ="registerdisplay" style='text-align: right;font-size: 12px;color:red;'>dfghjkl</td>
<td style='text-align: right;'>
<input type='button' id ="btnregister" style='width: 60px;height:25px;margin-right:75px;' value="Register"></td>
<td></td>
</tr>
</table>
</div>
Here is the ajax script is as follows.
Am not getting any response in the alert box.
Any help from you guy is greatly appreciated.
<script>
$(document).ready(function(){
$("#btnregister").click(function(){
$.ajax({
type: 'POST',
url: 'register.php',
dataType: 'text',
success: function(data) {alert(data);},
data: {"txtusername": $("#txtusername").val()}
});
});
});
</script>
data: {txtusername: sample}
Where did you define that 'sample' variable? It seems that it doesn't exist so you actually send no value. Then nothing is returned, because there's no data to create response.

Categories