SO for some reason my form is submitting two times with a single button press. This is my first time using jquery Form plugin, and I imagine that jquery is submitting once and the form is "naturally" submitting as well. I have seen that the remedy is to attach a "return false" to the onSubmit event handler of the form. I thought I am doing that, but obviously it is not working.
Can anyone help?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>User form entry </title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="jquery.validate.js"></script>
<script src="jquery.passwordStrength.js"></script>
<script src="jquery.form.js"></script>
<script>
$(document).ready(function() {
$('#signupForm').ajaxForm(function() {
var queryString = $('#signupForm').formSerialize();
$.post('process.php', queryString);
});
});
</script>
</head>
<body>
<form id="signupForm" action="process.php" onsubmit="return false" method="post">
<fieldset class="password">
... form goes here
<button type="submit" name="formSubmit" value="submit">Click to submit</button>
</form>
<div id="results"></div>
</body>
</html>
I have tried adding onsubmit="return false" to the form element, but then I have no submission at all. I have also tried adding "return false;" to the jQuery, but then I still have double submissions. What am I missing? This seems to be the standard methodology according to the jQuery Form Plugin site.. how is my form different?
(By the way, just to be clear.. I am not talking about the problem of having multiple consecutive form submits by pressing the button repeatedly. My problem is "one submit button push = two submits".)
$('#signupForm').ajaxForm(function() {
var queryString = $('#signupForm').formSerialize();
$.post('process.php', queryString);
});
This is submitting it twice .... the ajaxForm method once and then the post() the second time
The ajaxForm method handles the form sumbission for you ... you dont need to add the post() method ... the function inside of ajaxForm is a callback, executed on success ...
$('#signupForm').ajaxForm(function() {
alert("Thank you for your comment!");
});
this code would show the alert after the successful post ... simple example here -> http://jquery.malsup.com/form/
You should also remove the onsubmit attribute from the form ...
Update
If you want to show results ... do it like this :
$(document).ready(function() {
$('#signupForm').ajaxForm({ target: '#results' });
// this will output the responseText from the submitted form to the target DOM element
});
avoid using
<button type="submit" name="formSubmit" value="submit">
so naturally your submit button will do one submit and your jquery will do another submit
use this
<input type="button" name="formSubmit" value="submit">
Try using this:
onsubmit="void(0);"
Don't use a submit button. Use a regular button.
Related
My if(isset) validation is returning false after I have submitted the form through jQuery ,however works fine when done without jquery. Reason I am using jQuery is because I need to submit multiple forms:
Button
<input class="btn btn-primary" type ="submit" id="myButton"
name="create_record" value="Submit 1">
jQuery:
<script>
$(document).ready(function () {
$("#myButton").click(function (event) {
event.preventDefault();
$("#form1").submit();
// $("#form2").submit();
});
});
</script>
PHP
<?php
if(isset($_POST['create_record'])){
$ecode = $_POST['ecode'];
$ename = $_POST['ename'];
$date = $_POST['date'];
$jobRole = $_POST['jobRole'];
}else{
echo "did not receive anything";
}
?>
Always getting "did not receive anything" . Can someone please help.
The submit button value only gets sent if the form is submitted in the traditional way by a button click. Since you are submitting the form via javascript, you'll need to explicitly include the submit button's value or validate your post data in some other way. If you need the value of the specific button that was clicked, something like this should work:
$("#myButton").click(function (event) {
event.preventDefault();
var el = '<input type="hidden" name="' + $(this).prop('name') + '" value="' + $(this).val() + '">';
$("#form1").append(el).submit();
});
As for your objective of submitting multiple forms at once, I believe it's impossible without using ajax as discussed here. If you need guidance on how to do that, better to open a new question.
Your code, isset($_POST['create_record']) maybe false or it didn't receive any values. If your query is only in one PHP file together with your jQuery, you need to check first your algorithm or use var_dump() for testing. Second, If it didn't work, make an alternative solution for it. Do the proper HTML code when using form or make another PHP file for receiving post purpose only.
<form action="directory_to_another_file" method="POST">
<!-- SOME INPUTS HERE -->
<input type="submit" value="Submit 1" name="create_record">
</form>
Try to test all of your codes.
You have to set form method as "POST" type and if you want to receive the form data in same page then empty the "action" key otherwise give the target link.
<?php
if(isset($_POST['create_record'])){
print_r($_POST);
}
?>
<form action="" method="POST" id="form1">
<input type="text" name="create_record" value="Submit 1"/>
</form>
Submit
<script>
$(function(){
$("#myButton").click(function (event) {
event.preventDefault();
$("#form1").submit();
});
})
</script>
Let me know if it's work for you.
I am trying to pass form values through post form but nothing shows.
using this HTML5 Text Editor http://suyati.github.io/line-control/
I'm using this method for form and php code
<?php
if(isset($_POST['submit'])){
echo $_POST['txtEditor'];
}
?>
html form i am using
<form method="post">
<textarea name="txtEditor" id="txtEditor" ></textarea>
<input name="submit" type="submit" value="Submit">
</form>
and the java script is
<script src="WYSIWYG-Text-Editor/editor.js"></script>
<script>
$(document).ready(function() {
$("#txtEditor").Editor();
});
</script>
Have checked the doc and apparently you need to set the value by yourself, here is a solution:
Add some code to your initialization code:
<script>
$(document).ready(function() {
$("#txtEditor").Editor();
$('form').submit(function () {
$('#txtEditor').val($('#txtEditor').Editor('getText'));
});
$('#txtEditor').Editor('setText', $('#txtEditor').val());
});
</script>
On form submit we actually set the textarea value with what the user input in the WYSISWYG.
In the next line of code we set the value of the WYSISWYG with what value comes in the textarea (as you requested in your comment).
I wrote an array to pass variables in the GET to a search page. The search page only has 4 fields but I'm only passing the most important variables, first name and last. Here is the array:
<?php echo "<td><a href='" . matry::base_to('test/trace', array('first'=>$patient->first , 'last' =>$patient->last)) . "'><ul class='controls'>
<li id='check_orders'><`span class='symbols'>L</span><span class='label'>Skip Trace</span></li>
</ul></a></td>";?>
When the page loads i'm just echoing the _GET to pre populate the first and last input fields on that page..
What I'm looking for is a script that will execute the search with the first and last name fields populated as that page loads automatically. Additionally, when the search is executed it's populating in an iframe. (forgot about that part)~!
I tried using:
<script>document.getElementById('stack').submit();</script>
<form action='http://xxxx.yyyyyyy.com/stuffhere' name='es' target="my_iframe" id="stack">
with no avail.
Your <script> is running before the <form> exists.
Move the <script> below the <form>.
You are calling the submit function before the form is even loaded on the page.
Place the script tag after the closing form tag or call submit on document ready or window onload.
<form id-"stack">
... form fields...
</form>
<script>document.getElementById('stack').submit();</script>
or
<script>$(function(){$('#stack').submit();})</script>
Please imagine this simple example:
<html>
<head>
<script type="text/javascript">
function body_onload() {
var form = document.getElementById('theform');
form.submit();
}
</script>
</head>
<body onload="body_onload()">
<form id="theform" action="action.php">
<input type="hidden" name="query" id="query" value="foo" />
</form>
</body>
</html>
It will submit the form after the page has loaded.
But are you really searching for a non AJAX solution?
Try this, been using this on a redirect page for a while. This of course needs to be below the from so it is run after the browser process the form.
<script type="text/javascript">
function send(o)
{
var f = document.getElementById("theForm");
f.submit();
}
send();
</script>
If you want to submit the form when the page loads you should change your code to this:
<script>window.onload = function(){ document.getElementById('stack').submit(); }</script>
However this will redirect the user (as if they have clicked a form submit button). To avoid this you will need to use AJAX (I recommend using jQuery to do this). See example below:
$(document).ready(function(e) {
var form_data = $("#form_id").serialize();
var form_url = $("#form_id").attr("action");
var form_method = $("#form_id").attr("method").toUpperCase();
$.ajax({
url: form_url,
type: form_method,
data: form_data,
cache: false
});
});
See this page for more info on using AJAX
I have the following form and javascript function on my web page. This is a dummy function that I am using to test whether what I would like to do is possible.
What I am attempting to do is have a form send an AJAX request to the server, so that the server can update the database while the page itself continues along it's predetermined path. I am in a tight time crunch, so I unfortunately do not have time to rewrite the entire page to better support this. The problem that I have is the xmlhttp object does not seem to return properly. I get a readyState of 4 but a status of 0. can someone please explain what I need to do?
Here's my code:
ajax.php
<html>
<head>
<script type="text/javascript">
function test(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
document.getElementById("new").innerHTML+="<b>"+xmlhttp.readyState+"</b> "+xmlhttp.status;
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("hello").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","response.php",true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php
if ($_POST){
echo $_POST['hello'];
}
?>
<form action="ajax.php" method="post">
<input type="text" name="hello" />
<input type="submit" value="submit" onclick="test();" />
</form>
<div id="hello"></div>
<h3>debug</h3>
<div id="new"></div>
</body>
</html>
response.php
<?php
echo "Hello there";
?>
EDIT
Please note that I do not want to prevent the default behavior. In fact, the forn must be submitted as usual. I simply want to add an AJAX request to the process.
You can't trigger an AJAX request and allow the form to submit at the same time. Incomplete AJAX requests are cancelled when the page reloads (as is the case when the form is submitted). You'll either have to not submit the form at all, or wait until your AJAX call has completed before submitting the form. If you wanted to go the second route, you could make the following changes to your code:
<html>
<head>
<script type="text/javascript">
function test(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
document.getElementById("new").innerHTML+="<b>"+xmlhttp.readyState+"</b> "+xmlhttp.status;
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("hello").innerHTML=xmlhttp.responseText;
**document.getElementById("ajaxform").submit();**
}
}
xmlhttp.open("GET","response.php",true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php
if ($_POST){
echo $_POST['hello'];
}
?>
<form action="ajax.php" method="post" **id="ajaxform"**>
<input type="text" name="hello" />
<input type="submit" value="submit" onclick="test();**return false;**" />
</form>
<div id="hello"></div>
<h3>debug</h3>
<div id="new"></div>
</body>
</html>
Changes/additions are marked with **.
Note that there are a few practices in there I don't like, in particular using the onsubmit, etc attributes of HTML tags to attach Javascript event handlers.
I know this doesn't directly answer your question but if you are strapped for time then I would suggest just using jQuery to handle the AJax.
You can attach it to a button press and then call some code: http://api.jquery.com/jQuery.ajax/
I can dig out some code examples if you need them.
Once the submit button is pressed the browser will submit data to the server and a new page will be loaded. You can bind the submit event of the form and in the event make the ajax call but you will have 2 problems.
The browser may redirect before all ajax data is sent
You have no idea if the ajax call was successful or not
I would try sending the data you are sending via ajax in the same form data using hidden inputs. If both calls are aimed at different urls then try this:
var ajaxSent = false;
$('#myForm').submit(function(e){
if (!ajaxSent){
e.preventDefault();
$.ajax({
url: "ajaxUrl",
// data, method, etc
success: function(){
ajaxSent = true;
$('#myForm').trigger('submit');
}
}
// else submit the form
});
I have an HTML form that currently just posts the data directly to a PHP file. I want to update the code so that the submit button sends the data to a JavaScript function so that I can create an AJAX function. Is it possible for the submit button to activate a JavaScript function rather than posting to a php file? The only thing I have come up with is below, which quite obviously does not work:
<html>
<head>
<script type="text/javascript">
function ajax(){
//...
}
</script>
</head>
<body>
<form action="ajax();">
<!-- ... -->
<input type="submit" value="Submit" />
</form>
</body>
</html>
You can give the "submit" input a "click" handler that explicitly prevents the default behavior from being carried out.
<input type='submit' value='Submit' onclick='ajax(event)'>
Then in the function:
function ajax(event) {
if ('preventDefault' in event) event.preventDefault();
event.returnValue = false; // for IE before IE9
// ...
}
edit #Esailija points out correctly that another option is to handle the "submit" event on the <form> element instead. The function would look pretty much the same, in fact exactly the same, but you'd wire it up like this:
<form id='yourForm' onsubmit='ajax(event)'>
That will also trap things like the "Enter" key action, etc.
Of course you can. But it's more useful to call your Javascript function in the input like this :
<input type="submit" value="Submit" onclick="ajax();" />
And remove the action part in the form.
I jQuery you can use event.preventDefault(); otherwise just use return false;
http://jsfiddle.net/mKQmR/
http://jsfiddle.net/mKQmR/1/
Pointy is correct... just add a click handler to the submit button, however make sure the last line of the click handler returns "false" to prevent the form from actually being posted to the form's action.
<html>
<head>
<script type="text/javascript">
function ajax(){
//...
return false;
}
</script>
</head>
<body>
<form action="thispage.htm">
<!-- ... -->
<input type="submit" value="Submit" onclick="ajax();" />
</form>
</body>
</html>