0

First off, if there is a completely better way of doing this? let me know... This way seems kind of "hackish". Here is what I am trying to do: As you see in the below input, I have a text field that I simple want to appear like an <a> link. If the user clicks it, I want JavaScript pop up box to appear with user entering the email. I want them to hit ok, and I send that email value back to the form and the form then submits via a php post.

I do not know how to grab the value from the box and insert it in the form (if possible) so it can submit to php.

Here is the code:

    <form id="frm1" action="login.php" method = "post">
        <input onclick="resetPassword()" type="text" name="email" value="Reset Password" style="cursor: pointer; background:none; text-decoration: underline; border: none;"  />
    </form>


    <script>
        function resetPassword() {
        var x;

        var email=prompt("Enter email to reset password.", "[email protected]");

        if (email!=null)
          {
          document.getElementById("frm1").submit();
          }
        }
   </script>


  <?php 
     if (isset($_POST['email'])) {
         $email = $database -> escape_value(trim($_POST['email']));
        //  reset password    
      }
  ?>
3
  • You can use either jquery event post() or javascript post method: stackoverflow.com/questions/133925/… or api.jquery.com/jQuery.post Commented Jul 25, 2013 at 15:41
  • are you sending the data ..i dont see the code here Commented Jul 25, 2013 at 15:45
  • In the php part I email the new password to the user; I cut that out because that is after the fact. I simple need to send the users current email address TO php first. Commented Jul 25, 2013 at 15:51

3 Answers 3

1

Grab the email from promt, paste it to input field and submit the form.

<form id="frm1" action="login.php" method='post'>
    <input id="email" onclick="resetPassword()" type="text" name="email" placeholder="Reset Password" />
</form>

<script type="text/javascript">
  function resetPassword() {    
    var email = prompt("Enter email to reset password.", "[email protected]");

    if (email != null) {
      document.getElementById("email").value = email; 
      document.getElementById("frm1").submit();
    }
  }
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

Edit: Ok, the email IS being set, but when I get to php it is a blank value.
I assume the edit only was method='post'? I already had that actually cause I noticed earlier it was missing. everything else looks identical... exept I have inline CSS and a readonly in the form
document.getElementById("email").val = email was changed to document.getElementById("email").value = email
1

A better aproach would be the use of jQuery and send the info of the text field via AJAX to the script which is expecting the $_POST variable. This way, the <form> element would be unnecessary.

    function resetPassword() {
        var x;
        var email=prompt("Enter email to reset password.", "[email protected]");
        if (email!=null) {
             $.post( url_of_the_script, {email: email}, function() { alert('Email sent'); } );
        }
    }

Comments

1

Add an ID to the input field:

<form id="frm1" action="login.php">
    <input id="email" onclick="resetPassword()" type="text" name="email" value="Reset Password" style="cursor: pointer; background:none; text-decoration: underline; border: none;"  />
</form>

    function resetPassword() {

    var email=prompt("Enter email to reset password.", "[email protected]");

    if (email!=null)
      {
      document.getElementById("email").value = email; // Put value into form field
      document.getElementById("frm1").submit();
      }
    }

If you don't want the user to be able to type directly into the form field, you should give it the readonly attribute. onclick prevents them from clicking on it, but they could still get there with Tab.

3 Comments

Great catch on readonly. Though the other code still isn't pasting it over.
Sorry, it should be .value (I usually use jQuery, which has .val() for the analogous operation).
Yup, the other guy figured that one out too. I could have marked you both correct, but had to pick one... thanks for your incite on this. The least I can do it upvote the answer!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.