How would I go about fixing my PHP calculator - php

Hey guys im working on a PHP calculator for school and I cant seem to get it to work. I am not good with PHP and cant figure it out. When I run it under membership and calculate total no matter what option I choose it automatically switches it to the Gold membership. Any ideas? Below is the code:
<?php
$tax = 0;
$total = 0;
$membership = "gold";
$tennis = "no";
$racquetball = "no";
$golf = "no";
$child_care = "no";
$personal_trainer = "no";
$swimming_pool = "no";
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!empty($_POST["tax"])) {
$tax = test_input($_POST["tax"]);
}
if ($membership == "basic") {
$total = 80;
}
elseif ($membership == "platinum") {
$total = 100;
}
else {
$total = 120;
}
if (isset($_POST["tennis"])) {
$tennis = "yes";
$total = $total + 15;
}
if (isset($_POST["racquetball"])) {
$racquetball = "yes";
$total = $total + 20;
}
if (isset($_POST["golf"])) {
$golf = "yes";
$total = $total + 25;
}
if (isset($_POST["child_care"])) {
$child_care = "yes";
$total = $total + 15;
}
if (isset($_POST["personal_trainer"])) {
$personal_trainer = "yes";
$total = $total + 20;
}
if (isset($_POST["swimming_pool"])) {
$swimming_pool = "yes";
$total = $total + 25;
}
$total = $total + $total * $tax;
$total = round($total, 2);
}
?>
<html>
<head>
<title>Health Club (PHP)</title>
</head>
<body style="padding: 30px">
<h2>Health Club (PHP)</h2>
Franklin Covington <p>
<form method="post" name="healthClubForm" id="healthClubForm"
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div style="float:left; width:130px; background-color:pink;">
<dl>
<dt>Membership
<dt><input type="radio" name="membership" onchange="reloadForm"
<?php if (isset($membership) && $membership=="basic") echo "checked";?>
value="basic"> Basic
<dt><input type="radio" name="membership" onchange="reloadForm"
<?php if (isset($membership) && $membership=="platinum") echo "checked";?>
value="platinum"> Platinum
<dt><input type="radio" name="membership" onchange="reloadForm"
<?php if (isset($membership) && $membership=="gold") echo "checked";?>
value="Gold"> Gold
</dl>
</div>
<div style="float:left; width:180px; background-color:yellow;">
<dl>
<dt>Additional Charges (1)
<dt><input type="checkbox" onchange="reloadForm"
<?php if (isset($tennis) && $tennis=="yes") echo "checked";?>
name="tennis"> Tennis
<dt><input type="checkbox" onchange="reloadForm"
<?php if (isset($racquetball) && $racquetball=="yes") echo "checked";?>
name="racquetball"> Racquetball
<dt><input type="checkbox" onchange="reloadForm"
<?php if (isset($golf) && $golf=="yes") echo "checked";?>
name="golf"> Golf
</dl>
</div>
<div style="float:left; width:180px; background-color:red;">
<dl>
<dt>Additional Charges (2)
<dt><input type="checkbox" onchange="reloadForm"
<?php if (isset($child_care) && $child_care=="yes") echo "checked";?>
name="child_care"> Child Care
<dt><input type="checkbox" onchange="reloadForm"
<?php if (isset($personal_trainer) && $personal_trainer=="yes") echo "checked";?>
name="personal_trainer"> Personal Trainer
<dt><input type="checkbox" onchange="reloadForm"
<?php if (isset($swimming_pool) && $swimming_pool=="yes") echo "checked";?>
name="swimming_pool"> Swimming Pool
</dl>
</div>
<div style="clear:both"> </div>
<div style="float:left; width:150px; background-color:lightcoral;">
<dl>
<dt><input type="submit" value="Calculate Total">
<dt><input type="submit" value="Clear">
</dl>
</div>
<div style="float:left; background-color:lightgreen;">
<dl>
<dt>Tax: <input type="text" onchange="reloadForm" name="tax" value="<?php echo $tax;?>" size="10">
<dt>Total: <input type="text" name="total" value="<?php echo $total;?>" size="10">
</dl>
</div>
<script>
function reloadForm() {
document.getElementById("healthClubForm").submit();
}
</script>`your text`
</form>
</body>
</html>
I looked at the I have for gold and it doesnt look like I did anything wrong.

Because you put the value of membership as gold in line 4. You missed calling the post request of membership after submitting
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!empty($_POST["tax"])) {
$tax = test_input($_POST["tax"]);
}
$membership=$_POST["membership"];
if ($membership == "basic") {
$total = 80;
}
add this line $membership=$_POST["membership"];

Related

Not able to update 2 items using php and mysqli

I have a table where I want to update data in 2 rows.
Heres my code:
<?php
include("includes/conn.php");
session_start();
if(!isset($_SESSION['username']))
{
header('Location: login.php');
}
else{
?>
<?php
$page_title = 'Update Order';
include("includes/header.inc.php");
?>
<?php if( $_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['update-order']) ){
if( !empty($_POST['productname']) && !empty($_POST['quantity']) && !empty($_POST['price']) && !empty($_POST['amount']) ){
$total = 0;
$pid = mysqli_real_escape_string($db,$_POST['pid']);
$advance = mysqli_real_escape_string($db,$_POST['advance_paid']);
$dod = mysqli_real_escape_string($db,$_POST['dod']);
$dod = date('Y-m-d', strtotime($dod));
$mop = mysqli_real_escape_string($db,$_POST['mop']);
for($i = 0; $i<count($_POST['productname']); $i++)
{
$total = $total + $_POST['amount'][$i];
$query = "UPDATE `invoice` SET product_name = (select name from item where id = '{$_POST['productname'][$i]}'), quantity = '{$_POST['quantity'][$i]}', price = '{$_POST['price'][$i]}', discount = '{$_POST['discount'][$i]}', amount = '{$_POST['amount'][$i]}', dod = '{$dod}', added_by = '{$_SESSION['id']}' WHERE id = '{$pid}' ";
mysqli_query($db, $query);
$update_stock = "UPDATE `item` SET added_by = '{$_SESSION['id']}', total_stock = total_stock + '{$_SESSION['quant']}' - '{$_POST['quantity'][$i]}' WHERE id = '{$_POST['productname'][$i]}'";
mysqli_query($db, $update_stock);
}
$query_orders = "UPDATE `orders` SET amount = '{$total}', dod = '{$dod}', mop = '{$mop}', advance = '{$advance}', added_by = '{$_SESSION['id']}' WHERE order_id = '{$_POST['orderid']}' ";
mysqli_query($db, $query_orders);
}
}
?>
<div id="alteration-form">
<div style="width: 90%; margin: 0 auto;">
<form method="GET" action="">
<label for="update_orderno" >Order NO.</label>
<input type="text" name="orderno" class="input-field" id="update_orderno" />
<input type="submit" name="find-order" value="Search Order" class="btn-all-submit" id="btn-order" />
</form>
</div>
</div>
<?php if( $_SERVER["REQUEST_METHOD"] == "GET" ){ ?>
<?php
if( !empty($_GET['orderno']) ){
$orderno = $_GET['orderno'];
$sql_cust = "SELECT cust_id FROM `orders` where order_id = '$orderno' ";
$result_cust = mysqli_query($db, $sql_cust);
if( mysqli_num_rows($result_cust) > 0 ){
while( $row_cust = mysqli_fetch_array($result_cust, MYSQLI_ASSOC) ){
$cust_id = $row_cust['cust_id'];
}
$sql_name = "SELECT * FROM `users` WHERE id = '$cust_id' ";
$result_name = mysqli_query($db, $sql_name);
if( mysqli_num_rows($result_name) > 0 ){
while( $row_name = mysqli_fetch_array($result_name, MYSQLI_ASSOC) ){
$name = $row_name['name'];
$phone = $row_name['phone'];
$address = $row_name['address'];
$city = $row_name['city'];
$state = $row_name['state'];
}
}
}
?>
<div class="result-table-div">
<div class="divRow">
<div class="divCell" ><strong>Name:</strong></div>
<div class="divCell" ><?php echo $name; ?></div>
<?php if(!empty($phone)){ ?>
<div class="divCell" ><strong>Phone:</strong></div>
<div class="divCell" ><?php echo $phone; ?></div>
<?php } ?>
</div>
<div class="divRow">
<?php if(!empty($address)){ ?>
<div class="divCell" ><strong>Address:</strong></div>
<div class="divCell" ><?php echo $address; ?></div>
<?php } ?>
<?php if(!empty($city)){ ?>
<div class="divCell" ><strong>City:</strong></div>
<div class="divCell" ><?php echo $city; ?></div>
<?php } ?>
</div>
<div class="divRow">
<?php if(!empty($state)){ ?>
<div class="divCell" ><strong>State:</strong></div>
<div class="divCell" ><?php echo $state; ?></div>
<?php } ?>
</div>
</div>
<div class="result-table-div">
<form method="POST" action="">
<div class="divRow">
<div class="divCell" ><strong>Date of Delivery:</strong></div>
<div class="divCell" ><input type="text" name="dod" id="dod" value="<?php echo date("m/d/Y"); ?>"/></div>
<div class="divCell" ><strong>Mode of Payment:</strong></div>
<div class="divCell" >
<select name="mop">
<option value="cash">Cash</option>
<option value="card/cheque/online banking">Card/Cheque/Online Banking</option>
</select>
</div>
</div>
<table id="invoice-table">
<thead>
<tr>
<th>S No.</th>
<th>Item</th>
<th>Quantity</th>
<th>Price</th>
<th>Discount</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM invoice where order_id = '$orderno' ";
$result = mysqli_query($db, $sql);
if( mysqli_num_rows($result) > 0 ){
$count = 1;
while( $row = mysqli_fetch_array($result,MYSQLI_ASSOC) ){
$id = $row['id'];
$product_name = $row['product_name'];
$quantity = $row['quantity'];
$price = $row['price'];
$discount = $row['discount'];
$amount = $row['amount'];
$_SESSION['cust_id'] = $row['cust_id'];
$_SESSION['quant'] = $quantity;
?>
<tr>
<input type="hidden" name="pid" value="<?php echo $id; ?>" />
<td><?php echo $count++; ?></td>
<!--<td><input type="text" name="productname[]" value="?php echo $product_name ?>"></td>-->
<td>
<select name="productname[]">
<option value="1" <?php if( isset($product_name) && strtolower($product_name) == "bags" ) echo "selected"; ?> >Bags</option>
<option value="2" <?php if( isset($product_name) && strtolower($product_name) == "shoes" ) echo "selected"; ?> >Shoes</option>
<option value="3" <?php if( isset($product_name) && strtolower($product_name) == "suit" ) echo "selected"; ?> >Suit</option>
<option value="4" <?php if( isset($product_name) && strtolower($product_name) == "belts" ) echo "selected"; ?> >Belts</option>
<option value="5" <?php if( isset($product_name) && strtolower($product_name) == "t-shirts" ) echo "selected"; ?> >T-shirts</option>
<option value="6" <?php if( isset($product_name) && strtolower($product_name) == "others" ) echo "selected"; ?> >Others</option>
</select>
</td>
<td><input type="text" class="quantity" name="quantity[]" value="<?php echo $quantity ?>"></td>
<td><input type="text" class="price" name="price[]" value="<?php echo $price ?>"></td>
<td><input type="text" class="discount" name="discount[]" value="<?php echo $discount ?>"></td>
<td><input type="text" class="amount" name="amount[]" value="<?php echo $amount ?>"></td>
</tr>
<?php
}
?>
<input type="hidden" name="orderid" value="<?php echo $orderno; ?>">
<?php
}
?>
</tbody>
</table>
<p style="text-align: center;">Advanced Paid: <input type="text" name="advance_paid" /></p>
<div class="btn-div">
<input type="submit" class="btn-all-submit" name="update-order" value="Update Order">
</div>
</form>
</div>
<?php } } ?>
<script>
$('body').delegate('.quantity,.price,.discount','keyup',function()
{
var tr=$(this).parent().parent();
var qty=tr.find('.quantity').val();
var price=tr.find('.price').val();
var dis=tr.find('.discount').val();
var amt =(qty * price)-(qty * price *dis)/100;
tr.find('.amount').val(amt);
});
</script>
<?php include("includes/footer.inc.php"); ?>
<?php } ?>
I am using for loop to update the data but if I have 2 items to update, it is only updating second item and not the first one. Can someone please help me with this.
Problem occurs while updating invoice table.
I found the solution for my problem.
Query should be
$query = "UPDATE `invoice` SET product_name = (select name from item where id = '{$_POST['productname'][$i]}'), quantity = '{$_POST['quantity'][$i]}', price = '{$_POST['price'][$i]}', discount = '{$_POST['discount'][$i]}', amount = '{$_POST['amount'][$i]}', dod = '{$dod}', added_by = '{$_SESSION['id']}' WHERE id = '{$_POST['pid'][$i]}' ";
mysqli_query($db, $query);

how to make result of multiple choice in side of the answer

i want to make a multiple choice that if we choose wrong answer, then a text 'incorrect' will be displayed in side of the answer that we choose and if we choose right answer, then a text 'correct' will be displayed in side of the answer that we choose. i using radio button. please help.
this is my index.php:
<?php
include 'koneksi.php';
session_start();
$query = mysql_query("select * from t_soal") or die (mysql_error());
//$_SESSION['soal'] = mysql_fetch_array($query);
$_SESSION['soal'] = array();
$_SESSION['no'] = 1;
$_SESSION['score'] = 0;
$_SESSION['option'] = array();
$_SESSION['jawab'] = array();
$i=0;
while($row = mysql_fetch_assoc($query)){
$_SESSION['soal'][] = $row;
$_SESSION['option'][] = array($_SESSION['soal'][$i]['a'], $_SESSION['soal'][$i]['b'], $_SESSION['soal'][$i]['c'], $_SESSION['soal'][$i]['d']);
$i++;
}
if(isset($_SESSION['soal'])){
header("location:test.php");
}
this is test.php:
<?php
session_start();
$soal = $_SESSION['soal'];
$no = $_SESSION['no'];
if(isset($_POST['next'])){
$_SESSION['jawab'][] = $_POST['option'];
if($_POST['option'] == $soal[$no-2]['kunci']){
$_SESSION['score'] = $_SESSION['score'] + 10;
}
}
if(isset($soal[$no-1])){
?>
<!DOCTYPE html>
<html>
<head>
<title>Latihan Soal</title>
</head>
<body>
Kembali ke soal 1
<form action="" method="POST">
<p>
<?php
echo $no.". "; $_SESSION['no']++;
echo $soal[$no-1]['soal'];
$jawaban = $_SESSION['option'][$no-1];
shuffle($jawaban);
?>
</p>
<?php
for ($i=0; $i < 4; $i++) {
?>
<input type="radio" name="option" value="<?php echo $jawaban[$i]; ?>" required/> <?php echo $jawaban[$i]; ?></br>
<?php
}
?>
<input type="submit" name="next" value="next">
</form>
</body>
</html>
<?php
}else{
header("location:result.php");
}
?>
Assuming all your script is working already, then:
In PHP:
You will need to assign a new parameter on this script:
if($_POST['option'] == $soal[$no-2]['kunci']){
$_SESSION['score'] = $_SESSION['score'] + 10;
}
to
if($_POST['option'] == $soal[$no-2]['kunci']){
$_SESSION['score'] = $_SESSION['score'] + 10;
$_SESSION['correctAnswer'] = $soal[$no-2]['kunci']; // add sthing like this
}
and then, from
for ($i=0; $i < 4; $i++) {
?>
<input type="radio" name="option" value="<?php echo $jawaban[$i]; ?>" required/> <?php echo $jawaban[$i]; ?></br>
<?php
}
to
for ($i=0; $i < 4; $i++) {
?>
<input type="radio" name="option" value="<?php echo $jawaban[$i]; ?>" required/> <?php echo $jawaban[$i]; ?></br>
<?php
echo $jawaban[$i] == $_SESSION['correctAnswer'] ? " correct" : " incorrect";
}
In JS: (much easier)
HTML files will looks like this:
<div class="correctAnswer">
<input type="radio" name="theanswer" id="choice1" />
<label for="choice1">Choice 1</label>
</div>
<div class="incorrectAnswer">
<input type="radio" name="theanswer" id="choice2" />
<label for="choice2">Choice 2</label>
</div>
and the JS will looks like this:
$(".correctAnswer").click(function() {
$(".checks").remove();
$(this).append("<span class='checks'> correct</span>");
});
$(".incorrectAnswer").click(function() {
$(".checks").remove();
$(this).append("<span class='checks'> incorrect</span>");
});
If you want a working example, please check this fiddle link here.
Hope it helps !

Add-to-cart coding is that correct with form?

<?php
session_start();
include("conn.php");
$action = $_POST['action'];
$user = $_SESSION['username'];
if(empty($user)){
echo"<script>alert('Please log in!');window.location='Log In.php';</script>";
exit;
}
if($action == 'add'){
$cart_arr = array(
'foodID'=>$_POST['foodID'],
'order_num'=>$_POST['order_num'],
'food_type'=>$_POST['food_type'],
);
$cart_session = $_SESSION['cart_'.$user];
if(empty($cart_session)){
$cart_session[$cart_arr['foodID']] = $cart_arr;
} else if(!empty($cart_session[$cart_arr['foodID']])){
$cart_session[$cart_arr['foodID']]['order_num']+=$cart_arr['order_num'];
} else {
echo $cart_session[$cart_arr['foodID']] = $cart_arr;
}
$_SESSION['cart_'.$user] = $cart_session;
} else if($action == 'clear'){
$_SESSION['cart_'.$user]=array();
echo"<script>alert('Shopping cart is empty, return home!');window.location='homepage.php';</script>";
exit;
} else if($action == 'change'){
$temp_cart = $_SESSION['cart_'.$user];
foreach($temp_cart as $k=>$v){
if($_POST['goods_'.$k]!= $v['order_num']){
$temp_cart[$k]['order_num'] = $_POST['goods_'.$k];
}
if($_POST['goods_'.$k] == 0){
unset($temp_cart[$k]);
}
}
$_SESSION['cart_'.$user] = $temp_cart;
}
if(empty($_SESSION['cart_'.$user])){
echo"<script>alert('Shopping cart is empty, please add some orders!');window.location = 'homepage.php';</script>";
exit;
}
$goods_id = array();
$cart = $_SESSION['cart_'.$user];
$v['food_type'] = $_POST['food_type'];
foreach($cart as $k=>$v){
$goods_id[$v['foodID']] = $v['foodID'];
}
$goods_id_str = implode(",",$goods_id);
mysql_query("set names utf8");
$sql = "select * from foodmenu where foodID IN (".$goods_id_str.")";
$query = mysql_query($sql);
$cart_goods = array();
while($arr = mysql_fetch_array($query)){
$cart_goods[$arr['foodID']] = $arr;
}
foreach($cart as $k=>$v){
$cart[$k]['food_name'] = $cart_goods[$k]['food_name'];
$cart[$k]['food_img'] = str_replace("../","",$cart_goods[$k]['food_img']);
$cart[$k]['food_price'] = $cart_goods[$k]['food_price'];
$cart[$k]['food_description'] = $_POST['food_description'];
}
?>
May I know is that this coding correct?
Because it shows blank page when it click on the button on previous php for add-to-cart purpose and it just shows normal header at the top.
I will attach form to access this php.
<div class="detailtop">
<?php
$result = mysql_query("SELECT * FROM foodmenu where foodID = '$foodID'");
while($row=mysql_fetch_array($result)){
?>
<dl>
<dt>
<img src="<?php echo $row["food_img"];?>" /> </dt>
<dd>
<form action="order.php" method="get" name="send" onSubmit="return Check()" enctype="multipart/form-data">
<h3><?php echo $row["food_name"];?></h3>
<div class="detailtips">
<?php echo $row["food_description"];?>
</div>
<p><span>Restaurant:</span><strong><?php echo $row["restaurant_name"];?></strong></p>
<p><span>Type :</span><strong><?php echo $row["food_type"];?></strong></p>
<p><span>Price :</span>RM <strong><?php echo $row["food_price"];?><input name="num" type="hidden" class="num" value="<?php echo $row["food_price"];?>" /></strong></p>
<div class="order" style=" padding-top:20px; padding-left:20px;">
<input name="id" type="hidden" value="<?php echo $row["foodID"];?>" />
<input name="" type="submit" value="" class="ordersubmit" style=" margin-left:30px; margin-top:20px;">
</div>
</form>
</dd>
</dl>
<?php }?>
</div>

How to output all checkbox value

hello please help me i have a problem outputting all the values of checkbox it outputs only one i need to show all the checked checkbox and output them please help me here is the code it only shows one whenever i checked them all i need this
<html>
<head>
<title>Cake Form</title>
<link rel="stylesheet" href="cakeform.css">
</head>
<body>
<?php
$nameErr = $addErr = $phoneErr = $scake = $flavorcake = $fill = "";
$name = $address = $phone = $rcake = $fillr = $cakeflavor = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["address"])) {
$addErr = "Email is required";
} else {
$address = test_input($_POST["address"]);
}
if (empty($_POST["phone"])) {
$phoneErr = "Gender is required";
} else {
$phone = test_input($_POST["phone"]);
}
if(isset($_POST['SelectedCake'])){
$x=$_POST['SelectedCake'];
}
if(isset($_POST['CakeFlavor'])){
$y=$_POST['CakeFlavor'];
}
if(isset($_POST['Filling'])){
$z=$_POST['Filling'];
}
if(empty($x)){
$scake='Select one Cake';
}else{
$rcake= $x;
}
if(empty($y) OR $y == 'Flavor'){
$flavorcake='Select one flavor';
}else{
$cakeflavor= $y;
}
if(empty($z)){
$fill='Select at least one Fillings';
}else{
foreach($z as $item){
$fillr=$item;
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<div id="wrap">
<div class="cont_order">
<fieldset>
<legend>Make your own Cake</legend>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<h4>Select size for the Cake:</h4>
<input type="radio" name="SelectedCake" value="Round6">Round cake 6" - serves 8 people</br>
<input type="radio" name="SelectedCake" value="Round8">Round cake 8" - serves 12 people</br>
<input type="radio" name="SelectedCake" value="Round10">Round cake 10" - serves 16 people</br>
<input type="radio" name="SelectedCake" value="Round12">Round cake 12" - serves 30 people</br>
<span class="error">*<?php echo $scake;?></span>
<h4>Select a Cake Flavor: </h4>
<select name="CakeFlavor">
<option value="Flavor" selected="selected">Select Flavor</option>
<option value="Carrot" >Carrot</option>
<option value="Chocolate" >Chocolate</option>
<option value="Banana" >Banana</option>
<option value="Red Velvet" >Red Velvet</option>
<option value="Strawberry" >Strawberry</option>
<option value="Vanilla" >Vanilla</option>
<option value="Combo" >Combo</option>
</select>
<span class="error">*<?php echo $flavorcake;?></span>
<h4>Select Fillings:</h4>
<label><input type="checkbox" name="Filling[]" value="Cream"/>Cream</label><br>
<label><input type="checkbox" name="Filling[]" value="Fudge"/>Fudge</label><br>
<label><input type="checkbox" name="Filling[]" value="Ganache"/>Ganache</label><br>
<label><input type="checkbox" name="Filling[]" value="Hazelnut"/>Hazelnut</label><br>
<label><input type="checkbox" name="Filling[]" value="Mousse"/>Mousse</label><br>
<label><input type="checkbox" name="Filling[]" value="Pudding"/>Pudding</label><br>
<span class="error">*<?php echo $fill;?></span>
</fieldset>
</div>
<div class="cont_order">
<fieldset>
<legend>Contact Details</legend>
<label for="name">Name</label><br>
<input type="text" name="name" id="name"><span class="error">*<?php echo $nameErr;?></span>
<br>
<label for="address">Address</label><br>
<input type="text" name="address" id="address"><span class="error">*<?php echo $addErr;?></span>
<br>
<label for="phonenumber">Phone Number</label><br>
<input type="text" name="phone" id="phone"><span class="error">*<?php echo $phoneErr;?></span><br>
</fieldset>
<input type="submit" name="submitted" id="submit" value="Submit"/>
</div>
</form>
<div class="cont_order">
<?php
echo $name.'<br>';
echo $address.'<br>';
echo $phone.'<br>';
echo $rcake.'<br>';
echo $cakeflavor.'<br>';
echo $fillr.'<br>';
?>
</div>
</div>
</body>
</html>
You can do this:
var_dump($_POST['Filling']);
Or just this:
<?php
if(!empty($_POST['Filling'])) {
foreach($_POST['Filling'] as $check) {
echo $check;
}
}
?>
Tell me if it works =)
Fact is, you accept a list for filling.
In your code you do this :
foreach($z as $item){
$fillr=$item;
}
Replace it by :
$fillr = '';
foreach($z as $item){
$fillr .= '- ' . $item . '<br/>';
}
It should work better.
For comment :
#so let's say $z is an array :
$z = [1,2,3,4];
#then you say, for each element of the array as you will call $item, I do something
foreach($z as $item){
$fillr=$item; #here you say : for each element of $z (1,2,3,4), I put this value into $fillr.
}
#so let's do the loop together.
# First pass, $item == 1, so $fillr == 1.
# Second pass, $item == 2, so $fillr == 2.
# ...
# Last pass, $item == 4, so $fillr == 4.
# so then when you show $fillr it's the last item of the list (here 4).
# using PHP, .= means concatenate.
# for instance :
$a = "hello ";
$a .= "world";
echo $a;
> hello world
So basically, what I'm doing is :
Having an empty string I'll call $fillr.
For every element of the list,
Append "the_element plus a <br/>" to $fillr
so then when you output $fillr it looks sexy.
Do you get it bud?

PHP Update multiple rows in MySQL

I have this PHP/HTML Code that is selecting data from a MySQL Database:
<?php
$sql3="SELECT * from property_images where property_seq = '".$property["sequence"]."' ";
$rs3=mysql_query($sql3,$conn);
while($property_img=mysql_fetch_array($rs3))
{
?><tr>
<td colspan="2"><img src="http://domain.co.uk/img/property-images/<?php echo $property_img["image"]; ?>" width="80px" height="80px" /></td>
<td colspan="2"><input type="checkbox" name="image1" value="Y" <?php if($property_img["image1"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image2" value="Y" <?php if($property_img["image2"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image3" value="Y" <?php if($property_img["image3"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image4" value="Y" <?php if($property_img["image4"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image5" value="Y" <?php if($property_img["image5"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image6" value="Y" <?php if($property_img["image6"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image7" value="Y" <?php if($property_img["image7"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image8" value="Y" <?php if($property_img["image8"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image9" value="Y" <?php if($property_img["image9"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image10" value="Y" <?php if($property_img["image10"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image11" value="Y" <?php if($property_img["image11"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image12" value="Y" <?php if($property_img["image12"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image13" value="Y" <?php if($property_img["image13"] == 'Y') { echo 'checked="checked"'; } ?> /></td>
</tr><?php
}
?>
each row, has its own image with the columns image1 - image13
i want to update the table with the checkboxes that are checked and unchecked on the form update
how is this possible?
Thanks
If you mean that you want to update the $property_img["imagexx"] val on db depending on the user click on the checkbox you must use ajax.
Fire an event on triggering each checkbox and send the value to a php page that update the php.
Jquery Ajax function can help you on this task.
L
name all your checkboxes images[]
<input type="checkbox" name="images[]" value="1" />
<input type="checkbox" name="images[]" value="2" />
<input type="checkbox" name="images[]" value="3" />
<input type="checkbox" name="images[]" value="4" />
You can then update with PHP :
<?php
$q = "UPDATE property_images SET";
foreach($_POST["images"] as $image) {
$q .= " image" . $image ." = 'Y', ";
}
$q .= " property_seq = '".$property["sequence"]."' WHERE property_seq = '".$property["sequence"]."'";
mysql_query($q);
?>
First of all, mysql_ functions are deprecated, please google mysqli_ or PDO, mysql_ won't be supported on future versions, and is unsafe, etc
Your html output could be much simpler with a loop, also have an eye on putting the sequence number on a hidden field or something first:
<?php
$sql3="SELECT * from property_images where property_seq = '".$property["sequence"]."' ";
$rs3=mysql_query($sql3,$conn);
while($property_img=mysql_fetch_array($rs3)){
?>
<tr>
<td colspan="2"><img src="http://domain.co.uk/img/property-images/<?php echo $property_img["image"]; ?>" width="80px" height="80px" /></td>
<!-- THIS IS VERY IMPORTANT: send the sequence or ID through a hidden field, to know which row you are gonna update later-->
<input type="hidden" name="sequence" value="<?php echo $property_img['sequence']; ?>"/>
<td colspan="2">
<?php for($i = 1; $i <= 13; $i++): ?>
<input type="checkbox" name="images[]>" value="<?php echo $i; ?>" <?php if($property_img["image$i"] == 'Y') { echo 'checked="checked"'; } ?> />
<?php endfor ?>
</td>
</tr>
<?php
}
?>
Then this is the next page where the update is done, have a good look at the comments:
<?php
//Handle as you want the situation when there are no images selected instead of using an exit(), I used it here just for the quickness;
if(count($_POST['images'] < 1) exit('no images where selected to update');
$images = array();
for($i = 1; $i <= 13; $i++){
$string = "image$i = ";
if(in_array($i, $_POST['images'])){
$string .= "'Y'"; //notice the double quoting here, it's important
} else {
//This updates the table so if it was checked when loaded, but unchecked by the user, this makes the change!
$string .= "'N'";
}
}
//This creates a string like: image1 = 'Y', images2 = 'N', etc...
$images = implode(', ', $images );
//try to sanitize the query first ... I won't cos am just showing you your question xD
$sql = "UPDATE property_images SET $images WHERE property_seq = " . mysql_real_escape_string($_POST[sequence]) . ";
mysql_query($sql,$conn);
?>

Categories