Hi, I am reposting this qusetion as last time I left out some important information by mistake. Thankyou to the user tha
Posted: Fri May 20, 2022 10:13 am
Hi, I am reposting this qusetion as last time I left out some
important information by mistake.
Thankyou to the user that helped me in my last submission of
this question, the edit you sent me works really well, however, I
need all the error messages and the product table to print on the
same page rather than on another page.
Can someone please help me too do this. I am using postback but
for some reason it is still printing on another page. I have no
clue as to why?!
Thankyou!
Here is the Question:
• Create a PHP file named Exercise5.php in the practicals/week10
folder of your TWA web site.
• Combine the code from exercise4.html and exercise4.php into
Exercise5.php (this is similar to exercise 3 in week 7 practical
exercises) so that the php script and the html form are in the same
file.
A. Modify the code so that
i. The form uses postback (ie, the form action is the same file
exercise5.php)
ii. Only the form is displayed on first load of the page
iii. The table of products is only displayed when there are
records to display
iv. The form and the messages as described in exercise 4 are
displayed when appropriate in appropriate locations
v. The value that the user enters in the form is maintained in
the text box after form submission
Here is the code:
Exercise4.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
input[type="text"] {border: 1px solid black;}
</style>
<link rel="stylesheet" href="../css/week10Styles.css">
<title>Week 10 Exercise 4 Form</title>
</head>
<body>
<form id="exercise4Form" method="post"
action="exercise4.php">
<h1>Quantity in Stock</h1>
<p>Please enter the quantity to check against stock
levels</p>
<p>
<label for="quantity">Quantity: </label>
<input type="text" name="quantity" size="10" id="quantity"
maxlength="6">
</p>
<p><input type="submit" name="submit"></p>
</form>
</body>
</html>
Exercise4.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Welcome</title>
<style>
span{color:red;}
table{border-collapse:collapse;}
</style>
</head>
<body>
<?php
require_once("conn.php");
$qty = $_POST['quantity'];
if(!is_numeric($qty)){
echo "<span>The value entered for the quantity was not a
number.</span>";
}else{
$sql = "SELECT * FROM product where quantityInStock>$qty ORDER
BY QuantityInStock ASC";
$result = $dbConn->query($sql) or die("Problem with
query:".$dbConn->error);
if (!mysqli_num_rows($result) > 0) {
echo "<span>There are no products that have more than $qty in
stock.</span>";
}else{
?>
<h1>Product with stock > <?php echo
$qty;?></h1>
<table border="1">
<tr>
<th>Name</th>
<th>Quantity In Stock</th>
<th>Price</th>
</tr>
<?php
while($row = $result->fetch_assoc()){?>
<tr>
<td><?php echo $row["name"]; ?></td> <!--
display result -->
<td><?php echo $row["quantityInStock"];
?></td>
<td><?php echo $row["price"]; ?></td>
</tr>
<?php } } } $dbConn->close();?> <!-- closing connection
-->
</table>
</body>
</html>
conn.php is called for in Exercise4.php and is required for
everything to run correctly. This is already created for us and
must be kept as it is (not edited). I am putting it here so you can
better understand what is needed.
conn.php:
<?php
$dbConn = new mysqli("localhost", "TWA_student", "TWA_2022_Autumn",
"electrical");
if($dbConn->connect_error) {
die("Failed to connect to database " .
$dbConn->connect_error);
}
?>
Here is the completed code from the last submission of
this question that the user helped me to write:
<!DOCTYPE html>
<?php
//this if statement will execute when the user click on suubmit
button
if($_SERVER["REQUEST_METHOD"] == "POST"){
require_once("conn.php");//connect database
$qty = $_POST['quantity'];//get the value from form
//check the qty variable number or not
//if it is number, diplay a message otherwise it will execute else
part
if(!is_numeric($qty)){
echo "<span>The value entered for the quantity was not a
number.</span>";
}else{
$sql = "SELECT * FROM product where quantityInStock>$qty ORDER
BY QuantityInStock ASC";
$result = $dbConn->query($sql) or die("Problem with
query:".$dbConn->error);
if (!mysqli_num_rows($result) > 0) {
echo "<span>There are no products that have more than $qty in
stock.</span>";
}else{
?>
<h1>Product with stock > <?php echo
$qty;?></h1>
<table border="1">
<tr>
<th>Name</th>
<th>Quantity In Stock</th>
<th>Price</th>
</tr>
<?php
//display the product
while($row = $result->fetch_assoc()){?>
<tr>
<td><?php echo $row["name"]; ?></td> <!--
display result -->
<td><?php echo $row["quantityInStock"];
?></td>
<td><?php echo $row["price"]; ?></td>
</tr>
<?php
}
}
} $dbConn->close();
}
else {
?>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
input[type="text"] {border: 1px solid black;}
</style>
<link rel="stylesheet" href="../css/week10Styles.css">
<title>Week 10 Exercise 5 Form</title>
</head>
<body>
<form id="exercise4Form" method="post" action="<?php echo
$_SERVER["PHP_SELF"];?>">
<h1>Quantity in Stock</h1>
<p>Please enter the quantity to check against stock
levels</p>
<p>
<label for="quantity">Quantity: </label>
<input type="text" name="quantity" size="10" id="quantity"
maxlength="6">
</p>
<p><input type="submit" name="submit"></p>
</form>
</body>
</html>
important information by mistake.
Thankyou to the user that helped me in my last submission of
this question, the edit you sent me works really well, however, I
need all the error messages and the product table to print on the
same page rather than on another page.
Can someone please help me too do this. I am using postback but
for some reason it is still printing on another page. I have no
clue as to why?!
Thankyou!
Here is the Question:
• Create a PHP file named Exercise5.php in the practicals/week10
folder of your TWA web site.
• Combine the code from exercise4.html and exercise4.php into
Exercise5.php (this is similar to exercise 3 in week 7 practical
exercises) so that the php script and the html form are in the same
file.
A. Modify the code so that
i. The form uses postback (ie, the form action is the same file
exercise5.php)
ii. Only the form is displayed on first load of the page
iii. The table of products is only displayed when there are
records to display
iv. The form and the messages as described in exercise 4 are
displayed when appropriate in appropriate locations
v. The value that the user enters in the form is maintained in
the text box after form submission
Here is the code:
Exercise4.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
input[type="text"] {border: 1px solid black;}
</style>
<link rel="stylesheet" href="../css/week10Styles.css">
<title>Week 10 Exercise 4 Form</title>
</head>
<body>
<form id="exercise4Form" method="post"
action="exercise4.php">
<h1>Quantity in Stock</h1>
<p>Please enter the quantity to check against stock
levels</p>
<p>
<label for="quantity">Quantity: </label>
<input type="text" name="quantity" size="10" id="quantity"
maxlength="6">
</p>
<p><input type="submit" name="submit"></p>
</form>
</body>
</html>
Exercise4.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Welcome</title>
<style>
span{color:red;}
table{border-collapse:collapse;}
</style>
</head>
<body>
<?php
require_once("conn.php");
$qty = $_POST['quantity'];
if(!is_numeric($qty)){
echo "<span>The value entered for the quantity was not a
number.</span>";
}else{
$sql = "SELECT * FROM product where quantityInStock>$qty ORDER
BY QuantityInStock ASC";
$result = $dbConn->query($sql) or die("Problem with
query:".$dbConn->error);
if (!mysqli_num_rows($result) > 0) {
echo "<span>There are no products that have more than $qty in
stock.</span>";
}else{
?>
<h1>Product with stock > <?php echo
$qty;?></h1>
<table border="1">
<tr>
<th>Name</th>
<th>Quantity In Stock</th>
<th>Price</th>
</tr>
<?php
while($row = $result->fetch_assoc()){?>
<tr>
<td><?php echo $row["name"]; ?></td> <!--
display result -->
<td><?php echo $row["quantityInStock"];
?></td>
<td><?php echo $row["price"]; ?></td>
</tr>
<?php } } } $dbConn->close();?> <!-- closing connection
-->
</table>
</body>
</html>
conn.php is called for in Exercise4.php and is required for
everything to run correctly. This is already created for us and
must be kept as it is (not edited). I am putting it here so you can
better understand what is needed.
conn.php:
<?php
$dbConn = new mysqli("localhost", "TWA_student", "TWA_2022_Autumn",
"electrical");
if($dbConn->connect_error) {
die("Failed to connect to database " .
$dbConn->connect_error);
}
?>
Here is the completed code from the last submission of
this question that the user helped me to write:
<!DOCTYPE html>
<?php
//this if statement will execute when the user click on suubmit
button
if($_SERVER["REQUEST_METHOD"] == "POST"){
require_once("conn.php");//connect database
$qty = $_POST['quantity'];//get the value from form
//check the qty variable number or not
//if it is number, diplay a message otherwise it will execute else
part
if(!is_numeric($qty)){
echo "<span>The value entered for the quantity was not a
number.</span>";
}else{
$sql = "SELECT * FROM product where quantityInStock>$qty ORDER
BY QuantityInStock ASC";
$result = $dbConn->query($sql) or die("Problem with
query:".$dbConn->error);
if (!mysqli_num_rows($result) > 0) {
echo "<span>There are no products that have more than $qty in
stock.</span>";
}else{
?>
<h1>Product with stock > <?php echo
$qty;?></h1>
<table border="1">
<tr>
<th>Name</th>
<th>Quantity In Stock</th>
<th>Price</th>
</tr>
<?php
//display the product
while($row = $result->fetch_assoc()){?>
<tr>
<td><?php echo $row["name"]; ?></td> <!--
display result -->
<td><?php echo $row["quantityInStock"];
?></td>
<td><?php echo $row["price"]; ?></td>
</tr>
<?php
}
}
} $dbConn->close();
}
else {
?>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
input[type="text"] {border: 1px solid black;}
</style>
<link rel="stylesheet" href="../css/week10Styles.css">
<title>Week 10 Exercise 5 Form</title>
</head>
<body>
<form id="exercise4Form" method="post" action="<?php echo
$_SERVER["PHP_SELF"];?>">
<h1>Quantity in Stock</h1>
<p>Please enter the quantity to check against stock
levels</p>
<p>
<label for="quantity">Quantity: </label>
<input type="text" name="quantity" size="10" id="quantity"
maxlength="6">
</p>
<p><input type="submit" name="submit"></p>
</form>
</body>
</html>