Arjan Arjan - 4 years ago 107
PHP Question

Value from onSubmit prompt to PHP GET

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?

Answer Source

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'].

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download