1

I have a form to be filled in and a popup javascript alert will be displayed if the Password and Re-Confirm Password does not match. However, once I click "OK"on the popup alert, the whole form is reset. But I just want the password to be blank again, not the whole form.

I tried this way:

if($pwd != $pwd2) {
    echo("<script type='text/javascript'>alert('Password does not match!')</script>");
}

I also tried the one below but still the same thing happened:

if($pwd != $pwd2) {
    ?>
    <script type="text/javascript">
        alert("Password does not match!");
    </script>
    <?php
}
danopz
  • 3,310
  • 5
  • 31
  • 42
NFSJ
  • 101
  • 8

4 Answers4

0

Your validation is on server side, once you hit the server the form gets reset everytime, for showing validation error from server you need to pass the values to the form again with errors. If you want to show the alert on validation error, use client side validation with jQuery or simple JS. In this way your form's values remain the same and the alert will be popped up.

Muhammad Nauman
  • 1,249
  • 8
  • 10
0

if you really need using php

<?php
$pwd = isset($_REQUEST['pwd']) ? $_REQUEST['pwd'] : "";
$pwd2 = isset($_REQUEST['pwd2']) ? $_REQUEST['pwd2'] : "";

if($pwd != $pwd2) {
    echo("<script type='text/javascript'>alert('Password does not match!')</script>");
}
>
<input name"pwd" value="<?php echo $pwd; ?>">
<input name"pwd2" value="<?php echo $pwd2; ?>">
uingtea
  • 6,002
  • 2
  • 26
  • 40
0

If you want other input fields not to be empty after submission you should try this using php. Let's say your input field is username.

$username = $_POST['username'];
<input type="text" name="username"  value="<?php echo htmlentities($username); ?>">

When you use this, after you clicked ok and get the alert, your username field will not be empty. The value you entered will appear in the text field.

Buwaneka Sudheera
  • 1,277
  • 4
  • 17
  • 35
0

Try this code to solve your problem.

$username = $_POST['username'];
$password = $_POST['password'];

<input type="text" name="username"  value="<?php echo htmlentities($username); 
?>">
<input type="password" name="password">