Skip to content
Advertisement

How to submit values in form and save them on another page [PHP]?

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

Page 2 :

JavaScript
JavaScript
JavaScript

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

  1. PHP is a server side language. everything inside the <?php and ?> tags is executed on the server, and not on the client.
  2. HTML is a (most often) client side markup (display) language, and is executed on the client (in the browser)
  3. 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 or print or <?="hello world"?> is converted to text on your html page, and the resulting file is executed by the client’s browser
  4. 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

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
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement