so I have 2 pages . On the first page I have a form , where I need to input values and the other page receives those values and saves them. Then I go back to page 1 , input new values and those new values get saved again on page 2 , right next to the previous values. Basically like a shopping list. The problem is that i’m really new to PHP and I can’t really find solution how to save these forms next to each other. Massive thanks to anyone who replies.
Page 1 :
JavaScript
x
<fieldset style="width:fit-content; margin:170px; auto 10px; font-size: 30px; justify-content:center;">
<form action="./site2.php" method="post" >
<legend>Product add</legend>
<label>SKU:<br />
<input type="text" name="SKU" pattern="[0-9.]+" required id="SKU" /></label><br />
<label>Name:<br />
<input type="text" name="name" required id="name" /> </label><br>
<label>Price($):<br />
<input required id="price" pattern="[0-9.]+" type="text" name="price" ></label><br />
<input type="submit" name="Submit" value="Save" >
<input type="reset" value="Cancel">
Page 2 :
JavaScript
<form action="site1.php" method="get">
JavaScript
<?php
session_start();
$data=array();
if(!isset($_SESSION['data'])){
$_SESSION['data'] = array();
}
if (isset($_POST)) {
$_SESSION['data'][] = $_POST['SKU'];
$_SESSION['data'][] = $_POST['name'];
$_SESSION['data'][] = $_POST['price'];
}
foreach($_SESSION['data'] as $d){
}
?>
JavaScript
<form id="form-list" >
<fieldset style="width: fit-content; margin:130px; auto 10px; font-size: 20px; justify-content:center; " >
SKU: <?php echo $d; ?><br>
Product name: <?php echo $d; ?><br>
Price($): <?php echo $d; ?><br>
<input type="checkbox" value="asd" id="test" name="test" />
</form>
Advertisement
Answer
It looks like you have read a little, and are trying to build something based on that reading. Good start. but I think you need to take a further dive into the litterature.
Concepts
- PHP is a server side language. everything inside the
<?php
and?>
tags is executed on the server, and not on the client. - HTML is a (most often) client side markup (display) language, and is executed on the client (in the browser)
- When you have both php and html in a php file (and it is sent with the proper headers) the php code is executed, and any
echo
orprint
or<?="hello world"?>
is converted to text on your html page, and the resulting file is executed by the client’s browser - Anything in your
$_SESSION
is available to your PHP sections as they are executed, providing the session (set by the cookie or a path parameter) is the same
What your code is doing
Page 1 (site1.php)
- Displays a html form (could in reality be just a .html file instead, if you have no other php in the file)
Page 2 (site2.php)
- starts a
<form>
element which would submit the form to site1.php - parses the submitted form data and saves it in a session variable
- parses an empty
foreach
loop - displays some html elements with an apparently unset php variable
$d
- creates a checkbox
- closes the
<form>
without a sumbit button, which means you never get back to site1.php
Solution 1: Do everything in a single php file
JavaScript
<!-- input part -->
<html>
<body>
<fieldset style="width:fit-content; margin:170px; auto 10px; font-size: 30px; justify-content:center;">
<form action="" method="post" >
<legend>Product add</legend>
<label>SKU:<br />
<input type="text" name="SKU" pattern="[0-9.]+" required id="SKU" /></label><br />
<label>Name:<br />
<input type="text" name="name" required id="name" /> </label><br>
<label>Price($):<br />
<input required id="price" pattern="[0-9.]+" type="text" name="price" ></label><br />
<input type="submit" name="Submit" value="Save" >
<input type="reset" value="Cancel">
<!-- form input handling -->
<?php
session_start();
$data=array();
if(!isset($_SESSION['data'])){
$_SESSION['data'] = array();
}
if (isset($_POST)) {
$_SESSION['data'][] = $_POST['SKU'];
$_SESSION['data'][] = $_POST['name'];
$_SESSION['data'][] = $_POST['price'];
}
foreach($_SESSION['data'] as $d){
echo "SKU: ".$d['SKU']."<br>".
"Product name: ".$d['name']."<br>".
"Price($): ".$d['price']."<br>";
}
?>
<!-- some link to save.php where you push $_SESSION['data'] to your persistent databse (MySQL/PostgresQL/whatever)-->
</body>
Solution 2 (advanced):
- keep your setup with site1.php and site2.php, but keep site2.php as pure php without html, and return the data as json or xml
- use ajax (javascript) to submit the form to site2.php and parse the return data to a visible format
- have a third file (e.g. save.php) where you do the saving to a dbms
Solution 3: keep your code and fix the issues
- put your display code inside the
foreach
(see my code) - remove the
<form>
from site2.php and simply add a<a href="site1.php">add more items</a>
link at the bottom to go back to the input page