Why does not display a random number on the page?

index.php :

<form action="random.php" method="POST"> <h1>Random number is <span id="rnd-num"> <?php include_once 'random.php' ?> </span></h1> <br> <input type="text" class="form-control input-lg inp-rand" placeholder="Min (not necessary)" name="min"/> <input type="text" class="form-control input-lg inp-rand" placeholder="Max (not necessary)" name="max" /> <p><a type="submit" class="btn btn-primary btn-lg btn-rand" role="button">Random </a> </p> </form> 

random.php :

 <?php $min = $_POST['min']; $max = $_POST['max']; echo rand($min, $max); ?> 

    2 answers 2

    When connecting random.php , $_POST empty and has neither 'min' nor 'max'

    The <a> tag has no type attribute, so the click does not work.
    Instead, you can use <input type='submit'> or <button>Random</button>

     <form action='random.php' method='POST'> <input placeholder='Min (not necessary)' name='min'> <input placeholder='Max (not necessary)' name='max'> <button>Random</button> </form> 

    We use the ternary operator and, in any case, declare a variable.
    Through isset check the presence of 'max' in $_POST .
    In the presence of $max = $_POST['max'] , in the absence of $max = 10

     $min = isset($_POST['min']) ? $_POST['min'] : 0; $max = isset($_POST['max']) ? $_POST['max'] : 10; echo rand($min, $max); 

    In order not to open a new page, you can put everything in one document, then the action must be empty so that the request returns to the same page.

    index.php

     <?php $min = isset($_POST['min']) ? $_POST['min'] : 0; $max = isset($_POST['max']) ? $_POST['max'] : 10; echo "Random number is ".rand($min, $max); ?> 
     <body> <form action='' method='POST'> <input placeholder='Min (not necessary)' name='min'> <input placeholder='Max (not necessary)' name='max'> <button>Random</button> </form> </body> 

      There are two errors:

      Because you have $min and $max not set at the moment when the form has not yet been sent to the server.

      At the moment when the form is shown on the screen, there is no $_POST , but when the form is filled, then you will be $_POST .

      But in order to fill in the data and submit the form, you must also click the button But you do not have a button. Add a button.

       <form action="random.php" method="POST"> <h1>Random number is <span id="rnd-num"> <?php include_once 'random.php' ?> </span></h1> <br> <input type="text" class="form-control input-lg inp-rand" placeholder="Min (not necessary)" name="min"/> <input type="text" class="form-control input-lg inp-rand" placeholder="Max (not necessary)" name="max" /> <button type="submit" class="btn btn-primary btn-lg btn-rand" role="button">Random </button> </form> 

      Well, one more thing, I will mention for every fireman: both files must be in the same directory (well, you never know).

      • And as the number that falls, show "<span id =" rnd-num "> (right here) </ span>", so that when you click on a button, it doesn’t open a new page? - Roman Kravets 4:41 pm
      • Merge both files into one file. If $ _POST ['min'] and $ _POST ['max'] are set, then output a number. If not set - show the form (as an alternative - always output the form so that you can generate a new number again) - AK ♦