Javascript - submit button fails to call form action - javascript

Hi i simplified my register form as it wasn't calling the form action when the button was click. Now the form on contains a submit button, yet it still fails to call the action of the form. I can't see where there could be any problems. Thanks in advance for your help.
Form:
<div id="test">
<form id="test" name="test" method="post" action="test.php" >
<input type="submit" />
</form>
</div>
test.php
<?php
echo "test";
?>

Related

submitting a form via jquery removes submit input variable

Here's my HTML:
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="test" method="get" action="">
<input type="hidden" name="var1" value="true" />
<input type="submit" name="var2" value="submit" />
</form>
<script>
$("#test").submit();
</script>
The resultant request that that makes has var1 in it but not var2. My question is why and what can I do to get var2?
Here's a live demo:
http://www.frostjedi.com/terra/dev/submit.php
try: method="post" in form or use <button type="submit"></button>
A form should submit the value of a submit button only if it's clicked to submit the form (see HTML5 4.10.22.4 Constructing the form data set). Calling the submit method doesn't click the button, so it doesn't submit the value.
The code you've posted will endlessly submit the form, thanks.
You can call the click method of the button, but that may not work (i.e. submit the button's value) everywhere:
<form id="test" method="get" action="">
<input type="hidden" name="var1" value="true">
<input type="submit" name="var2" value="submit">
</form>
<button onclick="$('#test')[0].var2.click()">submit form</button>
Or, as user3701524 suggests, use method=post if that suits.
The value on button will only be submitted when u actually CLICK that button. To make it all work nice, I recommend binding to
$('form').on('submit', function(e) {
// here you have the button value if it was clicked.
e.preventDefault(); // optional this prevent default submission.
});
either way, calling $('form').submit() will NOT send the button value because is not triggered by the button itself.
Hope it helps!

Clearing input data on submit after opening it in a new tab

Anyways, straight to the point. I am in need of some help on getting my form submit button to clear the input field as well as send the information that was typed. Not very detailed, right?
Here's an example. Person types in field. Person clicks submit. Submitted information is then opened in a new tab. The original tab with the input field is then refreshed to remove the text in the input field.
This is my current form. I tried implemented javascript. It worked for the removing the information on click, but didn't send the information to the handler. Can you tell me how to fix this?
<form class="form-inline" role="form" method="post" target="_blank" action="derp.php">
<input id="banfix" type="text" name="player" class="form-control" placeholder="Player Username">
<button type="submit" class="btn btn-default" onclick="document.getElementById('banfix').value='';return false;">Search</button>
</form>
I don't mind it being in php or javascript. I just need to know how to fix my problem.
Using jquery:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
//clear data
$('.form-inline').on('submit','.form-inline',function(){
var value = $("#banfix").val();//now you have the value
$("input[type=text], textarea").val("");
});
});
</script>
could reset your form after submitting
$(".myform")[0].reset();
Use onsubmit(), it will work when the user click the button or just press enter
<form class="form-inline" role="form" method="post" target="_blank" action="derp.php" onsubmit="document.getElementById('banfix').value='';">
<input id="banfix" type="text" name="player" class="form-control" placeholder="Player Username">
<button type="submit" class="btn btn-default">Search</button>
</form>
You can do the opening new window in derp.php file after submitting the form.
<?php
$banfix = $_POST['banfix'];
$new_url = "YOUR_URL_HERE?fix=".$banfix;
echo "<script type='text/javascript'>window.open('".$new_url."','_blank');</script>";
http_redirect('YOUR_FORM_URL_HERE');
die();
?>
<form class="form-inline" role="form" method="post" target="_blank" action="derp.php">
<input id="banfix" type="text" name="player" class="form-control" placeholder="Player Username">
<button type="submit" class="btn btn-default" onclick="setTimeout(function() {
document.getElementById('banfix').value='';return false;
}, 100);">Search</button>
</form>
I added a set timeout function to my onclick. My problem was that the information being sent was being cleared before it was sent. So I set a 100 tick timeout so the info can be sent first. :3

Hide Div on submit

I coded a html form and would like to hide a div upon submission using javascript. For some reason the div isn't hiding.
DIV & Form code:
<div id="map"><iframe src="sourceurl" width="700" height="300" frameborder="0"></iframe></div><br />
<form method="get" action="<?php echo $url = basename($_SERVER['PHP_SELF']); ?>" onsubmit="mapHide(); return false;">
<select name="country" onchange='this.form.submit()'>
<input type="hidden" name="action" value="submit" /><br>
</select>
</form>
JS:
<script>
function mapHide() {
document.getElementById('map').style.display = 'none';
}
</script>
Seems like this would be easier just using php. Check if the the form has been submitted and display content accordingly.
if($_SERVER['REQUEST_METHOD'] == 'POST'){
//form was submitted content
}
else{
//empty form
}
you probably found a code meant for onsubmit and used it for an onchange event. I'm not an expert on this but based on what I've read so far, onchange="this.submit.form()" will refresh or reload your page therefore, everything just resets back.
I tried using your code and place it on a
<input type="submit" onsubmit="this.form.submit();" />
and it works.
So probably what you can do, is to do it using AJAX.

multiple submit buttons on the same page issue

I have two submit buttons on the same page. It doesn't seem to be working when I submit the page. Here is my code. Thanks for any suggestions.
<?php
if ($_POST['Submit_1'])
{
echo "Submit_1";
}
if ($_POST['Submit_2'])
{
echo "Submit_2";
}
?>
<form name="form1" id="form1" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<input type="submit" name="Submit_2" value="Add more" onclick=" validateForm('form1');return false;" >
<input type="submit" name="Submit_1" value="Submit" onclick=" validateForm('form1');return false;" >
</form>
return false; will stop the buttons from submitting the form.
You are using JavaScript to validate the form, then return false: the submit never propagates (unless it's part of your JavaScript we don't have). You need to submit after validation, and add a way to distinguish the button presses via JavaScript (like another parameter to your submit), or don't use JavaScript and PHP will handle it nicely for you.
Use type button instead of submit.

.submit() doesn't work with Firefox and Greasemonkey

I'm trying to make an auto login script and I'm stuck on the submit part...
The source of the submit form from the website is
<input type="submit" value="Sign In" class="signin" id="sumbitLogin">
and I'm trying
document.getElementById("sumbitLogin").submit();
if I set an Attribute, for example the value, it changes just fine...
How can I solve it?
You don't submit an input field. You submit a form.
<form id="formid">
<input type="submit" value="Sign In" class="signin" id="sumbitLogin">
</form>
and ..
document.getElementById("formid").submit();
Use form_name.submit()
<form id='myform' action='formmail.pl'>
Here is the code to submit a form when a hyperlink is clicked:
<form name="myform" action="handle-data.php">
Search: <input type='text' name='query' />
Search
</form>
<script type="text/javascript">
function submitform()
{
document.myform.submit();
}
</script>

Categories