I am trying to complete a small PHP project based on a shop idea.
The user select a quantity, a size and a colour.
At the end, based on those factors the PHP will respons with:
.the quantity
.the quantity multiplied by the value assigned to the size
.the colour.
The values for each size are:
Small-15.75
Medium-16.75
Large-17.75
Extra-Large-18.75
Files:
Shop.html
<html>
<head>
<title>Shopping Page</title>
</head>
<body>
<form action="sizes.php" method="post">
Select the qty of widgets you require:
<select name="selqty">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
<br><br>
<input type="submit" value="Buy"/>
</form>
</body>
</html>
sizes.php
<html>
<head>
<title>Sizes</title>
</head>
<body>
<form action="selectcolour.php" method="post">
<input type="hidden" name="selqty" value="<?php echo $_POST["selqty"] ?>"/>
Please select the size:
<select name="size">
<option>Small</option>
<option>Medium</option>
<option>Large</option>
<option>Extra-Large</option>
</select>
<br><br>
<input type="submit" value="Buy"/>
</form>
</body>
</html>
selectcolour.php
<html>
<head><title>Select colour page</title></head>
<body>
<form action="confirmation.ph" method="post">
Select the colour for the
<input type="hidden" name="size" value="<?php echo $_POST["size"] ?>"/>
<input type="hidden" name="selqty" value="<?php echo $_POST["selqty"] ?>"/>
widgets you are ordering
<select name="selcolour">
<option>white</option>
<option>red</option>
<option>yellow</option>
<option>green</option>
<option>blue</option>
</select>
<br/><br/>
<input type="submit" value="Buy"/>
</form>
</body>
</html>
confirmation.php
<?php
echo "<h2> Your order quantity is $_POST[selqty] <h2/><br/>";
switch ($_POST["size"])
{
case "Small":
echo "<h2> Your order total price is $_POST[selqty]*15.75 <h2/><br>";
break;
case "Medium":
echo "<h2> Your order total price is $_POST[selqty]*$_POST[Large] <h2/><br>";
break;
case "Large":
echo "<h2> Your order total price is $_POST[selqty]*$_POST[Medium]<h2/><br>";
break;
case "Extra-Large":
echo "<h2> Your order total price is $_POST[selqty]*$_POST[Extra] <h2/><br>";
break;
}
echo "<h2> and the selected colour is $_POST[selcolour] </h2>";
?>
I cannot figure it out why the total price by size and quantity is not displayed correctly...
I know there's something with the way I am using the $_POST[] method and how I calculate the output but I focused too much and now I am not able to think straight on a solution..
Everything.
Arrays, multiplicity,etc
Related
I have the following code to submit GET values:
<form method="GET">
<input type="hidden" name="price" value="ASC" />
<input type="submit" value="Price Low to High" />
</form>
<form method="GET">
<input type="hidden" name="price" value="DESC" />
<input type="submit" value="Price High to Low" />
</form>
<form method="GET">
<input type="hidden" name="price" value="" />
<input type="submit" value="Default" />
</form>
Which is working fine. So I am not repeating myself with three forms, is there a way I could have a single form, and define what GET values are submitted by the buttons? They would still need to have the same information visible to the user e.g. the button that submits DESC needs to display as Price High to Low
use this code:
<form method="GET">
<input type="submit" name="price" value="ASC">Price Low to High</input>
<input type="submit" name="price" value="DESC" >Price High to Low</input>
<input type="submit" name="price" value="" >Default</input>
</form>
If having three buttons is not obligatory by design, then I suggest, why not just use a select to let the user decide what he wants (as most of the sites already do)
<form method="GET">
<select id="price" name="price">
<option value="">Default</option>
<option value="ASC">Price Low to High</option>
<option value="DESC">Price High to Low</option>
</select>
<input type="submit" value="Price Low to High" />
</form>
Demo
And if having three buttons is necessary then, you could do either one of (though not limited to) the following..
Use plain links (<a> anchor tags), yup you read that correct, since the form is just submitting using the $_GET method, eventually the data is gonna be inside the URLs, so why not just have it there in the first place using the href attributes of the "buttons" ?
Use the solution that #Aref Anafgeh suggested, and have three submit buttons each with different values and just let the html handle which value is sent to the server.
Use JavaScript and make ajax calls, which pretty much allows you to handle what is & isn't sent in the request.
Try this code.
<!DOCTYPE html>
<html>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "GET")
{
echo "Form Submited by ".$_GET['submitbutton']." Button";
}
?>
<form method="GET" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" name="price" value="ASC" />
<input type="submit" value="Price Low to High" name="submitbutton"/>
</form>
<form method="GET" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" name="price" value="DESC" />
<input type="submit" value="Price High to Low" name="submitbutton"/>
</form>
<form method="GET" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" name="price" value="" />
<input type="submit" value="Default" name="submitbutton"/>
</form>
<script>
</script>
</body>
</html>
<form method="get">
<select name="sort">
<option value="asc">Ascending</option>
<option value="desc">Descending</option>
<option value="default">Default</option>
</select>
<button name="price">Sort price ascending</button>
</form>
snip
<?php if ($_GET['sort'] == "asc") {
// Do something
} elseif ($_GET['sort'] == "desc") {
// Do something else
}
// etc
?>
Hyperlinks to http://url/?price=XXX would also work fine since you're using GET
first page
<?php session_start();?>
<!DOCTYPE html>
<html>
<body>
<form method="post" action="form.php">
<select name="list1">
<option value="NY"> ny</option>
<option value="NJ"> nj</option>
<option value="DE"> de</option>
</select>
<input type="submit">
</form>
<h1><?php
print_r($_SESSION);
?></h1>
</body>
</html>
second page
<?php session_start();?>
<!DOCTYPE HTML>
<html>
<body>
<?php
$_SESSION["list"] = $_POST["list1"];
$list = $_SESSION['list'];
?>
<form action="form1.php" method="post">
Name: <input type="text" name="name1"><br>
E-mail: <input type="text" name="email1"><br>
<input type="submit">
</form>
<br>
<h1> chosen number <br />>>> <?echo $list; ?> </h1>
<br>
<h1><?php
print_r($_SESSION);
?></h1>
after this comes third step, where selected state disappears from SESSION array,this is a multiple step form, there are 4 - 5 steps and starting from second page all they till form processing page all text entries get stored in the SESSION array
I want to add in drop down list first option manual and after all from my "game table".
In my code everything works fine but my manual option "select" repeat every time after the result from game table.
Here is my code
<!DOCTYPE HTML>
<html>
<head>
<title>Update Result</title>
</head>
<body>
<?php
//Connect to Database Server
include 'secure.php';
//Select a Table from Database
$query = "SELECT * FROM game";
//Get Result from Table (all)
$result = mysqli_query($connect,$query);
?>
<!-- Head Line -->
<h3> Update Result</h3>
<!-- Create Result Form -->
<form action="rsave.php" method="post">
<label>Date:</label><input type="date" name="ResultDate" tabindex="1" min="1899-12-31" max="2100-12-31" title="Please Enter Correct Date" required ><br>
<!--Select Start with PHP-->
<label>Game:</label><select id="Select1" name="ResultName" tabindex="2" title="Select a GameName" required >
<?php
// PHP Again
while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) {
?>
<option value="">SELECT</option>
<option value="<?php echo $line['GameID'];?>"><?php echo $line['name'];?></option>
<?php
}
?>
</select><br>
<!--Select End with PHP-->
<label>ReNO:</label><input type="number" name="ResultNo" min="0" max="100" tabindex="3" pattern="[0-9]{2,3}" title="Add a Number Between 0 to 100"required ><br>
<input type="submit" value="Update" tabindex="4">
<input type="reset" value="Reset" tabindex="5"><br>
</form>
</body>
</html>
pull this line:
<option value="">SELECT</option>
above your while loop, like this:
<label>Game:</label>
<select id="Select1" name="ResultName" tabindex="2" title="Select a GameName" required >
<option value="">SELECT</option>
<?php
// PHP Again
while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) {
?>
<option value="<?php echo $line['GameID'];?>"><?php echo $line['name'];?></option>
<?php
....
I have a problem whit form submission. I have a html form and the action simpli doesnt want to work. maybe i am doing something wrong, but here take a look at it:
<form action="add.php" method="post" autocomplete="off">
<input type="text" name="title" placeholder="Title" style="width:300px;">
<select id="type" name="type">
<option value="0">Matrix</option>
<option value="1">LoL</option>
<option value="2">Dota 2</option>
</select><br />
<textarea rows="30" cols="90" placeholder="Content here" name="content"></textarea><br />
<input type="submit" value="DODAJ">
</form>
so when i click on the button i does refresh the page but nothing is displayed. I't worked in local but when i upload it online nothnig, just a blank page. And the stuff that has to go to the database doesnt go. no errors, no nothing.
help?
Here's the complete code for that add.php page:
<?php
include '../php/db/init.php';
include '../php/db/connection.php';
protect_page();
admin_protect();
if(isset($_SESSION['logged_in'])){
if(isset($_POST['title'], $_POST['content'], $_POST['type'])){
$title = $_POST['title'];
$content = nl2br($_POST['content']);
$content_short = substr($content,0, 150);
$type = $_POST['type'];
echo $type;
die();
if(empty($title) or empty($content) or empty($type)){
$error = 'Potrebno je popuniti sva polja!';
} else {
$query = $pdo->prepare('INSERT INTO articles (article_title, article_content, article_content_short, article_timestamp, article_type) VALUES (?,?,?,?,?)');
$query->bindValue(1,$title);
$query->bindValue(2,$content);
$query->bindValue(3,$content_short);
$query->bindValue(4,time());
$query->bindValue(5,$type);
$query->execute();
redirect('http://www.matrixgamingns.com/admin/index.php');
}
}
?>
<html>
<head>
<title>Matrix Gaming Admin Panel</title>
<link rel="stylesheet" type="text/css" href="../css/admin.css">
<link rel="stylesheet" type="text/css" href="../css/reset.css">
</head>
<body>
<div id="home">
<div id="wrapper">
<?php include 'adminmenusidebar.php'; ?>
<div id="field">
<h3 style="font-size:22px;">Dodaj vest</h3>
<?php if(isset($error)){ ?>
<small style="color:#aa0000"> <?php echo $error?> </small>
<br/>
<?php }?>
<br />
<form action="add.php" method="post" autocomplete="off">
<input type="text" name="title" placeholder="Title" style="width:300px;">
<select id="type" name="type">
<option value="0">Matrix</option>
<option value="1">LoL</option>
<option value="2">Dota 2</option>
</select><br />
<textarea rows="30" cols="90" placeholder="Content here" name="content"></textarea><br />
<input type="submit" value="DODAJ">
<?php
ini_set('display_errors','On');?>
</form>
</div>
</div>
</div>
</body>
</html>
<?php
} else {
redirect('http://www.matrixgamingns.com/admin/index.php');
}
?>
Get rid of the die() after echo $type;
By including a die() function, it stops all in its tracks, just like return does and nothing gets executed afterwards.
You could use the login form example posted here: HTML Dynamic Forms Generator
I downloaded the source code for the login form and it saved me lots of time.
i was trying to get my check box to output "Korting is ... %" at the bottom of the page depending on which check box is checked e.g. 2th and 3th boxes are checked it would output "Korting is 15%" but i cant seem to get it to work. can someone please help me
thank you in advance
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="nl">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>XXL Computer winkel</title>
</head>
<body>
<h3>php lab 04</h3>
<table border=0 cellpadding=0 cellspacing=0 width=100%>
<form name="orderform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<tr>
<td>
Korting:<br />
<input type="checkbox" name="korting" value="15" /> Student 15% <br/>
<input type="checkbox" name="korting" value= "10" /> Senior 10% <br/>
<input type="checkbox" name="korting" value= "5" /> klant 5% <br/>
<hr/>
<img src="images/Lab4/1.jpg" width="200px" height="200px" alt=" "/>
<td/>
</tr>
<tr>
<td>
Toshiba Satellite A100-510 basisprijs 999.99
</td>
</tr>
<tr>
<td><!-- Shopping cart begin-->
<input type="hidden" name="toshibaproduct" value="001"/>
<input type="hidden" name="toshibamerk" value="Toshiba"/>
<input type="hidden" name="toshibamodel" value="Sattelite A100-510"/>
Aantaal: <input type="text" size=2 maxlenght=3 name="toshibaaantal" value="0"/>
<input type="hidden" name="toshibaprijs" value="999.99"/>
<input type="image" src="images/Lab4/2.jpg" border=0 value="bestellen"/>
<hr/>
</td><!--Shopping Cart END -->
</tr>
</form>
</table>
</body>
</html>
You need to use a name ending in [] such as name="korting[]". Then you can use $_POST['korting'] which will be an array containig the values of the checked checkboxes.
Note that $_POST['korting'] will not be set at all if no checkbox is checked!
Use radio instead of checkbox if you want one option to be selected
Do not use PHP_SELF, it is a security hole. Instead: <form action="">
Access the value with $_POST['korting']
To sum up all values, make your form like this:
<input type="checkbox" name="korting[]" value="15" /> Student 15% <br/>
<input type="checkbox" name="korting[]" value= "10" /> Senior 10% <br/>
<input type="checkbox" name="korting[]" value= "5" /> klant 5% <br/>
and the processing like that:
<?php
$korting = 0;
if (isset($_POST['korting']) && is_array($_POST['korting'])) {
$korting = array_sum($_POST['korting']);
}
echo 'Korting is ' . $korting . '%';
?>