1

I am trying to build a PHP calculator with buttons. One of my problems is that I cannot have no more than one digit value. For example, when I try to enter a value, for example, 33, the result becomes 3 in the textbox because the page gets refreshed as I am using submit for all of the numbers. Only one PHP page should be used. this Solution was suggested to me, but it does not solve my problem as it is using more than one page. Also, the more than one digit does not seem to work.

<input type="text" value="<?php
if(isset($_GET['number'])){
$number = $_GET['number'];
echo '<input type="hidden" value="'. print ($number) .'"';
} ?">

also, here is a sample of my HTML

 <input type="submit" value="9" name="number"class="button1">

and here is a picture of my UI An example of my UI

aftermath
  • 29
  • 5

1 Answers1

0

You can use Session variables to preserve data between page loads. Basically, you put this:

session_start();

at the beginning of every page you want to share variables, then you declare/call them like this:

$_SESSION["number"] = 3;

So, if you post/reload/navigate to another page that is part of the session, $_SESSION["number"] will still be 3 for you to add another 3 to it.

Going one step further though, you typically don't want to submit your result on each number you add as an HTML form. What you probably want to use is an AJAX call to just return the contents of the computed field(s). That way you can calculate in real time without having to reload your whole page since it sounds like that is what you are going for.

jQuery's AJAX method is very good for this.

The way that works is that you have 2 php files, 1 is your view, the other is a controler. The view is the page that stays loaded with your form and what ever other GUI elements there are, and the controller gets called every time you make a change in the view. Instead of returning a whole page, the controller is just the math operations so it will just return the number part of what you are doing and you can reload it into your View whereever you need it.

[FOLLOW UP]

For a simple solution that is pure php/html, try something like this:

<?php
  session_start();
  If (!isset($_SESSION['start'])) {$_SESSION['start'] = true;}
  if ($_GET['clear'] == true || $_SESSION['start'] == true){
    $_SESSION['number'] = "";
    $_SESSION['start'] = false;
  }
  if(isset($_GET['number'])){
    $_SESSION['number'] = $_SESSION['number'].$_GET['number'];
  } 
?>
<input type="text" value="<?=$_SESSION['number']?>">
Nosajimiki
  • 1,073
  • 1
  • 9
  • 17
  • Just read the previous comments, to the original question. What framework/CMS/etc are you using that is preventing you from using JavaScript or from interacting with your header? In some cases, like if you are using WordPress, the session is already declared, and you can just use Session variables. – Nosajimiki Oct 08 '18 at 18:55
  • I am not using any special framework or anything. Just pure HTML and PHP. It is a project related to a PHP course. Thank you very much for your suggestion. However, it is requited to use HTML and PHP exclusively. Regards, – aftermath Oct 09 '18 at 14:00