I'm trying to use the submit-function in javascript to submit a form. The reason I'm not using a submit button is that I want to ask the user if really wants to submit the form. Here is the code:
<?php
if(isset($_POST['submit1']))
die("Submit successfull!");
?>
<!--Javascript submit question.-->
<script type="text/javascript">
function question_submit() {
if(confirm('Are you sure you want to submit?')) {
return true;
}
else {
alert('You have not submitted this form');
return false;
}
}
</script>
<!--The form-->
<form action='test.php' method='POST'>
<input type='button' name='submit1' value='Submit' onClick="if(question_submit()) { this.form.submit(); }" />
</form>
Result: The popups works great, it just never wants to submit the form and so "Submit successfull!" never shows up.
The message is not even shown when I add a real submit button to the page. It's like the POST-data won't work.
But when I add a PHP-code and die-function för the submit-button the thing will work and "REal Submit Works" will show up, which is strange because the buttons code is first.
<?php
if(isset($submit1))
die("Submit successfull!");
if(isset($_POST['realsubmitbutton']))
die("REal Submit Works");
?>
I I've also tried to change the onClick to just contain the javascript submit function. Like this: onClick="this.form.submit();". But that won't work either. So my question is, what's wrong with the code and why won't it work?
Can't buttons send it's value with POST?
The issue is that you are submitting the form via your onclick handler. That means, the button isn't pressed as part of the submit.
You can return true from that function to get the behavior you want.
Really though, this is a bad idea. Many of us prefer to simply push enter on a form field, rather than clicking submit. On your form handler, just check !empty($_POST) instead. Put your "are you sure" function in as the onsubmit action of the form.
Use directly "onSubmit" event on form instead of button click.
Then return "false" if you don't actually want to submit.
Modify your function so that it gets a reference to the form and calls it's submit() method:
function question_submit() {
if(confirm('Are you sure you want to submit?')) {
document.getElementById("myForm").submit();
return true;
} else {
alert('You have not submitted this form');
return false;
}
}
Also, update the form HTML so the form has an id, and modify your HTML button so that it calls the question_submit() function. Leave the actual business logic to the function to keep the HTML clean. Finally, make sure your attributes are all lowercase. This shouldn't affect functionality, but it is cleaner to use lowercase attribute names:
<form action='test.php' method='POST' id='myForm'>
...
<input type='button' name='submit1' value='Submit' onclick="question_submit();" />
</form>
The returning true or false works only when the input is type submit. So, you should change
<input type='button' name='submit1' value='Submit' onClick="if(question_submit()) { this.form.submit(); }" />
for
<input type='submit' name='submit1' value='Submit' onClick="if(question_submit()) { this.form.submit(); }" />
With your function, it will only submit when the user accepts. Or maybe, you could use
<form action='test.php' method='POST' onClick="return question_submit()">
<input type='submit' name='submit1' value='Submit' />
</form>
I've read all your answers and have finished the code. It now works with both clicking on the button or pressing Enter key. The javascript runs correctly either way and the form gets submitted.
This is the code:
<?php
if(!empty($_POST['justaname']))
die("Submit successfull!");
?>
<!--Javascript submit question.-->
<script type="text/javascript">
function question_submit() {
if(confirm('Are you sure you want to submit?')) {
document.getElementById("myform").submit();
}
else {
alert('You have not submitted this form');
}
}
</script>
<!--The form-->
<form action='test.php' method='POST' id="myform" onSubmit="event.preventDefault(); question_submit();">
<input type="text" name='justaname' />
<input type='button' name='submit1' value='Submit' onClick="question_submit()" />
</form>
Thanks for all the answers and hope this is helpfull for someone.
Related
As the title says This is the code that I tried with. The forms must appear one by one because information from previous forms determine how the next ones will look.
$(document).ready(function(){
$('#first_form').submit(function(){
$('#first_form').fadeOut('fast');
$('#second_form').fadeIn('fast');
});
});
<form action="new_patch.php" method="POST" id="first_form">
Title: <input type="text" name="patch" placeholder="Patch 4.20">
<br/>
Number of Champions: <input type="number" name="champ_number" min="1" max="99">
<br/>
<input type="submit" value="submit">
</form>
<form action="new_patch.php" method="POST" id="second_form" style="display: none;" >
<input type="text" value="text">
<input type="submit" value="submit">
<?php
$champ_number = null;
if(isset($_POST['champ_number']))
{
$champ_number = $_POST['champ_number'];
for($champ_number;$champ_number>0;$champ_number--)
{
echo "<br/>Champion ".$champ_number."<input type=\"number\" name=".$champ_number." min=\"1\" max=\"99\">";
}
}
?>
</form>
You're mixing client-side and server-side form code. Submitting the form will reload the page entirely, so from the looks of your code it will fade in the new form when the old form is submitted, but then reload the page so the old form will show again anyway.
You could either:
Let the PHP determine how the next form appears based on the submission of the first form, e.g. if (isset($_POST["First_form_submit"]) { Show second form... }
Probably better and more user-friendly: make the second form appear below once the user has filled in the relevant inputs on the first form before they've submitted
you can use:
$('#first_form').submit(function(){
$('#first_form').fadeOut(function() {
$('#second_form').fadeIn('fast');
});
return false;
});
From the jQuery documentation the syntax is fadeIn( [duration ] [, complete ] ) it accepts a duration and a onComplete callback that you can use to execute the next action when the first is completed.
I did this once too, just add a submit class to the button and make it like this:
<input type="submit" value="submit" class="submit">
Change script to a click function.
$(document).ready(function(event){
event.preventDefault();
$('.submit').click(function(){
$('#first_form').fadeOut(400);
$('#second_form').fadeIn(400);
});
});
PS, also you need to prevent submit default...otherwise it will just submit the form, see this JSfiddle
i have a problem,
i can't auto submit after fill the text box. i want to submit without the submit button.
check my code below
<?php
echo"
<form action='save.php' method='POST'>
<center>
Enter ID<br>
<input type='text' name='id'>
</center>
</form>
";
?>
Trigger the form's submit() event when the field loses focus. You can detect this by attaching a blur() event handler to it.
$("#field").blur(function() {
$("#form").submit();
});
If you field doesn't have an ID or other means of easily identifying it (which I would recommend) you could also do something like this:(only used when you have one field)
$("#form :input").blur(function() {
$("#form").submit();
});
I'm trying to do two different javascript actions with jquery for my php form which has two submit buttons: 'save' and 'next'. The idea is that both button submits form that saves data into db, but while 'next' goes through client-side validation and progress further, the 'save' just skips validation, returns true and the user stays on the form.
<form id="form" name="form" method="post" action="?action=my_php_form">
<input type="submit" name="save" class="save" id="save" value="save"/>
<input type="submit" name="next" id="next" class="next" value="next"/>
</form>
I already managed to succeed when user clicks either 'save' or 'next' after reload, but if user clicks 'next', launches validation and submit returns false, he cant click 'save' and ignore validation anymore. What might be the cause of this?
$(function() {
//Lets skip the whole thing if save is clicked
$('#save').click(function() {
$('#form').submit(function() {
return true;
});
});
$('#next').click(function() {
$('#form').submit(function() {
var invalid = 0;
//A lot of crazy validation, if some invalid stuff then increment increment invalid
if(invalid > 0) {
return false;
}
else {
return true;
}
});
});
});
I think I would do something like this :
$(function() {
//Lets skip the whole thing if save is clicked
//Actually, no binding is needed as the button is already a submit button
//Thx to Ocanal
$('#next').click(function() {
var invalid = 0;
//Validation process
if(invalid) {
$('#form').submit();
} else {
return false;
}
});
});
Why would you skip validation, especially if it so crazy ? Anyway the problem is here
$('#form').submit(function() { ... }
this doesn't overwrite the submit event handler, this ADDS a function to it. Therefore if you first click next then save , the function you defined for next will still be triggered when clicking on save.
While it's not clear with the notation, it's quite logic : that's what allows you to "add" action to your documentReady event from wherever you wish, not only from a central place.
You can use a different type for your non-submit button.
You'll want something like this:
<form id="form" name="form" method="post" action="?action=my_php_form">
<input type="submit" name="save" class="save" id="save" value="save"/>
<input type="button" name="next" id="next" class="next" value="next"/>
</form>
I think what's happening to you now is that both buttons are acting as your "submit" button, so the form is trying to submit, regardless of which button you're clicking, or what functions you've added to the EventListener.
First of all, Java Script is created every time when refreshing the page. It's important to understand that.
Now, if you click on the button 'Save' of type submit, your information will pack up in the form packet and sent to the server. The submit action requires reloading of the page(!).
Therefore, if you want to keep values in fields (if that's what you want) you may use PHP.
Using PHP is not complicated. I combine the code inside the <body> tag and before the <form> tag.
The code checks whether there is a value in the 'Save' field of $_POST variable, if true, we will save the received values.
And then, I present the values using variable access <? = $name ?>. That's it.
<?php
$name = "";
$credit = "";
if(isset($_POST['save'])) {
$name = $_POST['name'];
$credit = $_POST['creditCard'];
}
?>
<form id="form" name="form" method="post" action="good.php">
<input type="text" name="name" value="<?=$name ?>" />
<input type="text" name="creditCard" value="<?=$credit ?>"/>
<input type="submit" name="save" class="save" id="save" value="save"/>
<input type="submit" name="next" id="next" class="next" value="next"/>
</form>
I have a form called choose_dates.php that submits to a file called process.php. The form consists of a textbox, a dropdown list and a submit button. I have set it up so that you can submit either one value, or the other, or both at the same time. I would like to have it such that if the user has put a value in the textbox AND the dropdown list, then a prompt will ask if that is what he/she really wants to do. The code below doesn't seem to do that when the submit button is pressed. The rest of my code (that I have not placed on here) works fine, this is more of a user interface issue.
<form name="dates" action="process.php" method="POST">
<input type="text" name="submitDate">
<select name="removeException">
<option value="some-value">display dropdown stuff</option>
.
.
</select>
<input type="submit" value="submit"
<?php
if($_POST['submitDate'] != "" and $_POST['removeException'] != "")
{
echo " onclick=\"return confirm('Are you sure you want to submit both values at the same time?')\" ";
}
?>
tabindex="2">
</form>
And of course, please ask any questions if what I said isn't clear enough. Regards.
Add onsumbit="return checks();" in form tag.
checks is a Javascript function that verify everything is good, if not, return false and the form will not be submited. If true, the form will be submited normally. just move your onclick to onsumbit in form.
You need to do that on the client side using javascript ( preferably ). The post data will be submitted when the form is submitted. Try adding this function as your form's onsubmit event
function func(){
var a = document.getElementsByName('removeException'),
b = document.getElementsByName('submitDate');
if(a[0].value!=null && b[0].value!=null){
var c = confirm('Are you sure you want to submit both values at the same time?')
if(c){
return true;
}else{
return false;
}
}
}
Then
<form name="dates" action="process.php" method="POST" onSubmit='return func()'>
i wanna disable a submit button when onclick. im able to disable the button but i cant submit the post value to php.
<input type="submit" onclick="this.disabled = true" value="Save"/>
or ref this
If you disable an input, then its value naturally won't be included in the form data. You'll need to disable the button after you submit. If you bind a callback to onclick, then it runs before the form submits.
What you need is something like this:
jQuery:
$(document).ready(function() {
$(document).unload(function() {
$('#submit-btn').attr('disabled', 'disabled');
});
});
Regular JS:
document.onunload = disableSubmit;
function disableSubmit() {
/* disable the submit button here */
}
Basically, instead of binding to the submit button's onclick event, this binds the disabling code to the document's unload event (document.onunload), which gets fired once the form is submitted and you begin to leave the page.
I solved it with simple jQuery. The code removes the button on click, then appends the fake button or some like "loading.." text and finally sends the form.
HTML:
<div class="holder"><input type='submit' value='ACCEPT' class='button'></div>
jQuery:
$('.button').click(function() {
$('.button').remove();
$('.holder').append("//fake input button or whatever you want.");
$('.form').submit();
});
In diference with other methods like unload the button changes in the instant moment you click and sends the form. With heavy forms i think is a better practice.
Using jQuery, add onClick handler that returns false:
<input type="submit" value="Submit" onClick="$(this).click(function() {return false;});"/>
i found a alternative online. wat i did is to create a fake disable and hidden button. when the actual button is clicked, i will hide it and show the fake disable button.
actual button:
$onclick = "
var boolconfirm = confirm('$strconfirmattempt');
if(boolconfirm==true){
finishattempt.style.display='none';
finishattempt2.style.display='inline';
}
return boolconfirm;";
fake button:
echo "<input type=\"submit\" name=\"finishattempt\" value=\"submit\" onclick=\"$onclick\" />.
<input type=\"submit\" id=\"finishattempt2\" name=\"finishattempt2\" value=\"submit\" style=\"display:none;\" DISABLED/>\n";
You could use a hidden field which would hold the value of the button and pull that value out of your POST data:
<input type="hidden" id="hiddenField" value="default" />
<input type="button" id="myButton" onclick="buttonClick();">
function buttonClick()
{
document.myForm.myButton.disabled = true;
document.myForm.hiddenField.value = "myButtonClicked";
}
My PHP is a little rusty, but then you can access the hidden field like so:
if ($POST['hiddenField'] == "myButtonClicked")
{
// Click handling code here
}
Why not create a disabled submit button that is hidden, and an active submit button, and onClick show the disabled and hide the active? I could do this in jQuery, but I'm kinda useless without it. Sad, eh?
Here's a method using onsubmit instead of onlick:
This goes at the top:
<script type='text/javascript'>
function disableButtons()
{
$('input[type="submit"]').attr('disabled', true);
}
</script>
Then your PHP (note that isset post is NOT for the submit button, because we want to disable the submit button).
if (isset($_POST['dothis']))
{
//CODE TO EXECUTE
}
Then HTML.
<form method='post' action='' onsubmit='disableButtons()'>
<input type='hidden' name='dothis' value=''>
<input type='submit' value='Submit'></form>
Onsubmit goes in .
Make sure your isset (the PHP part) is for an input that goes with your submit, but is not the submit button itself. You can see that it is the hidden value being checked for with the PHP, rather than the submit button, and the submit button is what gets disabled.
By doing this, you can disable a submit button without disabling the PHP.