2

I've spent the last hour or so trawling through here and Google without success. I have a form that I want to submit via POST. The javascript runs and validates fine, it even 'submits' the form. But when it does, it just reloads the page. No POST headers are sent (confirmed using Firebug in Firefox). It's as though it simply reloads the page rather than submitting it.

This form is embedded in the addUser.php page.

<form action="addUser.php" method="post" name="NewUser">
<div data-role="field-contain">
 <label label-for="EmpID" class="ui-hidden-accessible">Enter Employee ID</label>
 <input name="EmpID" id="EmpID" placeholder="Employee ID" value="" autocomplete="off" title="Enter Employee ID" type="number" required autofocus>
 <label label-for="UserName" class="ui-hidden-accessible">Enter username</label>
 <input name="UserName" id="UserName" placeholder="Username" value="" autocomplete="off" title="Enter the new username" required>
 <p>
 <label label-for="NewPass1" class="ui-hidden-accessible">Enter Your Current Password</label>
 <input name="NewPass1" id="NewPass1" placeholder="New Password" value="" type="password" autocomplete="off" title="Must be at least 8 characters, with numbers and upper and lower case letters." required>
 </p>
 <p>
 <label label-for="NewPass2" class="ui-hidden-accessible">Enter Your Current Password</label>
 <input name="NewPass2" id="NewPass2" placeholder="Verify Password" value="" type="password" autocomplete="off" title="Re-enter your chosen new password." required>
 </p>
</div>
<button type="button" name="submitButton" value="submitButton" data-theme="d" data-inline="true" data-icon="check" data-iconpos="left" onclick="validateForm ()">
 Add User
</button>

And here is the Javascript:

function validateForm()
{
 var reason = "";
 reason += validateEmpID();
 reason += validateUserName();
 reason += validateNewPassword();
 if (reason != "") {
 alert("There are some errors with the data you entered:\n" + reason);
 return false;
 } else {
 document.NewUser.submit();
 }
}

I have tried various different methods of calling submit, all with the same result. Any help greatly appreciated.

Thanks Gareth

asked Mar 20, 2013 at 4:52
4
  • Have u tried var_dump($_POST) to see what is in $_POST? Commented Mar 20, 2013 at 4:58
  • 1
    did you miss </form> tag ? Commented Mar 20, 2013 at 5:00
  • slacker - I just missed it in the copy-paste, it is in the HTML. Kishor - I haven't. I might give it a go just to see what it's showing. Commented Mar 20, 2013 at 5:12
  • You wouldn't read about it - after running some jQuery entries, I went back to the straight javascript (exactly as above). And it works!!! GAH! Commented Mar 20, 2013 at 5:18

4 Answers 4

1

I think it should be

document.forms['NewUser'].submit();

or

document.forms.NewUser.submit();
answered Mar 20, 2013 at 4:59
Sign up to request clarification or add additional context in comments.

Comments

1

I think somewhere you are missing adding return false that is the reason your browser is asking for reloading the form. And better way to submit is to use submit button instead of using simple button.

answered Mar 20, 2013 at 6:24

Comments

0

Why not use an AJAX post? Or better yet a jQuery AJAX POST?

$.post(url, data, datatype);

http://api.jquery.com/jQuery.post/

answered Mar 20, 2013 at 4:58

1 Comment

Never actually used jQuery (well, apart from formatting controls). This has it inserting the data, now I need to do some research on displaying the results!
0
function validateForm(event) {
 var reason = "";
 reason += validateEmpID();
 reason += validateUserName();
 reason += validateNewPassword();
 if (reason != "") {
 alert("There are some errors with the data you entered:\n" + reason);
 event.preventDefault();
 }
}
document.getElementById('my_form').addEventListener('submit', validateForm, false);
answered Mar 20, 2013 at 5:15

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.