Auto Submit After Finished Fill Text Box - php

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();
});

Related

how to get value of two normal button in a single form?

I have a form in which i have two input button one for multiple delete and another for multiple suspend items. like this-
<form>
<input type="checkbox" name="check_item[]" value="<?php echo $users['user_id']; ?>" />
<input type="button" id="suspendall" name="suspendall" value="Suspend" />
<input type="button" id="deleteall" name="deleteall" value="Delete" /></td>
</form>
when I click on delete or suspend it ask for confirm that event by jquery like-
$('#deleteall').click(function() {
if(confirm('Really Want To Delete This?')){
$('#listing').submit();
}
});
if it confirm cancel form is not submitted and if it confirm OK form is submitted and on submission i have to delete or suspend that item from db. I have write this code for this-
if(isset($_POST['deleteall'])){
$check_array = $_POST['check_item'];
$usersId = implode($check_array,',');
$db->deleteUser($usersId);
}
if(!empty($_POST['suspend'])){
$check_array = $_POST['check_item'];
$usersId = implode($check_array,',');
$db->suspendUser($usersId);
}
the problem I am facing is both times when the form is submited i got only array of ids of check boxes. I am not able to identify which button is clicked because I am not getting button value. that why its not working, and if I changed these button into submit button its working very nice but didn't ask for confirm the event. What should I do for that. Do anyone have any solution for that, Please help me. thanks
When submitting with jQuery().submit() the button on which was clicked is lost.
You could try to not submit() the form in the click handler but instead call evt.preventDefault() when ! confirmed().
In you HTML form has a hidden form field.
On the JS event set the value of the hidden field before submitting
$('#deleteall').click(function() {
if(confirm('Really Want To Delete This?'))
{
$("#hiddenFormId").val("deleteAll");
$('#listing').submit();
}
});
Then use this type of code in your PHP (Sorry not a PHP programmer)
if $_POST['hiddenFormId'] == 'deleteAll'

Check if form is filled before submitting

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()'>

Javascript submit-function with button won't work

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.

onclick disable submit button

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.

How can I submit a web form by clicking keyboard button?

<form action="page.php" method="post">
<input type="text" name="some_text" />
<input type="submit" name="some_submit" /
</form>
I want to submit this form by pressing defined keyboard button. I wonder how to do it. If it was just an <a> element it would be simple - I would just change window.location value after handling keypress event. But there is some data to send and I have no idea how to solve this.
You can create an event handler for a key press event, then submit the form using its submit() method to pass all the form data to the recipient page "page.php".
Using jQuery, this is trivial:
<form id="myForm" action="page.php" method="post">
<input type="text" name="some_text" />
<input type="submit" name="some_submit" />
</form>
<script type="text/javascript">
$('#myForm").keyDown(function(e){
if(e.which == 13) // When key pressed is "Enter" key.
$('#myForm').submit();
});
</script>
Read Javascript Madness: Keyboard Events for more information on interpreting keyboard events.
The data will be passed automatically as a result of form.submit()
Give a name to the form
<form name="myform" action="page.php" method="post">
After handling key press event do
document.myform.submit()
which will basically submit the form. If you want to add more parameters to it add those as hidden elements inside form.
For an example of key press handling, see http://dev.kanngard.net/Permalinks/ID_20050426091851.html - it submits a form if key 13 (Enter) is pressed, but can be modified to any other key.
In general: register a function to be called on KeyDown event, inside the function, check which key was pressed; if needed, submit() your form.
I'm in trouble:
function action(button) {
if ($('a.'+button+':visible').length > 0) {
if ($('a.'+button+':visible').attr('class') == button || $('a.'+button+':visible').attr('class') == 'rotate '+button) {
var adr = $('a.'+button+':visible').attr('href');
window.location = adr;
}
if ($('a.'+button+':visible').attr('class') != button) {
$('a.'+button+':visible').click();
}
}
if ($('input.'+button+':visible').length > 0) {
$('#form').submit();
}
}
Where variable 'button' is button's classname and #form is form's id. By clicking submit it works normally, but it doesn't work by keyboard event.

Categories