0

I have a modifypassword form that modifys the password in a flat file for a user.( I know its not safe etc. ).

So the thing is: whenever I click on modify password on my website, I get a prompt that asked to enter a new password. All fine, after that it will submit the form. But I cannot get the variable somehow that is typed in. I want to $_GET['newpw'] so I can use it to adjust my flat file.

So I have a form like this:

echo "<td> <form action=\"admin.php\" method=\"GET\" onsubmit=\" modifyPassword();\">

this is the modifyPassword function:

<script type="text/javascript">
    function modifyPassword() {
        var newpw=prompt("Enter a new password");
        if(newpw !== null) {
            $.ajax({
                type: "GET",
                url: "admin.php",
                data: {data: newpw},
                success: function(data) {
                    console.log(data);
                }
            });
        }
    }
</script>

And when the form is actually submitted I want to get the value from what is typed in like this:

echo $_GET['data'];

This is all in the same file. The output of $_GET['data'] does not show anything. The rest just works fine when I choose a static password like: "test". It will update my flat file, but I want to get the user input to change the password.

Can someone tell me what i am doing wrong?

5
  • Where is the PHP code that handles the request? Feb 2, 2017 at 10:29
  • }elseif (isset($_GET['Modify'])){ echo $_GET['data']; // also tried 'newpw'
    – Arjan
    Feb 2, 2017 at 10:35
  • Well... $_GET['Modify'] isn't set in that request so it would never happen Feb 2, 2017 at 10:36
  • you are sending data so use $_GET['data']. Feb 2, 2017 at 10:39
  • Checkout my answer. Feb 2, 2017 at 10:40

2 Answers 2

0

I think the base problem may be, that you have event handler onsubmit, but the form gets submited anyway. So the actual prompt has no chance to execute.

You should add return false; or manage the logic in modifyPassword() method.

For example this may help to stop form to submit: echo "<td> <form action=\"admin.php\" method=\"GET\" onsubmit=\"modifyPassword(); return false;\">"

OR

If you want to submit the form in standard way, just to change the password, just change the input value.

Modify form: echo "<td> <form action=\"admin.php\" method=\"GET\" onsubmit=\"modifyPassword(this)\"><input type=hidden name=newpw />"

And then change the javascript:

<script type="text/javascript">
function modifyPassword(passForm)
        {
            var newpw = prompt("Enter a new password");
            if (newpw !== null)
            {
                passForm.newpw.value = newpw;
            }
        }
</script>

Then you should have the value in php in $_GET['newpw'].

0
0

Your current PHP code:

}elseif (isset($_GET['Modify'])){ echo $_GET['data'];

But according to the Ajax request code, you don't send a Modify parameter so the $_GET['Modify'] isn't set and therefore the condition returns false and never reach to the echo $_GET['data'] part.

You need to add another parameter to the data as in the following:

JS

<script type="text/javascript">
                        function modifyPassword(){

                            var newpw=prompt("Enter a new password");
                            if(newpw !== null){
                                $.ajax({
                                    type: "GET",
                                    url: "admin.php",
                                    data: {action: 'modifypass', data: newpw}, //Added another parameter.
                                    success: function(data)
                                    {
                                        console.log(data);
                                    }
                                });
                            }}
                        </script>

PHP

 elseif (isset($_GET['action']) && $_GET['action'] == "modifypass"){
    echo $_GET['data'];
1
  • Maybe i dont understand your answer clearly, but it does reach the echo statement, its just empty. I also have other code in the elseif isset modify. That gets executed. For example: this gets executed when i submit: echo $_GET['data'] . " <--- new password"; And also other lines beneath this code. Because right now it does modify the password to "test" but i want to modify the password by user input
    – Arjan
    Feb 2, 2017 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.