Display dynamic table of checkbox in 3 columns in php - php

I have a database table of subjects. I have a web page that displays the list of subjects in 3 columns so that a user can select subjects using a checkbox. The challenge I have is it takes a long time for the list to display. The code is below:
<?php
$subjects = mysql_query("select id, name from subjects WHERE exam_type_id='0' OR exam_type_id='$exam_type_id' ORDER BY name");
$noOfSubjects = mysql_num_rows($subjects);
?>
<form name="form1" method="post" action="" class="FormCss" onsubmit=" return window.confirm('You are about to submit selected subjects. If you are not sure, cancel');">
<table>
<tr class="FormLabel2">
<td align="left" colspan="3" valign="top"></td>
</tr>
<?php
$i = 1;
while ($subject = mysql_fetch_array($subjects)){
// if it is a value that is supposed to be in the 1st cell of a row, print tr
if ($i==1 || $i==4 || $i==7 || $i== 10){
?>
<tr>
<?php
}
// print a td if value is not suppose to be the last on a row
if ($i%3 !=0){
?>
<td><input type="checkbox" name="subject[]"
value="<?php echo $subject['id']?>" <?php if (isCompulsoryMockSubject($subject['id'])){?> onclick="return false;" onkeydown="return false;" <?php }else{}?> <?php if (isCompulsoryMockSubject($subject['id'])){?> CHECKED="checked" <?php }else{}?> <?php if (hasSelectedSubject($_SESSION['candidate_id'], $exam_type_id, $subject['id'])){?> checked="checked" <?php }?>><?php echo $subject['name'];?>
</td>
<?php
}else{
// end the row
?>
<td><input type="checkbox" name="subject[]"
value="<?php echo $subject['id']?>" <?php if (isCompulsoryMockSubject($subject['id'])){?> onclick="return false;" onkeydown="return false;" <?php }?> <?php if (isCompulsoryMockSubject($subject['id'])){?> CHECKED="checked" <?php }else{}?> <?php if (hasSelectedSubject($_SESSION['candidate_id'], $exam_type_id, $subject['id'])){?> checked="checked" <?php }?>><?php echo $subject['name'];?>
</td>
</tr>
<?php
}
$i++;
}
?>
<tr>
<td width="34%" align="left" valign="top" class="FormLabel2"> </td>
<td width="33%" align="left" valign="top"><span
class="FormLabel2"> <input name="Submit" type="submit"
class="loginInputSubmit" value="Save"> </span></td>
<td width="33%" align="left" valign="top"> </td>
</tr>
<tr>
<td align="left" valign="top" class="FormLabel2"> </td>
<td colspan="2" align="right" valign="top" class="FormLabel2"> </td>
</tr>
</table>
</form>

Related

Removing array [0] in SESSION array error

As a novice in this and I'm struggling with a small project.
I’m putting together a page for INTERNAL orders in a small MTB club. My problem is if there are more than 1 order in the $_SESSION['cart'] and I try to remove the first one [0], then I get “Undefined offset: 0”.
I get that is to do with that I remove the first entry and therefor it can’t be found when it lists the array. I just can’t see how I solve it, I have googled and tried different solutions without result.
Please advice on how to get forward for me..
Below code is made for this purpose only
enter code here <?php
session_start();
if(isset($_POST['submit'])){
$total = $_POST['Amount'] * $_POST['Price'];
$cart=array(
'Amount'=>$_POST['Amount'], //Amoun ordered of clothes
'Size'=>$_POST['Size'], //Size of clothes
'Price'=>$_POST['Price'], //Price of clothes
'Product_id'=>$_POST['Product_id'],//Id of clothes for DB
'Product_Name'=>$_POST['Product_Name'], //Name of clothes
'Product_Total'=>$total //Total price
);
$_SESSION['cart'][]=$cart;
}
if(isset($_GET['Reset'])){
unset($_SESSION['cart']);
header('location:demo.php');
}
if(isset($_GET['remove'])){
$do = $_GET['do'];
unset($_SESSION['cart'][$do]);
//Redirecting After Unset SESSION
header('location:demo.php');
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Session Demo/Test</title>
</head>
<body>
<table width="590" border="4" cellspacing="0" cellpadding="5" align="center" >
<tbody>
<tr>
<td width="373">Name of clothes</td>
<td width="185">
Reset Session</td>
</tr>
</tbody>
</table>
<form method="POST">
<table width="590" border="4" cellspacing="0" cellpadding="5" align="center">
<tbody>
<tr>
<td colspan="4" rowspan="5" align="center"><img src="images/Bike_Jersy_short.jpg" width="200" height="200" alt=""/></td>
<td height="120" colspan="2">
Clothes description
</td>
</tr>
<tr>
<td width="112">Price:</td>
<td>300,00 Kr.</td>
</tr>
<tr>
<td width="112">Amount:</td>
<td width="116">
<input name="Amount" type="number" id="Amount" tabindex="1" value="" size="1" required >
</td>
</tr>
<tr>
<td>Size:</td>
<td>
<select name="Size" id="Size" tabindex="2" required>
<option value=""></option>
<option value="XS">XS</option>
<option value="S">S</option>
<option value="L">L</option>
<option value="XL">XL</option>
<option value="2XL">2XL</option>
<option value="3XL">3XL</option>
<option value="4XL">4XL</option>
<option value="5XL">5XL</option>
<option value="6XL">6XL</option>
</select>
</td>
</tr>
<tr>
<td>Add:</td>
<td>
<input type="submit" name="submit" id="submit" value="Add">
<input name="Price" type="hidden" id="Price" value="300">
<input name="Product_id" type="hidden" id="Product_id" value="1">
<input name="Product_Name" type="hidden" id="Product_Name" value="Name of clothes">
</td>
</tr>
</tbody>
</table>
</form>
<p></p>
<?php
if(empty($_SESSION['cart'])){
?>
<table width="898" border="4" cellspacing="0" cellpadding="5" align="center">
<tbody>
<tr>
<td>You have no items in yet </td>
</tr>
</tbody>
</table>
<?php
}
else{
?>
<table width="900" border="4" cellspacing="0" cellpadding="5" align="center">
<tbody>
<tr>
<td width="109">Name</td>
<td width="31">Amount</td>
<td width="33">Size.</td>
<td width="63">Price</td>
<td width="55">Total</td>
<td width="45">Remove</td>
</tr>
<?php for($i = 0 ; $i < count($_SESSION['cart']) ; $i++) {
?>
<tr>
<td><?php echo $_SESSION['cart'][$i]['Product_Name'];?></td>
<td><?php echo $_SESSION['cart'][$i]['Amount'];?>
</td>
<td><?php echo $_SESSION['cart'][$i]['Size'];?></td>
<td><?php echo $_SESSION['cart'][$i]['Price'];?></td>
<td><?php echo $_SESSION['cart'][$i]['Product_Total'];?></td>
<td>Remove
<?php
}
?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
Use the array_shift() function.
Like so:
$value = array_shift($_SESSION['cart']);
This will remove the first element in the array regardless of the current index. $value is the value of the element that was removed.

how to get user radio button selected values

How to get user selected value , below is my code.
Here are two packages with different prices names etc, shown fine, but what so ever user selected it treats as first one is selected.
PHP/HTML Code
<?php
$sql_pac=mysql_query("select * from package1 where status=0 order by package_id");
$i=0;
while($res_pac=mysql_fetch_array($sql_pac)){
//$products=unserialize(stripslashes($res_pac['products']));
//echo $products[$i];?>
<tr class="RowBGStandard">
<td>
<input name="package" value="<?php echo $res_pac["package_id"] ?>"
<?php echo ($res_pac["package_id"]==2) ? 'checked' : '' ?> type="radio">
<input type="hidden" name="package_volume" value="<?php echo $res_pac["total_bv"] ?>">
<input type="hidden" name="package_price" value="<?php echo $res_pac["total_fees"] ?>">
<input type="hidden" name="package_name" value="<?php echo $res_pac["package_name"] >">
</td>
<td align="right"><strong><!--710--> </strong>
</td>
<td class="col-lg-6 control-label">
<strong><?php echo $res_pac["package_name"] ?></strong>
</td>
<td align="right">
<strong><?php echo Currency.' '.$res_pac["total_fees"] ?></strong>
</td>
</tr>
<tr class="RowBGStandard">
<td colspan="2"></td>
<td colspan="1" class="col-lg-6 control-label">
<em><?php echo $res_pac["description"] ?></em>
</td>
<td>
</td>
</tr>
<tr>
<td colspan="6" height="3"></td>
</tr>
<tr>
<td height="1" colspan="6" bgcolor="black">
</td>
</tr>
<tr>
<td colspan="6" height="3">
</td>
</tr>
<?php $i++;
}
?>
Try this:
<?php
if($res_pac["package_id"] == 2){
$selectedTwo = 1;
} else {
$selectedTwo = 0;
}
?>
<input name="package" value="<?php echo $res_pac['package_id']; ?>" <?php echo ($selectedTwo) ? 'checked' : ''; ?> type="radio">
See, if that works

populate triple drop down in ajax and php in a form

I have managed to populate my three drop down lists in php and ajax. Here is the one part of my code that displays the three dropdown lists:
<?php
echo "<font id=\"categoria\"><select>\n";
echo "<option value='0'>Select the Firm</option> \n" ;
echo "</select></font>\n";
?>
<?php
echo "<font id=\"subcategoria\"><select>\n";
echo "<option value='0'>Select the Claims Hub</option> \n" ;
echo "</select></font>\n";
?>
<?php
echo "<font id=\"subcategoria2\"><select>\n";
echo "<option value='0'>Select the Area</option> \n" ;
echo "</select></font>\n";
?>
My question is: I also have a form on this pages and i want to make the three dropdown lists part of my form in order to post the values into another mysql table. Any suggestions how i can do this? Here is my form part:
<form method="post" align= "right">
<table >
<tr><td> </td>
<tr>
<td>Select the Matter Type: </td>
<td><?php echo $dropdown; ?></td>
</tr>
<tr><td> </td>
<tr>
<td style="text-align: left; ">Active </td>
<td align="left" >
<input type="checkbox" name="active" value="1" />
</td>
</tr>
<tr><td> </td>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Add" align= "right" /></td>
</tr>
</table>
Why not just insert them into the html?
<form method="post" align= "right">
<table >
<tr><td> </td>
<tr>
<td>Select the Matter Type: </td>
<td><?php echo $dropdown; ?></td>
</tr>
<tr>
<td>
<?php echo "<font id=\"categoria\"><select><option value='0'>Select the Firm</option></select></font>";?>
</td>
</tr>
<tr><td> </td>
<tr>
<td style="text-align: left; ">Active </td>
<td align="left" >
<input type="checkbox" name="active" value="1" />
</td>
</tr>
<tr><td> </td>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Add" align= "right" /></td>
</tr>

Multiple checkbox to a sentence before insert to mysql

I' edit this question with my full coding
Let me post my full code here.
Page1.php
<div id="container">
<div class="box">
<form name="form" method="post" action="userbooking2.php">
<table width="" style="border: 0px solid black" align="center">
<tr>
<td><h2>Book a Room Step 1 - Booking Information</h2></td>
</tr>
<tr>
<td>
<table>
<tr>
<td width="150px" class="titlestyle">Booked by</td>
<td width="400px"><span style="text-transform: uppercase;"><?php echo $_SESSION['SESS_FNAME']; ?></span></td>
</tr>
<tr>
<td class="titlestyle">Title</td>
<td><span class="hint--right" data-hint="Hint : Organization meeting"><input type="text" name="booking_title" id="booking_title" maxlength="50" size="50" /></span></td>
</tr>
<tr>
<td class="titlestyle">Date</td>
<td>From <input type="text" name="booking_start_date" id="booking_start_date"/> to <input type="text" name="booking_end_date" id="booking_end_date"/></td>
</tr>
<tr>
<td class="titlestyle">Time</td>
<td>From <input name="booking_start_time" id="booking_start_time" value="8:00 AM" /> to <input name="booking_end_time" id="booking_end_time" value="8:30 AM"/></td>
</tr>
<tr>
<td class="titlestyle">Room</td>
<td><select name="room_type" id="room_type">
<option value="none">--Select--</option>
<option value="Meeting Room">Meeting Room</option>
<option value="Lecture Room">Lecture Room</option>
<option value="Computer Lab">Computer Lab</option>
</select>
</td>
</tr>
<tr>
<td class="titlestyle">Participant</td>
<td><span class="hint--right" data-hint="Hint : Insert number of participants"><input type="text" name="room_participant" id="room_participant" maxlength="50" size="3" /></span> Persons</td>
</tr>
<tr>
<td class="titlestyle">Equipment</td>
<td><table>
<tr>
<td><input type="checkbox" name="room_facility[]" value="Audio System" />Audio System</td>
<td><input type="checkbox" name="room_facility[]" value="Projector" />Projector</td>
<td><input type="checkbox" name="room_facility[]" value="Video Conferencing" />Video Conferencing</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="titlestyle">Food & Baverages</td>
<td><table>
<tr>
<td><input type="checkbox" name="room_food[]" value="Breakfast" />Breakfast</td>
<td><input type="checkbox" name="room_food[]" value="Tea Break" />Tea Break</td>
<td><input type="checkbox" name="room_food[]" value="Lunch" />Lunch</td>
</tr>
<tr>
<td><input type="checkbox" name="room_food[]" value="High Tea" />High Tea</td>
<td><input type="checkbox" name="room_food[]" value="Dinner" />Dinner</td>
<td><input type="checkbox" name="room_food[]" value="No" />No, thanks</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="titlestyle">Additional Info</td>
<td><span class="hint--right" data-hint="Hint : ."><textarea name="booking_desc" id="booking_desc" style="max-width:400px;" rows="4" cols="50"></textarea></span></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" name="Submit" value="Next" /></td>
</tr>
</table>
</form>
</div>
Page2.php
<?php
//now, let's register our session variables
session_register('fname');
session_register('booking_title');
session_register('booking_start_date');
session_register('booking_end_date');
session_register('booking_start_time');
session_register('booking_end_time');
session_register('room_participant');
session_register('room_type');
session_register('booking_facility');
session_register('booking_food');
session_register('booking_desc');
//finally, let's store our posted values in the session variables
$_SESSION['fname'] = $_SESSION['SESS_FNAME'];
$_SESSION['booking_title'] = $_POST['booking_title'];
$_SESSION['booking_start_date'] = $_POST['booking_start_date'];
$_SESSION['booking_end_date'] = $_POST['booking_end_date'];
$_SESSION['booking_start_time'] = $_POST['booking_start_time'];
$_SESSION['booking_end_time'] = $_POST['booking_end_time'];
$_SESSION['room_participant'] = $_POST['room_participant'];
$_SESSION['room_type'] = $_POST['room_type'];
$_SESSION['room_facility'] = /* WHAT TO SEND HERE? */
$_SESSION['room_food'] = $_POST['room_food'];
$_SESSION['booking_desc'] = $_POST['booking_desc'];?>
// Other form process here
Page3.php
<div id="container">
<div class="box">
<form name="form" method="post" action="">
<table width="" style="border: 0px solid black" align="center">
<tr>
<td><h2>Book a Room Step 3 - Final</h2></td>
</tr>
<tr>
<td>
<table>
<tr>
<td width="150px" class="titlestyle">Booked by</td>
<td width="400px" ><span style="text-transform: uppercase;"><?php echo $_SESSION['SESS_FNAME']; ?></span></td>
</tr>
<tr>
<td class="titlestyle">Title</td>
<td><?php echo $_SESSION['booking_title']; ?></td>
</tr>
<tr>
<td class="titlestyle">Date</td>
<td>From <?php echo $_SESSION['booking_start_date']; ?> to <?php echo $_SESSION['booking_end_date']; ?></td>
</tr>
<tr>
<td class="titlestyle">Time</td>
<td>From <?php echo $_SESSION['booking_start_time']; ?> to <?php echo $_SESSION['booking_end_time']; ?></td>
</tr>
<tr>
<td class="titlestyle">Room</td>
<td><?php echo $_SESSION['room_name']; ?></td>
</tr>
<tr>
<td class="titlestyle">Participant</td>
<td><?php echo $_SESSION['room_participant']; ?> Persons</td>
</tr>
<tr>
<td class="titlestyle">Equipment</td>
<td><?php echo $_SESSION['room_facility']; ?></td>
</tr>
<tr>
<td class="titlestyle">Food & Baverages</td>
<td><?php echo $_SESSION['room_food']; ?></td>
</tr>
<tr>
<td class="titlestyle">Additional Info</td>
<td><?php echo $_SESSION['booking_desc']; ?></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" name="Submit" value="Submit" /></td>
</tr>
</table>
</form>
</div>
In page3.php, i want to view this kind of string "I book Value1, Value2" before store it into the database
Use:
$string = implode(', ', isset($_REQUEST['room_facility']) ? $_REQUEST['room_facility'] : array());
echo 'I need ' . $string;
Then just insert into the database as normal

I want to submit this form into DB php

i want to insert this below given form data to database, but i dont know how to do this, it is about inserting product to multiple warehouse with different quantities, when checkbox against the warehouse name is checked than the quantity textbox appears.
and this is my PHP code
<form name="form1" method="post" action="product_insert.php" enctype="multipart/form-data">
<table>
<tr>
<td width="274" align="right" height="25"><strong>Product Name :</strong></td>
<td><input type="text" name="wproname" value="" /></td>
</tr>
<tr>
<td width="274" align="right" height="25"><strong>Select Warehouse :</strong></td>
<td width="500"><table style="border:none">
<tr >
<td> Select </td>
<td> Name </td>
<td> Quantity </td>
</tr>
<?php
$sql="select * from tbl_warehouse where w_flag='1'";
$result=ExecuteGetRows($sql);
$num_rows=count($result); ?>
<?php for($i=0;$i<$num_rows;$i++){ ?>
<tr>
<td><input type="checkbox" name="chk<?php echo $result[$i]['w_id'];?>" value="<?php echo $result[$i]['w_id'];?>" id="chk<?php echo $result[$i]['w_id'];?>" onChange="display<?php echo $result[$i]['w_id'];?>();" />
</td>
<td><?php echo $result[$i]['w_name'];?></td>
<td><input type="text" name="qty<?php echo $result[$i]['w_id'];?>" id="qty<?php echo $result[$i]['w_id'];?>" style="display:none" />
</td>
</tr>
<script>
function display<?php echo $result[$i]['w_id'];?>()
{
if(document.getElementById("chk<?php echo $result[$i]['w_id'];?>").checked)
{
document.getElementById("qty<?php echo $result[$i]['w_id'];?>").style.display="block";
}
if(!document.getElementById("chk<?php echo $result[$i]['w_id'];?>").checked)
{
document.getElementById("qty<?php echo $result[$i]['w_id'];?>").style.display="none";
}
}
</script>
<?php } ?>
</table></td>
</tr>
<tr>
<td align="right"></td>
<td width="296"><input type="submit" name="Submit" value="Add New" align="middle" class="button login_btn"/></td>
</tr>
</table>
</form>
And I am using this php code for inserting data to the database
$sqls=mysql_query("select * from tbl_warehouse");
while($result=mysql_fetch_array($sqls)) {
$w = $result['w_id'];
echo $_POST['chk'.$w];
foreach($_POST['chk'.$w] as $key=>$val) {
echo $_POST['chk'.$w];
$sql = "INSERT INTO tbl_product(p_id,w_id,p_name,p_qty) values ('','".$wid."','".$wproname."','".$qty."')";
mysql_query($sql);
}
}
You don't need the leading comma in your values list in your insert statement. So change this:
"INSERT INTO tbl_product(p_id,w_id,p_name,p_qty) values ('','".$wid."','".$wproname."','".$qty."')";
To this:
"INSERT INTO tbl_product(p_id,w_id,p_name,p_qty) values ('".$wid."','".$wproname."','".$qty."')";

Categories