Submit Form When a date is chosen - php

Is there a way I can submit a form using the input date type. meaning if a date is chosen, the form gets submitted without any button click?
For example,
I have this form:
<form action='ex.php' method='POST'>
Enter a date: <input type='date' name='date' />
</form>

If have any datepicker then try below code.
$('#date1').change(function(){
console.log('Submiting form');
$('#form1').submit();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action='ex.php' method='POST' id="form1">
Enter a date: <input type='date' name='date' id="date1" />
</form>

Something like this might do the trick - though some validation of the user supplied date might be called for to ensure it is a date that is added & processed.
<form action='ex.php' method='POST'>
Enter a date: <input type='date' name='date' />
</form>
<script>
var oDate=document.querySelectorAll('input[type="date"]')[0];
oDate.addEventListener('blur',function(e){
/* additional date validation here */
if( oDate.value!='' )oDate.parentNode.submit();
},false);
</script>

Related

Is it possible to have multiple actions in forms using only one submit button?

See question -
I've tried this:
echo "<form action='index.php' method='post'>
Use the previous input of participants: <input type='radio' name='participants[]'value='$participants[0]'>
OR <br>
<form action='index.html' method='post'>
Use a different input of participants: <input type='radio' name='participants[]' value='0'>
<input type='submit' value='send' name='send'> </form> <br>";
Both of the radio button lead me to index.php when I want to be able to go to index.html in case i press on the second radio button.
You may solve it by using JQuery
<form action='index.php' method='post'>
Use the previous input of participants:
<input type='radio' name='participants[]' value='$participants[0]'>
<br/>OR <br>
Use a different input of participants:
<input type='radio' name='participants[]' value='0'>
<input type='submit' value='send' name='send'>
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var form = $('form');
var input = $('input[type=radio]');
input.on('click', function() {
if ($(this).val()==0) {
form.prop('action', 'index.html');
} else {
form.prop('action', 'index.php');
}
});
});
</script>

Form submission return me to index.php

I always have this error when changing any form in my site from post to get and click on submit button redirecting me to index.php.
<form action="index.php?pg=users" method="get">
<input type='text' placeholder='user name' name='guildn' id='guild'>
<input type='submit ' name='submit ' value='Search'>
</form>
You might want to update your form a little,
For POST, doing this is fine :
<form action="index.php?pg=users" method="post">
<input type='text' placeholder='user name' name='guildn' id='guild'>
<input type='submit' name='submit' value='Search'>
</form>
But for GET, in your form action you have "index.php?pg=users" where "pg=users" is part of your URL query string already.
You can move the "pg=users" as part your form input with type hidden like the example below :
<form action="index.php" method="get">
<input type='hidden' name='pg' value='users'>
<input type='text' placeholder='user name' name='guildn' id='guild'>
<input type='submit' name='submit' value='Search'>
</form>
Remember that in method="GET", your input will be appended to your action URL on form submit and will ignore the query string in action URL.

Ajax form onsubmit stop refresh

I have one page lets call it "X.php" that i use that calls another page "Y.php" with the xmlhttp.open() function.
At the moment i have a form in page Y.php, and i would like to prevent it from refreshing because once i click on the submit button, my form dissapear.
I tryed with Jquery 'event.preventDefault()' in page X.php but nevertheless it didnt work.
Does anyone know a solution?
while($row = mysql_fetch_array($query)) {
echo " <h2>Update your personal settings</h2>
<form action='' id='target' method='POST'>
Surname: <input type=text name='lid_voornaam' value='".$row['lid_voornaam']."'><br/>
Name: <input type=text name='lid_naam' value='".$row['lid_naam']."'><br/>
Email: <input type=text name='lid_email' value='".$row['lid_email']."'><br/>
<input type='submit' value='Whatever' id='test1'/>
</form>";
}
Java script:
<script type="text/javascript">
$(document).ready(function(){
$("#test1").click(function(event){
event.preventDefault();
});
});
In your form tag. Add onSubmit="return false;
<form action='' id='target' method='POST' onSubmit="return false;>
Surname: <input type=text name='lid_voornaam' value='".$row['lid_voornaam']."'><br/>
Name: <input type=text name='lid_naam' value='".$row['lid_naam']."'><br/>
Email: <input type=text name='lid_email' value='".$row['lid_email']."'><br/>
<input type='submit' value='Whatever' id='test1'/>
</form>
As #andrew mentions. Your php script will generate multiple <form> tags which will all have the same id id='target'. This can cause problems on your page as that isnt valid html
I would have thought that the javascript you posted should work actually.
But if this is your actual code I suspect it fails because all of the submit buttons on all forms have id='test1' and id should be unique
Try like this:
$i=0;
while($row = mysql_fetch_array($query)) {
echo " <h2>Update your personal settings</h2>
<form action='' id='target."$i++".' method='POST'>
Surname: <input type=text name='lid_voornaam' value='".$row['lid_voornaam']."'><br/>
Name: <input type=text name='lid_naam' value='".$row['lid_naam']."'><br/>
Email: <input type=text name='lid_email' value='".$row['lid_email']."'><br/>
<input type='submit' value='Whatever' id='test1'/>
</form>";
}
and
$("form").submit(function(event){
var id = $(this).attr('id').split('target')[1];
alert('form ' +id+ ' clicked');
event.preventDefault();
});

PHP submit Multiple Forms with 1 button and 1 action

I want to submit Multiple Forms with 1 button and 1 action target using PHP. Is is possible ?
HTML
<form name="myform" action="test_post.php" method="post">
Name: <input type='text' name='name' />
</form>
<form name="myform2" action="test_post.php" method="post">
Class: <input type='text' name='class' />
</form>
Search
JS
function submitform()
{
document.myform.submit();
document.myform2.submit();
}
PHP (test_post.php)
echo $name = $_POST['name'];
echo $class = $_POST['class'];
I tried with that code but it just show $_POST['class'] value. For name it show error : Undefined index: name in...
Please advice.
you dont need one form per input, you can have a million in one one form so ..
<form name="myform" action="test_post.php" method="post">
Name: <input type='text' name='name' />
Class: <input type='text' name='class' />
</form>
Search
should be fine, and you don't really need js to submit. a html submit input is better supported
<input id="submit" type="submit" value="submit">
You actually want two fields on one form - which you can then submit with no problems:
<form name="myform" action="test_post.php" method="post">
Name: <input type='text' name='name' />
Class: <input type='text' name='class' />
<input type='submit'>
</form>
Or you can submit it by using some JS anyhow if you do other thigns in the JS code.
if jquery is an option, then .deferred might be what you are looking for.
function submitform(){
//define a variable where we will store deferred objects
var def = true;
$("form").each(function() {
var postResult = $.Deferred();
//.when takes a deferred object as a param.
// if we don't pass a deferred object .when treats it as resolved.
// as our initial value of def=true, the first .when starts immediately
$.when(def).then(function(){
$.post('post_destination.php', form.serialize()).done(function(){
//the chain will fail after the first failed post request
//if you want all the requests to complete in any case change the above .done to .always
post.resolve();
});
});
// now we reassign def with the deferred object for the next post request
def = postResult;
});
}
and here's the link to when I asked the question some time ago. How to submit multiple jquery posts to a page one after another if database updates successfully?

Pass One Input Value Into Input Type Hidden Value When User Submits

I have a simple form as outlined in the code below. I would like to append the value submitted in rm_accounts text box to the hidden page_confirm input value at the end of the URL. Hopefully that makes sense.
Essentially, if the user enters '123456' in the rm_accounts textbox, I want value of page_confirm to be http://www.mywebsite.com/index.php?rm_accounts=123456
<form name="signup" method="post" action="https://go.reachmail.net/libraries/form_wizard/process_subscribe.asp" >
<input type='text' name='rm_accounts' value='' />
<input type="hidden" name="page_confirm" value="http://www.mywebsite.com/index.php?rm_accounts=">
<input type="submit" name="Submit" value="Submit" >
</form>
All help is very much appreciated. Thanks!
Use Jquery focusout event to update hidden field value
When user enters 12345 and focus out of textbox or clicks submit(or anywhere) the below code get executed and update the value of hidden field.
$('input[type=text]').focusout(function(){
$('input[type=hidden]').val("http://www.mywebsite.com/index.php?rm_accounts="+$('input[type=text]').val());
console.log($('input[type=hidden]').val());
});
or in submit button click
$('input[type=submit]').click(function(){
$('input[type=hidden]').val("http://www.mywebsite.com/index.php?rm_accounts="+$('input[type=text]').val());
console.log($('input[type=hidden]').val());
});
Working JSFiddle link
http://jsfiddle.net/mkamithkumar/qNdny/1/
I would first suggest you give your HTML some IDs like so:
<form id="signup" method="post" action="https://go.reachmail.net/libraries/form_wizard/process_subscribe.asp" >
<input type='text' name='rm_accounts' id='rm_accounts' value='' />
<input type="hidden" name="page_confirm" id="page_confirm" value="http://www.mywebsite.com/index.php?rm_accounts=">
<input type="submit" name="Submit" value="Submit" >
</form>
Then use some jQuery for your task:
$('#signup').submit(function(){
var value = $('#rm_accounts').val();
var page_confirm = 'http://www.mywebsite.com/index.php?rm_accounts='+value;
$('#page_confirm').val(page_confirm);
});

Categories