I have been using different script for auto submitting form like:
document.getElementsByTagName('form')[0].submit();
But all these refresh for endless time without submitting the form. I tried using alert statement which worked fine but still the form was not submitted.
<form id="level" method="post">
<br/>
<label for="answer">Answer:</label>
<input type="text" name="answer" id="answer" />
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
.
With all your questions, I could try another conclusion.
If you're trying to make a brute-force attempt to retrieve a password, the server could have identified your ip and just stall your request because you've tried too often. Or even notice you're inhumanly fast with your request. These kind of things are a base defense against brute-force attacks.
The code should work fine.
Another guess could be that the page actually does something different when clicking that button, in stead of the normal submit. So you could try simulating a click on the button in stead of submitting the form directy.
$('#submit').click();
that line of code is right, maybe it is DOM issue, there is another <form> before or something else
you can try to access by ID
or jQuery :
$("#level").submit();
Related
I'm using this simple form to implement a login:
<form action="/handleLogin/" name="login" method="post">
<div>
<label for="userid">Username</label>
<input type="text" id="userid" name="identifier" value="" pattern="\s*([^#\s]+#[^#\s]+|\d+)\s*" data-parsley-required>
</div>
<div>
<label for="userpassword">Password</label>
<input type="password" id="userpassword" name="credential" data-parsley-required>
</div>
<button id="login-button" class="button" type="submit">Login</button>
</form>
Our QA team noticed that it's possible to click the login-button twice or hammer the return button on the keyboard two times in a row within a short period of time.
This leads to multiple requests being sent to the server which causes problems.
Thus, I tried to prevent this with the following JavaScript code:
$('form[name="login"]').on('submit', function() {
$("#userid, #userpassword, #login-button").attr("disabled", true);
});
Unfortunately, now the values of username + password won't be sent to the server anymore. I guess that's because they're disabled and the browsers thinks: "Ok, if the fields are disabled, I'm not gonna send em".
How can I work around this problem?
I thought about canceling the original submit event using "return false;" and manually send a copy of the form but that's not really a very good solution, I think.
Try this:
$('form[name="login"]').on('submit', function() {
$("#login-button").attr("disabled", true);
$("#userid, #userpassword").attr("readonly", true);
});
Use
$('button[type=submit], input[type=submit]').prop('disabled',true);
instead of:
$("#userid, #userpassword, #login-button").attr("disabled", true);
It will only disable button.
Demo Link
I'm working on a single PHP file, which has 2 different forms.
Example:
<form action="index.php" method="POST">
<input type="checkbox" name="box1">
<input type="submit" value="submit1" name="submit">
</form>
<br>
<form action="index.php" method="POST">
<input type="checkbox" name="box2">
<input type="submit" value="submit2" name="submit">
</form>
My problem is, I want to make both of them work at the same time, independent from each other. For example, when I hit 'submit1', the whole index.php reloads since action is set to that page. The other checkbox might lose it's condition, if I set it to checked before submitting the first form. Might be confusing, I know.. Since I have PHP code behind, I can' really handle the whole thing between 1 form tag. That's why I'm asking if there's another option like javascript, or something. Thanks in advance!
You can use a javascript cookie. You could set it so the cookie will have the fields and values of everything in both forms, and then is saved/created upon submit. Then once the page is reloaded, javascript can split the cookie and refill the field values for the other form. You might need a hidden field in both forms so that you can identify which form was submitted. Here's a tutorial that might explain cookies to you in greater detail: http://www.tutorialspoint.com/javascript/javascript_cookies.htm
I have a form, which has a few different submit buttons on it all doing different things with the same post data.
Lets say for simplicity sake the form looks like this:
<form method="post">
<input type="hidden" name="ids" value="1,2,3,4" />
<input type="submit" id="picking" name="picking" value="Picking" />
<input type="submit" id="shipping" name="shipping" value="Shipping" />
<input type="submit" id="invoice" name="invoice" value="Invoice" />
</form>
At the moment the form submits to itself and I work out server side which submit button is pressed, build a URL from the POST data, then do a PHP redirect to what I need to go. This works fine.
However, I am looking for the form to post its data to a new window, but only when "invoice" is clicked. This rules out just adding target="_blank" to the form, as the other 2 buttons would submit to new pages as well.
I also can't split the form into 3 different forms as the data is a lot more complex than the above, and a lot of it is input by the user.
Is there a way to do this using JavaScript/JQuery? If so, where would I start?
Thanks
could you not add target blank to the form when invoice is clicked?:
$("#invoice").click(function(){
$('#form_id').attr('target', '_blank');
});
or:
$(document).on("click","#invoice",function(){
$('#form_id').attr('target', '_blank');
});
Try adding a click handler to the correct submit button.
$('#invoice').on('click', function(){
//doStuff
});
This will allow you to control the action of #invoice without affecting the others.
Question: How can you send a form with Javascript if one form input has the name submit?
Background: I am redirecting the user to another page with a hidden HTML form. I cannot change name on the (hidden) inputs, since the other page is on another server and the inputs need to be exactly as they are. My HTML form looks like this:
<form id="redirectForm" method="post" action="http://www.example.com/">
<input name="search" type="hidden" value="search for this" />
<input name="submit" type="hidden" value="search now" />
</form>
I use the following javascript line to send the form automatically today:
document.getElementById('redirectForm').submit();
However, since the name of one input is "submit" (it cannot be something else, or the other server won't handle the request), document.getElementById('redirectForm').submit refers to the input as it overrides the form function submit().
The error message in Firefox is: Error: document.getElementById("requestform").submit is not a function. Similar error message in Safari.
Worth noting: It's often a lot easier to just change the input name to something other than "submit". Please use the solution below only if that's really not possible.
You need to get the submit function from a different form:
document.createElement('form').submit.call(document.getElementById('redirectForm'));
If you have already another <form> tag, you can use it instead of creating another one.
Use submit() method from HTMLFormElement.prototype:
HTMLFormElement.prototype.submit.call(document.getElementById('redirectForm'));
I'm implementing a voting system like the one used by stackoverflow. It's working with ajax sending POST request to an url. I'd like a way to fail gracefully when javascript/ajax isn't supported or enabled, a GET like /voteup/id isn't even considered because i'm altering the database.
What's the best solution? I'm either considering a form or simply removing the feature if js isn't enabled.
There are at least three related entries on SO but i can't insert more than one hyperlink
POST with links without JavaScript
Make the basic voting actions mini-forms, then use javascript to disable their posting action.
<form method=post action="hit-url">
<input type=hidden name=vote value="1" />
<input type=submit value="Vote Up" onSubmit="doVote(1);return false;" />
</form>
<form method=post action="hit-url">
<input type=hidden name=vote value="-1" />
<input type=submit value="Vote Down" onSubmit="doVote(-1);return false;" />
</form>
To replace these with links for javascript-enabled users:
<div id="voteupbutton">
<form method=post action="hit-url">
<input type=hidden name=vote value="1" />
<input type=submit value="Vote Up" onSubmit="doVote(1);return false;" />
</form>
</div>
<script>
document.getElementById("voteupbutton").innerHTML="<a href='script:return false' onClick='doVote(1);return false;'>Vote up</a>";
</script>
I haven't tested the above. If you're using jQuery or some other framework, there will be more elegant ways of doing all of this.
The straightforward option is just a regular form POST, even if it is to the URL /voteup/id, and I'm not sure why you can't do that (or even the GET you mentioned).
Put onsubmit="return false" into the tag to prevent POSTing by users who do have JS enabled.
While you can't use links to submit forms, you can certainly use links to trigger database actions if you want to, via the querystring. In no particular scripting language:
<===
if (querystring("v")) then {
v.value.writeToDatabase
}
===>
Vote A, Vote B