0

I have this piece of Javascript:

<script type="text/javascript" src="sha512.js"></script>
<script type="text/javascript" src="forms.js"></script>

<script>
            function loadXMLDoc(form, password) 
            {
            var xmlhttp;
            if (window.XMLHttpRequest)
              {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
              }
            else
              {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
            xmlhttp.onreadystatechange=function()
              {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
               {
                   document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
                   formhash(form, password);
               }
              }
            xmlhttp.open("POST","post.php",true);
            xmlhttp.send();
            }
</script>

And some HTML:

<div id="myDiv">
    <form action="register.php" method="post">
       Email: <input type="text" name="email" /><br />
       Username: <input type="text" name="username" /><br />
       Password: <input type="password" name="p" id="password" /><br />
       <input type="button" value="Register" onclick="loadXMLDoc(this.form, this.form.password);" />
    </form>
</div>

The Javascript code above has the job to run some Ajax with PHP and after that, run a encryption function (formhash). When the Ajax is run, it reads a page named "post.php" the post.php's job is to check if any of the fields are empty, before the password is encrypted.

My problem is that post.php can't remember that there is any values from the Register form which I posted above. post.php has to remember what the user typed in the fields, in order to see if they are empty or not. Any ideas on, how I can transfer those values to the post.php file?

1
  • xmlhttp.send(string); If you are using POST Method you should manually generated query string passed as a string in send() function. e.g. var post_query_string = "[email protected]&name=abc&p=myp"; xmlhttp.send(post_query_string); Commented Sep 30, 2013 at 9:41

2 Answers 2

1

You can try:

   parameters= 'username='+ form.elements[0].value +'&' +'password=' +form.elements[1].value ;
   xmlhttp.open("POST","post.php",true);
   xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
   xmlhttp.send(parameters);
Sign up to request clarification or add additional context in comments.

3 Comments

This looks simple enough, will it then transfer the parameters to the post.php document? I will have to try this a bit later
You are going to get into trouble with the stack police for supplying a w3schools resource - whether this specific info is accurate or not.
This worked well, I just had to change the password parameter to 2, as the form starts with email(0), username(1) and then password(2).
1

looks like you are not submitting any data to server in the below line:

            xmlhttp.open("POST","post.php",true);

You can try below code to send data:

xmlhttp.open("POST","post.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("[email protected]&username=Ford&p=pass123");

2 Comments

Ah awesome, what is the email thing about?
i just passed all form elements. I think you need only the password.

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.