0

Hi guys the issue I have is within my users admin page, I have displayed all the admin users within a table each assigned a delete and edit button.

When I click on edit the given users details appear in a modal form. Within the edit details there is a password and confirm password box, when I click on the edit details for the first user, the validation for confirm password appears as "passwords don't match" however when I click on the second user the confirm password does not validate.

Below is the source code I am having issues with any help will be much appreciated:

<a rel="tooltip" title="Edit" id="e<?php echo $id; ?>" href="#edit<?php echo $id; ?>" data-toggle="modal" class="btn btn-success"></i>Edit</a>

<div id="edit<?php echo $id;?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-body">
        <div class="alert alert-info"><strong>Edit User</strong></div>
        <form class="form-horizontal" method="post">
            <div class="control-group">
            <label class="control-label" for="inputEmail">Firstname</label>
            <div class="controls">
                <input type="text" id="inputEmail" name="firstname" value="<?php echo $row['firstname']; ?>" required>
            </div>
            </div>

            <div class="control-group">
            <label class="control-label" for="inputEmail">Lastname</label>
            <div class="controls">
                <input type="text" id="inputEmail" name="lastname" value="<?php echo $row['lastname']; ?>" required>
            </div>
            </div>

            <div class="control-group">
            <label class="control-label" for="inputEmail">Mobile Number</label>
            <div class="controls">
            <input type="text" id="inputEmail" name="mobilenumber" value="<?php echo $row['mobilenumber']; ?>" required>
            </div>
            </div>

            <input type="hidden" id="inputEmail" name="id" value="<?php echo $row['admin_id']; ?>" required>
            <div class="control-group">
            <label class="control-label" for="inputPassword">Password</label>
            <div class="controls">
                <input type="text" name="password" id="password" required>
            </div>
            </div>

            <div class="control-group">
            <label class="control-label" for="inputPassword">Confirm Password</label>
            <div class="controls">
                <input type="text" name="confirmpassword" id="confirmpassword" required>
            </div>
            </div>

            <div class="control-group">
            <div class="controls">
                <button name="edit" type="submit" class="btn btn-success">&nbsp;Update</button>
            </div>
            </div>
        <script type="text/javascript">
            window.onload = function () {
            document.getElementById("password").onchange = validatePassword;
            document.getElementById("confirmpassword").onchange = validatePassword;
            }
            function validatePassword(){
            var confirmpassword=document.getElementById("confirmpassword").value;
            var password=document.getElementById("password").value;
            if(password!=confirmpassword)
            document.getElementById("confirmpassword").setCustomValidity("Passwords Don't Match");
            else
            document.getElementById("confirmpassword").setCustomValidity('');    
            //empty string means no validation error
            }
        </script>
        </form>
    </div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">&nbsp;Close</button>
    </div>
</div>
<?php
    if (isset($_POST['edit'])){

    $admin_id=$_POST['id'];
    $firstname=$_POST['firstname'];
    $lastname=$_POST['lastname'];
    $mobilenumber=$_POST['mobilenumber'];
    $password= sha1 ($_POST['password']);

    mysql_query("update admin set firstname = '$firstname', lastname = '$lastname', mobilenumber = '$mobilenumber', password = '$password' where admin_id='$admin_id'")or die(mysql_error()); ?>
    <script>
    window.location="adminusers.php";
    </script>
    <?php
    }
    }
    ?>

Validation issue

Validation issue 2

Umar
  • 1
  • 1
  • Welcome to Stack Overflow! It would helpful for us to answer this if you are more clear about what the *exact* problem is that you're having. What is the behavior now, and what do you want it to be? – Frank Bryce Dec 04 '15 at 18:24
  • Hi John, Thanks for your reply, I have just added a screenshot (Validation issue) to my post above of how its working for the first user, the issue I am having is that when I click on edit for any other users apart from the first user the validation for confirm password "passwords don't match" does not appear. – Umar Dec 04 '15 at 18:37
  • In the screen shot that you've provided, the passwords do not match so the behavior from what I understand is correct. If you click on another user, and enter the same passwords does this problem still happen? – Frank Bryce Dec 04 '15 at 18:41
  • Yes your right the behavior in the screenshot is correct for the first user however when I click edit for another user and type in different passwords the validation "passwords don't match" does not appear it just closes the modal form. – Umar Dec 04 '15 at 18:47
  • My only idea from the code that you've posted and your description of the problem is that `getElementById()` may be searching through your document and finding the first element that has that Id. If there are multiple elements with id `confirmpassword` or `password` this would be a problem. Is this a section of your code, and there is more? A side note, hitting F12 in your browser will let you set breakpoints in your javascript code and help you debug :) – Frank Bryce Dec 04 '15 at 19:01
  • Thanks for the suggestion, Yes this is a section of the code, the only multiple element was id:password I have tried changing it to password1 so it doesn't conflict however the result is still the same. How do I debug in google chrome? Do I just hit f12 while in the modal form? – Umar Dec 04 '15 at 19:18
  • [javascript debugger in chrome](http://stackoverflow.com/questions/66420/how-do-you-launch-the-javascript-debugger-in-google-chrome). I would recommend looking at the ["Elements Tab" in the developer tools](https://developer.chrome.com/devtools#devtools-window) of chrome as well for more elements with the same id :) – Frank Bryce Dec 04 '15 at 19:21
  • Thanks for your help John I will look into this and see what I can do. – Umar Dec 04 '15 at 19:29
  • No problem, happy coding! – Frank Bryce Dec 04 '15 at 19:30
  • Hi John I have added another screenshot (Validation issue 2) which may make the issue I am having more clearer to you. As you can see in the second screenshot no validation is being displayed even though the passwords don't match. – Umar Dec 04 '15 at 20:57
  • I understood what you meant a few comments ago. However, I don't have enough information to fix it for you. I would suggest posting your code to jsfiddle.net and linking it, or make you sample smaller to show what's wrong and post it inline on Stack Overflow. If you are struggling to do either of these things, then after you've tried for a while come back here with problems you have doing this. – Frank Bryce Dec 04 '15 at 21:03
  • I have tried with jsfiddle.net however it's not possible to replicate the issue in there as I can't display multiple users to show the problem occurring. All I can gather so far is when loading the modal form for the first user it executes the javascript however when using the same modal form for other users its skipping the javascript. – Umar Dec 04 '15 at 22:30
  • I have added the php code which executes after the user clicks on update, I was wondering if there's a way to add the javascript within the php code so that before the update query runs it must run the javascript? – Umar Dec 05 '15 at 11:45

0 Answers0