I have gotten rid of the errors with the help of others but for some reason the calculations are not going through, does anyone have any thoughts?
</head>
<body>
<?php
if (($_POST['lastname'] == NULL) || ($_POST['quantity'] == NULL) ||
!isset($_POST['numberofrooms']) || !isset($_POST['smokingpreference']))
{
echo "<h1>Please return to the form and fill out completely</h1>";
}
else
{
?>
<h1>Thanks for the order, <?php echo $_POST['lastname']; ?> Family.</h1>
<h1>Your reservation for <?php echo $_POST['quantity']; ?> Night/s </h1>
<h1>With <?php echo $_POST['numberofrooms']; ?> room/s</h1>
<h1>And a room type of <?php echo $_POST['smokingpreference']; ?> </h1>
<h1>
<?php
if (isset($_POST['pets']))
{
echo 'With Pets';
}
if (isset($_POST['breakfast']))
{
echo ' and Breakfast Buffet';
}
?>
<h1/>
<h1>Your total due will be: <?php echo $_POST['$total']; ?> </h1>
<p> </p>
<h1>
<?php
if ($_POST['numberofrooms'] == "1")
{
$subtotal = ($_POST['quantity'] * 250);
}
elseif ($_POST['numberofrooms'] == "2")
{
$subtotal = ($_POST['quantity'] * 350);
}
else //isset($_POST['numberofrooms']) == "3")
{
$subtotal = ($_POST['quantity'] * 425);
}
$breakfast = 25;
if (isset($_POST['breakfast']))
{
$subtotal = ($_POST['quantity'] * $breakfast);
}
$smoke = 500;
if ($_POST['smokingpreference'] == 'smoking')
{
$subtotal = $subtotal + $smoke;
}
$pets = 200;
if (isset($_POST['pets']))
{
$total = $subtotal + $pets;
}
}
?>
</h1>
</body>
</html>
</head>
<body>
<form id="form1" name="form1" method="post" action="hw3.php">
<h1> Vacation Rental
</h1>
<p>Last Name:
<label for="lastname"></label>
<input name="lastname" type="text" id="lastname" size="18" maxlength="18" />
</p>
<p>Number of Nights Staying:
<label for="quantity"></label>
<input name="quantity" type="text" id="quantity" size="3" maxlength="3" />
</p>
<p>Number Of Rooms:
<label>
<br />
<input type="radio" name="numberofrooms" value="1" id="NumberofRooms_0" />
1</label>
- $250.00 Per Night<br />
<label>
<input type="radio" name="numberofrooms" value="2" id="NumberofRooms_1" />
2</label>
- $350.00 Per Night <br />
<label>
<input type="radio" name="numberofrooms" value="3" id="NumberofRooms_2" />
3</label>
- $425.00 Per Night</p>
<p>Smoking or Non-Smoking Room:
<label>
<br />
<input type="radio" name="smokingpreference" value="smoking" id="SmokingPreference_0" />
Smoking</label>
- Add $500.00
<br />
<label>
<input type="radio" name="smokingpreference" value="nonsmoking" id="SmokingPreference_1" />
Non-Smoking</label>
</p>
<p>
<input type="checkbox" name="pets" id="pets" />
<label for="pets">Pets</label>
- Add $200.00</p>
<p>
<input type="checkbox" name="breakfast" id="breakfast" />
<label for="breakfast">Breakfast Buffet</label>
- Add $25.00 Per Room</p>
<p>
<label>
<input type="reset" name="reset" id="reset" value="Reset" />
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
<br />
</p>
</form>
</body>
</html>
I cannot seem to wrap my head around how I should go about getting my outputs, as my syntax is fine, but I keep getting errors. Obviously the user needs to be able to select any combination and get the correct output, but I cannot even find the cause of the error as syntactically it's fine. I will add the html portion as well.
</head>
<body>
<form id="form1" name="form1" method="post" action="hw3.php">
<h1> Vacation Rental
</h1>
<p>Last Name:
<label for="lastname"></label>
<input name="lastname" type="text" id="lastname" size="18" maxlength="18" />
</p>
<p>Number of Nights Staying:
<label for="quantity"></label>
<input name="quantity" type="text" id="quantity" size="3" maxlength="3" />
</p>
<p>Number Of Rooms:
<label>
<br />
<input type="radio" name="numberofrooms" value="1" id="NumberofRooms_0" />
1</label>
- $250.00 Per Night<br />
<label>
<input type="radio" name="numberofrooms" value="2" id="NumberofRooms_1" />
2</label>
- $350.00 Per Night <br />
<label>
<input type="radio" name="numberofrooms" value="3" id="NumberofRooms_2" />
3</label>
- $425.00 Per Night</p>
<p>Smoking or Non-Smoking Room:
<label>
<br />
<input type="radio" name="smokingpreference" value="smoking" id="SmokingPreference_0" />
Smoking</label>
- Add $500.00
<br />
<label>
<input type="radio" name="smokingpreference" value="nonsmoking" id="SmokingPreference_1" />
Non-Smoking</label>
</p>
<p>
<input type="checkbox" name="pets" id="pets" />
<label for="pets">Pets</label>
- Add $200.00</p>
<p>
<input type="checkbox" name="breakfast" id="breakfast" />
<label for="breakfast">Breakfast Buffet</label>
- Add $25.00 Per Room</p>
<p>
<label>
<input type="reset" name="reset" id="reset" value="Reset" />
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
<br />
</p>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Vacation Rental Response Form</title>
</head>
<body>
<?php
if (($_POST['lastname'] == NULL) || ($_POST['quantity'] == NULL) ||
!isset($_POST['numberofrooms']) || !isset($_POST['smokingpreference']))
{
echo "<h1>Please return to the form and fill out completely</h1>";
}
else
{
?>
<h1>Thanks for the order, <?php echo $_POST['lastname']; ?> Family.</h1>
<h1>Your Reservation For <?php echo $_POST['quantity']; ?> Night/s </h1>
<h1>With <?php echo $_POST['numberofrooms']; ?> Room/s</h1>
<h1>With A Room Type Of <?php echo $_POST['smokingpreference']; ?> </h1>
<h1>With/Without <?php echo $_POST['pets']; ?> </h1>
<h1>And <?php echo $_POST['breakfast']; ?> </h1>
<h1>Your Total Due Will Be: <?php echo $_POST['$sub1']; ?> </h1>
<p> </p>
<h1>
<?php
if (isset($_POST['numberofrooms']) == "1")
{
$sub1 = ($_POST['quantity'] * 250);
}
elseif (isset($_POST['numberofrooms']) == "2")
{
$sub2 = ($_POST['quantity'] * 350);
}
else //isset($_POST['numberofrooms']) == "3")
{
$sub3 = ($_POST['quantity'] * 425);
}
$breakfast = 25;
if (isset($_POST['breakfast']))
{
$sub1 = $sub1 * $breakfast;
$sub2 = $sub2 * $breakfast;
$sub3 = $sub3 * $breakfast;
}
$smoke = 500;
if (isset($_POST['smokingpreference']))
{
$sub1 = $sub1 * $smoke;
$sub2 = $sub2 * $smoke;
$sub3 = $sub3 * $smoke;
}
$pets = 200;
if (isset($_POST['pets']))
{
$sub1 = $sub1 * $pets;
$sub2 = $sub2 * $pets;
$sub3 = $sub3 * $pets;
}
}
?>
</h1>
</body>
</html>
Your problem is that your are trying to access indexes in your $_POST array that are no there:
<h1>With/Without <?php echo $_POST['pets']; ?> </h1>
The element for pets is a checkbox, if it is not checked, you will not get a pets element in $_POST. So something like this would be better:
<h1>With/Without <?php echo isset($_POST['pets']) ? 'With' : 'Without'; ?> </h1>
Related
I'm working on an app for PHP class where we have to create an app to order pizza online. I'm stuck on an html form that is not posting on the script and I can't get it to work. I'm getting the following error and I don't know how to fix it.
I'm working on an app for PHP class where we have to create an app to order pizza online. I'm stuck on an html form that is not posting on the script and I can't get it to work. I'm getting the following error and I don't know how to fix it. Undefined index error
HTML index
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<title>HTech Pizza Online Order</title>
</head>
<body>
<div class="container">
<form action="receipt.php" method="POST">
<h2>HTech Pizzeria Online Order</h2>
<fieldset>
<legend><b>Customer Information</b></legend>
Name: <input type="text" require id="customer" name="customer"
placeholder="Enter Your Name..."><br>
Phone Number: <input type="text" require id="phoneNumber" name="phoneNumber"
placeholder="Enter Your Phone Number..."><br>
Address: <input type="text" require id="address" name="address"
placeholder="Enter Your Address..."><br>
</fieldset>
</form>
</div>
<form action="receipt.php" method="POST">
<fieldset>
<legend><b>Order Customization</b></legend>
<div class="checkbox">
<b>Size : </b><br><br>
<input id="radio3" type="radio" name="size" value="Large"><label for="radio3"><span><span></span></span>Large: $10.00</label> <br>
<input id="radio2" type="radio" name="size" value="Medium"><label for="radio2"><span><span></span></span>Medium: $8.00</label> <br>
<input id="radio1" type="radio" name="size" value="Small" checked="checked"><label for="radio1"><span><span></span></span>Personal: $6.00</label>
<br>
<br>
<b>Crust : </b><br><br>
<input id="radio4" type="radio" name="crust" value="traditional" checked="checked"><label for="radio4"><span><span></span></span>Traditional: $2.00</label><br>
<input id="radio5" type="radio" name="crust" value="welldone"><label for="radio5"><span><span></span></span>Well Done: $2.00</label><br>
<input id="radio6" type="radio" name="crust" value="thin"><label for="radio6"><span><span></span></span>Thin: $2.00</label><br><br>
<hr>
</div>
<div class="checkbox"><b>Toppings:</b><br><br>
<div>
<input id="toppingBox1" type="checkbox" name="toppings[]" value="Pepperoni"><label for="toppingBox1"><span></span>Pepperoni</label>
</div>
<div>
<input id="toppingBox2" type="checkbox" name="toppings[]" value="Bacon"><label for="toppingBox2"><span></span>Bacon</label>
</div>
<div>
<input id="toppingBox3" type="checkbox" name="toppings[]" value="Chicken"><label for="toppingBox3"><span></span>Chicken</label>
</div>
<div>
<input id="toppingBox4" type="checkbox" name="toppings[]" value="Sausage"><label for="toppingBox4"><span></span>Sausage</label>
</div>
<div>
<input id="toppingBox5" type="checkbox" name="toppings[]" value="Ham"><label for="toppingBox5"><span></span>Ham</label>
</div>
<div>
<input id="toppingBox6" type="checkbox" name="toppings[]" value="BlackOlives"><label for="toppingBox6"><span></span>Black Olives</label>
</div>
<div>
<input id="toppingBox7" type="checkbox" name="toppings[]" value="Parmesan"><label for="toppingBox7"><span></span>Parmesan</label>
</div>
<div>
<input id="toppingBox8" type="checkbox" name="toppings[]" value="Pineapple"><label for="toppingBox8"><span></span>Pineapple</label>
</div>
</div>
<hr>
<div class="checkbox"><b>Student Discount:
</b><br><br>
<input id="radio7" type="radio" name="studentBol" value="student"><label for="radio7"><span><span></span></span> Student</label> <br>
<input id="radio8" type="radio" name="studentBol" value="nonStudent"><label for="radio8"><span><span></span></span>Non - Student</label><br>
<br><hr><br>
<b>Delivery or Pickup:</b> <br><br>
<input id="radio9" type="radio" name="modePickup" value="Delivery"><label for="radio9"><span><span></span>
</span>Delivery</label><br>
<input id="radio0" type="radio" name="modePickup" value="Pickup"><label for="radio0"><span><span></span></span>Store Pickup</label><br>
<br>
<b>Boxes Of Pizza:</b><br><br>
<div class="custom-select">
<label>
<select name="noOfPizza">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</label>
</div>
</div>
<br>
<br>
Additional Instructions:<br><br>
<label>
<textarea name="addInstructions" cols="60" rows="10"></textarea>
</label>
<br>
<br>
<input class="btn btn1" type="submit" name="order" value="Order Now">
</fieldset>
<br>
</form>
</body>
</html>
PHP Script
<?php
if (isset($_POST['order'])) {
// Check for pizza size
switch ($_POST['size']) {
case "Small":
$pizzaSizePrice = 6.00;
break;
case "Medium":
$pizzaSizePrice = 8.00;
break;
case "Large":
$pizzaSizePrice = 10.00;
break;
}
// Checking Student Qualification
if ($_POST['studentBol'] == "student") {
$studentBol = true;
} else {
$studentBol = false;
}
// Toppings
$toppingArray = $_POST['toppings'];
if (count($toppingArray) == 1) {
$toppingAmount = 0.00;
} else {
// First topping free, added toppings an extra $1
$toppingAmount = count($toppingArray) - 1;
}
// Pickup
if ($_POST['modePickup'] == "Delivery") {
$modePickup = 5.00;
} else {
$modePickup = 0.00;
}
$crustType = 2.00;
$numberOfPizza = $_POST['noOfPizza'];
$instructions = $_POST['addInstructions'];
// Calculating Amount
$netAmount = $crustType + $pizzaSizePrice + $toppingAmount;
$netAmount = $netAmount * $numberOfPizza;
// Adding Delivery Fee
$netAmount = $netAmount + $modePickup;
if ($studentBol) {
$studentDiscount = $netAmount * 10 / 100;
// Applying the student discount
$netAmount = $netAmount - $studentDiscount;
$studentDiscount = number_format($studentDiscount, 2, '.', '');
}
// adding tax
$salesTax = $netAmount * 10 / 100;
$salesTax = number_format($salesTax, 2, '.', '');
$total = $salesTax + $netAmount;
$total = number_format($total, 2, '.', '');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<title>Order Confirmation</title>
</head>
<body>
<h2>Order Confirmation</h2>
<hr>
<h3>Customer Information:</h3>
<h3>First Name:</h3><?php echo $_POST['customer']; ?>
<h3>Phone Number:</h3><?php echo $_POST['phoneNumber']; ?>
<h3>Address:</h3><?php echo $_POST['address']; ?>
<hr>
<h3>Size: </h3><?php echo $_POST['size'] . " $" . $pizzaSizePrice; ?>
<h3>Crust Type: </h3><?php echo $_POST['crust'] . " $2"; ?>
<h3>Toppings: </h3>
<?php
if (count($toppingArray) == 1) {
foreach ($toppingArray as $topping) {
echo $topping . " Free<br>";
}
} else {
foreach ($toppingArray as $topping) {
// First topping free
if ($topping == "Peperroni") {
echo $topping . " Free<br>";
} else {
echo $topping . " $1<br>";
}
}
}
?>
<h3>Pizza's Ordered: </h3><?php echo $numberOfPizza; ?>
<h3>Additional Instructions:</h3> <?php echo $_POST['addInstructions']; ?>
<br>
<hr>
<h3>Delivery or Pickup: </h3><?php echo $_POST['modePickup'] . " $" . $modePickup; ?>
<!-- Check Student Discount -->
<?php
if ($studentBol) {
echo "<br><br>Student Discount: $" . $studentDiscount;
}
?>
<h3>Sales Tax:</h3> $<?php echo $salesTax; ?>
<hr>
<h3>Total Due on Delivery: </h3> $<?php echo $total; ?>
</body>
</html>
It's because you have two forms in your HTML.
Refer to this part of your code:
...
<form action="receipt.php" method="POST">
<h2>HTech Pizzeria Online Order</h2>
<fieldset>
<legend><b>Customer Information</b></legend>
Name: <input type="text" require id="customer" name="customer"
placeholder="Enter Your Name..."><br>
Phone Number: <input type="text" require id="phoneNumber" name="phoneNumber"
placeholder="Enter Your Phone Number..."><br>
Address: <input type="text" require id="address" name="address"
placeholder="Enter Your Address..."><br>
</fieldset>
</div>
<form action="receipt.php" method="POST">
...
You're only submitting the second of the two forms and the customer info is in the first form. Try consolidating your fields into one form and you should be good to go.
Hope this helps.
I have a code. Somehow, it only able to pick the last hidden input name field instead of the other one. I also tried use if and else but nothing is displayed. Please advise.
Without the if else cases scenario:
HTML:
<div class="tab-label">
<input type="radio" id="ldktech_product_good" name="conditition" value="good" >
<input type="hidden" id="ldktech_product_price_good" name="price" value="7.50">
<label for="ldktech_product_good">Good</label>
NOTE: Please include the charger with your iPad trade-in, or a replacement fee will be deducted from the offer
<div class="tab-label">
<input type="radio" id="ldktech_product_flawless" name="conditition" value="flawless" >
<input type="hidden" id="ldktech_product_price_flawless" name="price" value="10">
<label for="ldktech_product_flawless">Flawless</label>
PHP:
$condition = $_POST["conditition"];
$price = $_POST["price"];
echo $price;
echo "<br>";
echo $condition;
With the if else scenarios:
HTML Code:
<div class="tab-label">
<input type="radio" id="ldktech_product_good" name="conditition" value="good" >
<input type="hidden" id="ldktech_product_price_good" name="price-good" value="7.50">
<label for="ldktech_product_good">Good</label>
<div class="tab-label">
<input type="radio" id="ldktech_product_flawless" name="conditition" value="flawless" >
<input type="hidden" id="ldktech_product_price_flawless" name="price-flawless" value="10">
<label for="ldktech_product_flawless">Flawless</label>
PHP code:
$condition = $_POST["conditition"];
if($condition == "good"){
$price = $_POST["price-good"];}
else if ($condition == "flawless"){
$price = $_POST["price-flawless"];}
echo $price;
echo "<br>";
echo $condition;
Nothing work. Please advised
works fine for me. don't know if your form method is post or not or your action url is set to your script url or if your script is on the same page make sure you place it accordingly.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PhpFiddle Initial Code</title>
</head>
<body>
<div style="margin: 30px 10%;">
<h3>My form</h3>
<form id="myform" name="myform" method="post">
<div class="tab-label">
<input type="radio" id="ldktech_product_good" name="conditition" value="good" >
<input type="hidden" id="ldktech_product_price_good" name="price-good" value="7.50">
<label for="ldktech_product_good">Good</label>
<div class="tab-label">
<input type="radio" id="ldktech_product_flawless" name="conditition" value="flawless" >
<input type="hidden" id="ldktech_product_price_flawless" name="price-flawless" value="10">
<label for="ldktech_product_flawless">Flawless</label> <br /><br />
<button id="mysubmit" type="submit">Submit</button><br /><br />
</form>
</div>
<?php
if(isset($_POST["conditition"])){
$condition = $_POST["conditition"];
if($condition == "good"){
$price = $_POST["price-good"];
}
else if ($condition == "flawless"){
$price = $_POST["price-flawless"];
}
echo $price;
echo "<br>";
echo $condition;
}
?>
</body>
</html>
I believe this is what you're trying to do?
EDIT:
<form method="post">
<div class="tab-label">
<input type="radio" name="condition1" value="good">
<input type="hidden" name="price1" value="7.50">
<label for="ldktech_product_good">Good</label>
</div>
<div class="tab-label">
<input type="radio" name="condition2" value="flawless">
<input type="hidden" name="price2" value="10">
<label for="ldktech_product_flawless">Flawless</label>
</div>
<input type="submit" name="submit" value="Get records">
</form>
<?php
if (isset($_POST['submit'])) {
if (isset($_POST['condition1']) && $_POST['condition1'] == "good") {
echo "You have selected :".$_POST['price1']; // Displaying Selected Value
} else if (isset($_POST['condition2']) && $_POST['condition2'] == "flawless") {
echo "You have selected :".$_POST['price2']; // Displaying Selected Value
}
}
?>
Im currently using CodeIgniter for my webpage and I am trying to make the radio button to be selected if the locked_status is locked and vice versa. I have data that is passed to the view and I want to use the data to check if the locked_status is either locked or not. The user is able to change the locked_status and when the form is submitted, the data is updated accordingly when the form is submitted. I'm trying to use javascript but I do not know how to retrieve the data and check it. Or is there any other better way to do this?
my controller loading view and passing the data to view.
function userInformation($userName)
{
$this->load->model("agentDB_model");
$data['results'] = $this->agentDB_model->getSelectedAgentDetails($userName);
$this->load->view("viewAgentInfo_view", $data);
}
view
<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome</title>
</head>
<body>
<div id="content">
<h1>Home Page</h1>
<p>Selected Agent Information</p>
</div>
<?php
foreach($results as $row)
{
$userName = $row->userName;
$userPassword = $row->userPassword;
$agencyName = $row->agencyName;
$agencyCodeNo = $row->agencyCodeNo;
$invalidLoginCount = $row->invalidLoginCount;
$locked_status = $row->locked_status;
$logged_in = $row->logged_in;
}
/*
if($row->locked_status == 0)
{
$locked_status = "Avalaible";
}
else if($row->locked_status == 1)
{
$locked_status = "Locked";
}
*/
?>
<?php echo form_open('site/updateValues') ?>
User Name: <input type="text" name="tbx_userName" value="<?php echo "$userName"?>" readonly/></br>
User Password: <input type="password" name="tbx_userPassword" value="<?php echo "$userPassword"?>"/></br>
Agency Name: <input type="text" name="tbx_agencyName" value="<?php echo "$agencyName"?>"/></br>
Agency Code Number: <input type="text" name="tbx_agencyCodeNo" value="<?php echo "$agencyCodeNo"?>"/></br>
Invalid Login Count: <input type="text" readonly name="tbx_invalidLoginCount" value="<?php echo "$invalidLoginCount"?>"/></br>
Locked Status: <input type="radio" name="locked_status" id="lock">Lock
<input type="radio" name="locked_status" id="unlock">Unlock
<br/>
<!--<input name="tbx_locked_status" value="<?php echo "$locked_status"?>" readonly/></br> -->
Logged In: <input type="text" readonly name="tbx_logged_in" value="<?php echo "$logged_in"?>" /></br>
<input type="submit" value="Submit"/>
</form>
<div id="footer">
<p>Copyright (c) 2012 basicsite.com</p>
</div>
</body>
</html>
Try this one
<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome</title>
</head>
<body>
<div id="content">
<h1>Home Page</h1>
<p>Selected Agent Information</p>
</div>
<?php
foreach($results as $row)
{
$userName = $row->userName;
$userPassword = $row->userPassword;
$agencyName = $row->agencyName;
$agencyCodeNo = $row->agencyCodeNo;
$invalidLoginCount = $row->invalidLoginCount;
$locked_status = $row->locked_status;
$logged_in = $row->logged_in;
if($row->locked_status == 0)
{
$locked_status = "<input type="radio" name="locked_status" checked value=0 id="lock">Locked <input type="radio" name="locked_status" id="unlock" value=1> Unlocked";
}
else if($row->locked_status == 1)
{
$locked_status = " <input type="radio" name="locked_status" value=0 id="lock"> Locked <input type="radio" name="locked_status" checked id="unlock" value=1> Unloacked";
}
}
?>
<?php echo form_open('site/updateValues') ?>
User Name: <input type="text" name="tbx_userName" value="<?php echo "$userName"?>" readonly/></br>
User Password: <input type="password" name="tbx_userPassword" value="<?php echo "$userPassword"?>"/></br>
Agency Name: <input type="text" name="tbx_agencyName" value="<?php echo "$agencyName"?>"/></br>
Agency Code Number: <input type="text" name="tbx_agencyCodeNo" value="<?php echo "$agencyCodeNo"?>"/></br>
Invalid Login Count: <input type="text" readonly name="tbx_invalidLoginCount" value="<?php echo "$invalidLoginCount"?>"/></br>
Locked Status: <?php echo $locked_status; ?>
<br/>
Logged In: <input type="text" readonly name="tbx_logged_in" value="<?php echo "$logged_in"?>" /></br>
<input type="submit" value="Submit"/>
</form>
<div id="footer">
<p>Copyright (c) 2012 basicsite.com</p>
</div>
</body>
</html>
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm making a calculator-like web application that asks for user names, then displays their names on the next page and adds their score. I have three problems:
The user names won't display on the second page
It won't add the score
Whenever you enter a score for player 1, player 2's score is reset to zero, and vise-versia
You can access the page at: ripdvd.x10.mx/index.php
Thanks in advance!
First page:
<!DOCTYPE html>
<html>
<head>
<title>Select Players</title>
<link rel=StyleSheet href="style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form method="post" action="scoregen.php">
<p class="par">
<label for="player1">Please type in the one of the players first name:</label>
<input type="text" id="player1" name="player1" size="17" maxlength="17" class="textbox" />
</p>
<p class="par">
<label for="player2">Please type in another players first name:</label>
<input type="text" id="player2" name="player2" size="17" maxlength="17" class="textbox" />
</p>
<input type="submit" class="button" name="button" value="Start Playing!" />
</form>
</body>
</html>
Second page(displays scores and provides a way to update them)
<?php
// Get data from HTML form.
//Gets the player names
$player1 = $_POST['player1'];
$player2 = $_POST['player2'];
$addScore1 = $_POST['addScore1'];
$addScore2 = $_POST['addScore2'];
$oldScore1 = $_POST['oldScore1'];
$oldScore2 = $_POST['oldScore2'];
$curr1=$_COOKIE["score1"]+$addScore1;
$curr2=$_COOKIE["score2"]+$addScore2;
setcookie("score1", $_COOKIE["score1"]+$addScore1, time()+3600);
setcookie("score2", $_COOKIE["score2"]+$addScore2, time()+3600);
//Reset cookies if reset button is 't', which makes it clear scores
if ($clse = t){
setcookie ("score1", "", time() - 3600);
setcookie ("score2", "", time() - 3600);
}
// Generate HTML form
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Score Add</title>
<link rel=StyleSheet href="style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form method="post" action=" ">
<p class="par">
<label for="addScore1">Enter your score, <?php echo $player1; ?>:</label>
<input type="text" name="addScore1" id="addScore1" class="textbox" />
<input type="hidden" name="oldScore1" id="oldScore1" value="<?php echo $oldscore1; ?>" />
<input type="submit" class="button" value="Add Score!" />
</p>
<p class="par">
<label for="addScore2">Enter your score, <?php echo $player2; ?>:</label>
<input type="text" name="addScore2" id="addScore2" class="textbox"/>
<input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $oldscore2; ?>" />
<input type="submit" class="button" value="Add Score!"/>
</form>
</p>
<form method="post" action=" ">
<input type="hidden" name="clsc" id="clsc" value="t" />
<input type="submit" class="reset" value="Clear Scores" />
</form>
<!--Shows player and score-->
<p class="par"><?php echo $player1;?>:<?php echo $curr1?></p>
<p class="par"><?php echo $player2;?>:<?php echo $curr2?></p>
</body>
</html>
I made several changes to your code (including the removal of cookies - hope that's ok). If you can live without cookies, try this:
<?php
// Get data from HTML form.
// Gets the player names
$player1 = $_POST['player1'];
$player2 = $_POST['player2'];
if ($_POST['addScore1'] == null)
$addScore1 = 0;
else
$addScore1 = $_POST['addScore1'];
if ($_POST['addScore2'] == null)
$addScore2 = 0;
else
$addScore2 = $_POST['addScore2'];
if ($_POST['oldScore1'] == null)
$oldScore1 = 0;
else
$oldScore1 = $_POST['oldScore1'];
if ($_POST['oldScore2'] == null)
$oldScore1 = 0;
else
$oldScore2 = $_POST['oldScore2'];
$curr1=$oldScore1+$addScore1;
$curr2=$oldScore2+$addScore2;
if ($_POST['clse'] == "t"){
$curr1 = 0;
$curr2 = 0;
}
// Generate HTML form
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Score Add</title>
<link rel=StyleSheet href="style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<p class="par">
<label for="addScore1">Enter your score, <?php echo $player1; ?>:</label>
<input type="text" name="addScore1" id="addScore1" class="textbox" />
<input type="hidden" name="oldScore1" id="oldScore1" value="<?php echo $curr1; ?>" />
<input type="submit" class="button" value="Add Score!" />
</p>
<p class="par">
<label for="addScore2">Enter your score, <?php echo $player2; ?>:</label>
<input type="text" name="addScore2" id="addScore2" class="textbox"/>
<input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $curr2; ?>" />
<input type="submit" class="button" value="Add Score!"/>
</p>
<input type="hidden" name="player1" value="<?PHP echo $player1;?>" />
<input type="hidden" name="player2" value="<?PHP echo $player2;?>" />
</form>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="hidden" name="clsc" id="clsc" value="t" />
<input type="submit" class="reset" value="Clear Scores" />
</form>
<!--Shows player and score-->
<p class="par"><?php echo $player1;?>:<?php echo $curr1?></p>
<p class="par"><?php echo $player2;?>:<?php echo $curr2?></p>
</body>
</html>
Note, I haven't actually ran this, but try it and let me know if you get any errors or still have problems.
Try this one:
index.php
<!DOCTYPE html>
<html>
<head>
<title>Select Players</title>
<link rel=StyleSheet href="style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form method="post" action="scoregen.php">
<p class="par">
<label for="player1">Please type in the one of the players first name:</label>
<input type="text" id="player1" name="player1" size="17" maxlength="17" class="textbox" />
</p>
<p class="par">
<label for="player2">Please type in another players first name:</label>
<input type="text" id="player2" name="player2" size="17" maxlength="17" class="textbox" />
</p>
<input type="submit" class="button" name="names" value="Start Playing!" />
</form>
</body>
</html>
scoregen.php
<?php
// Get data from HTML form.
//Gets the player names
if(isset($_POST['player1'])){
$player1 = $_POST['player1'];
}else{
$player1 = "No Player";
}
if(isset($_POST['player2'])){
$player2 = $_POST['player2'];
}else{
$player2 = "No Player";
}
//CHECKS IF THE ADDSCORE BUTTON IS HIT
if(isset($_POST['AddScore'])){
$oldScore1 = $_POST['oldScore1'];
$oldScore2 = $_POST['oldScore2'];
if(!empty($_POST['addScore1'])){
$addScore1 = $_POST['addScore1'];
}else{
$addScore1 = 0;
}
if(!empty($_POST['addScore2'])){
$addScore2 = $_POST['addScore2'];
}else{
$addScore2 = 0;
}
}else{//INITIALIZES THE SCORES TO ZERO; OR SETS IT TO ZERO IF THE CLEAR SCORES BUTTON IS HIT
$oldScore1 = 0;
$oldScore2 = 0;
$addScore1 = 0;
$addScore2 = 0;
}
$curr1 = $oldScore1 + $addScore1;
$curr2 = $oldScore2 + $addScore2;
// Generate HTML form
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Score Add</title>
<link rel=StyleSheet href="style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form method="post" action=" ">
<p class="par">
<label for="addScore1">Enter your score, <?php echo $player1; ?>:</label>
<input type="text" name="addScore1" id="addScore1" class="textbox" />
<input type="hidden" name="oldScore1" id="oldScore1" value="<?php echo $curr1; ?>" />
<input type="submit" name="AddScore" class="button" value="Add Score!" />
</p>
<p class="par">
<label for="addScore2">Enter your score, <?php echo $player2; ?>:</label>
<input type="text" name="addScore2" id="addScore2" class="textbox"/>
<input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $curr2; ?>" />
<input type="submit" name="AddScore" class="button" value="Add Score!"/>
</p>
<input type="hidden" name="player1" value="<?php echo $player1;?>"/>
<input type="hidden" name="player2" value="<?php echo $player2;?>"/>
<p class="par">
<input type="submit" class="reset" value="Clear Scores" />
</p>
</form>
<!--Shows player and score-->
<p class="par"><?php echo $player1;?>:<?php echo $curr1?></p>
<p class="par"><?php echo $player2;?>:<?php echo $curr2?></p>
</body>
</html>
1) I did not use cookies here, you don't actually need it for the scores, you can use to save the usernames though.
2) You can actually just have one addScore button, but in this code it doesn't matter. it add the scores anyway.
3) I always check if a an element of the POST is set or if it has a value or not. So i put some checking for the scores and the names.
4) In this line of your code:
<input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $oldscore2; ?>" />
i changed the $oldscore to $curr, because the $curr score would be needed in the next submit as the oldscore so the score would add up.
$oldScore1 does not equal $oldscore1
and in any case you need to keep the new values to post to the next page
<input type="hidden" name="oldScore1" id="oldScore1" value="<?php echo $curr1; ?>" />
<input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $curr2; ?>" />
Drop the cookies and just use
$curr1=$oldScore1+$addScore1;
$curr2=$oldScore2+$addScore2;
It's also worth checking if the posts are set to avoid warnings, and making sure they are integers, e.g.
$oldScore1 = isset($_POST['oldScore1'])? (int)$_POST['oldScore1'] : 0;
$oldScore2 = isset($_POST['oldScore2'])? (int)$_POST['oldScore2'] : 0;
When I input numeric value in Number 1 and Number 2, and press "Add". It does not display the total added value. Please see my coding below. and advice me, what to is the problem, and what can be done.
<html>
<head>
<title>Simple Calculator</title>
<?php
if(isset($_POST['submitted'])){
if(is_numeric($_POST['number1']) && is_numeric($_POST['number2'])){
$add = ($_POST['number1'] + $_POST['number2']);
echo "Add: ".$_POST['number1']."+".$_POST['number2']."=";
}
}
?>
<script type="text/javascript">
</script>
</head>
<body>
<h1>Simple Calculator</h1>
<form action="simple_calculator.php" method="post">
<p>Number 1: <input type="text" name="number1" size="20" value="<?php if(isset($_POST['number1'])) echo $_POST['number1'];?>"/></p>
<p>Number 2: <input type="text" name="number2" size="20" value="<?php if(isset($_POST['number2'])) echo $_POST['number2'];?>"/></p>
<input type="button" name="add" value="Add" />
<input type="button" name="minus" value="Minus" />
<input type="button" name="multiply" value="Multiply" />
<input type="button" name="divide" value="Divide" />
<input type="reset" name="rest" value="Reset" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>
You are echoing the result data into the <head>, so it will not be displayed.
You forgot to echo $add.
Your <input>s are of type button and not submit, so the form will not be submitted to the server.
Because you are echoing the previously entered values into the form, <input type="reset"> will probably not do what you want/expect it to do. I think it would be better to implement this as another submit.
Because this form affects only what the next page displays and does not make a permanent change to the server, you should use the GET method and not POST.
Try this:
<html>
<head>
<title>Simple Calculator</title>
<script type="text/javascript"></script>
</head>
<body>
<h1>Simple Calculator</h1>
<form action="simple_calculator.php" method="get">
<p>Number 1: <input type="text" name="number1" size="20" value="<?php if (isset($_GET['number1']) && !isset($_GET['reset'])) echo $_GET['number1'];?>"/></p>
<p>Number 2: <input type="text" name="number2" size="20" value="<?php if (isset($_GET['number2']) && !isset($_GET['reset'])) echo $_GET['number2'];?>"/></p>
<input type="submit" name="add" value="Add" />
<input type="submit" name="minus" value="Minus" />
<input type="submit" name="multiply" value="Multiply" />
<input type="submit" name="divide" value="Divide" />
<input type="submit" name="reset" value="Reset" />
<input type="hidden" name="submitted" value="1" />
</form>
<?php
if (isset($_GET['submitted']) && !isset($_GET['reset'])) {
echo "<div>";
if (is_numeric($_GET['number1']) && is_numeric($_GET['number2'])) {
if (isset($_GET['add'])) {
$result = $_GET['number1'] + $_GET['number2'];
echo "Add: ".$_GET['number1']." + ".$_GET['number2']." = ".$result;
} else if (isset($_GET['minus'])) {
$result = $_GET['number1'] - $_GET['number2'];
echo "Minus: ".$_GET['number1']." - ".$_GET['number2']." = ".$result;
} else if (isset($_GET['multiply'])) {
$result = $_GET['number1'] * $_GET['number2'];
echo "Multiply: ".$_GET['number1']." * ".$_GET['number2']." = ".$result;
} else if (isset($_GET['divide'])) {
$result = $_GET['number1'] / $_GET['number2'];
echo "Divide: ".$_GET['number1']." / ".$_GET['number2']." = ".$result;
}
} else {
echo "Invalid input";
}
echo "</div>";
}
?>
</body>
</html>
The solution of DaveRandom works fine if you change this
action="simple_calculator.php"
by
action="<?php echo $_SERVER['PHP_SELF'] ?>"