0

I am trying to display the output of either the password or mobile invalid statement to a HTML Page. Here is my code:

<?php
  session_start();
  if(isset($_POST['register'])) {
    $phone = ($_POST['tphone']);
    $upass = ($_POST['tpassword']);     
    if (!@preg_match('/^(?=.*\d{3,})(?=.*[A-Za-z]{5,})[0-9A-Za-z!@#$%]{8,32}$/', $upass)) {
      $upass = 'You have enter an invalid password';
    } 
    else if ( !@ereg( "^[0-9]{3}-[0-9]{7}$", $phone)) {
      $mobile = 'Invalid mobile'
    }
  }
?>
<html>
  <body>
    <p><?php echo $upass ?>.</p>
    <form method="post">
      <input type="text" name="tphone" placeholder="First Name" class="form-name"  required>
      <input type="password" name="tpassword" placeholder="Last Name" class="form-name2" required>
      <button type="submit" name = "register" class="btn-register">Register</button>  
    </form>
  </body>
</html>

The problem here is how should I use the PHP variable in my HTML like I have defined $upass inside the p tags. I also want to set this variable when the register button is clicked.

Right now when I echo $upass, it displays Undefined variable when I have not selected the register button.

Vivek Pradhan
  • 4,777
  • 3
  • 26
  • 46
xodebox95
  • 93
  • 2
  • 4
  • 13
  • 1
    Possible duplicate of [How to call a PHP file from HTML or Javascript](http://stackoverflow.com/questions/20637944/how-to-call-a-php-file-from-html-or-javascript) – Abhishek Gurjar Jun 21 '16 at 15:51
  • 3
    `ereg` is deprecated/dead/gone. You should NOT be using that in any new code, and using `@` is the PHP equivalent of stuffing your fingers in your ears and going "lalalalalala can't hear you". – Marc B Jun 21 '16 at 15:54
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Gerald Schneider Jun 21 '16 at 17:55

2 Answers2

0

Just assign the error message to a variable then check if that variable is not not null. If it has a string value in it then show the <p> with the error message in it.

<?php
session_start();
$errorMessage = null;

if (isset($_POST['register'])) {
    $phone = $_POST['tphone'];
    $upass = $_POST['tpassword'];     

    if (!@preg_match('/^(?=.*\d{3,})(?=.*[A-Za-z]{5,})[0-9A-Za-z!@#$%]{8,32}$/', $upass)) {
        $errorMessage = 'You have enter an invalid password';
    } elseif (!@ereg( "^[0-9]{3}-[0-9]{7}$", $phone))  {
        $errorMessage = 'Invalid mobile'
    }
}
?>
<html>
<body>
     <?php if ($errorMessage) { ?>
       <p><?php echo $errorMessage; ?></p>
     <?php } ?>
     <form method="post">
        <input type="text" name="tphone" placeholder="First Name" class="form-name"  required>
        <input type="password" name="tpassword" placeholder="Last Name" class="form-name2" required>
        <button type="submit" name = "register" class="btn-register">Register</button>  
      </form>
</body>
</html>
Kal
  • 2,239
  • 6
  • 36
  • 74
  • the problem with this is that it still displays invalid password, even though a button has not been triggered – xodebox95 Jun 21 '16 at 16:08
0

You need to understand that PHP is run on the web server to generate the HTML page that is sent to the browser.

That means that when PHP is finished running is when the user receives its output. If you need something to happen after a user action, then something must be done in the front-end (browser) to trigger it, typically using JavaScript.

If you want the front-end to then send data back or query something on the web server, you need to use either AJAX (or its variants), or WebSockets to allow JavaScript from the browser to pass a request directly to the web server without reloading the page.

Julie Pelletier
  • 1,740
  • 1
  • 10
  • 18