Handling a form purely from Javascript - html

I have the following form in a website of mine.
<form action="" method="GET">
<input type="text" name="query" value="" />
<input type="button" name="talk" value="talk" onClick="askCow(this.form)" />
</form>
Clicking the button does fire an AJAX request that updates the page. Using return does not, however; it simply submits the form using GET, which is not the desired behavior. Using onsumit="return false;" or similar disables submission, but this is not the desired behavior.
How can I execute the askCow function on an enter press ("form submission") while making sure the form is NOT submitted? Am I a bad person for doing this? The reason I use AJAX is that it lets me omit templating logic on the server side entirely.
Thanks!
Edit: here is the complete page
<html>
<head><title>Psycowlogist</title></head>
<body>
<div id="history">
Previous discussion
</div>
<div id="cow">
<pre>
_______________________
< What is your problem? >
-----------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
</pre>
</div>
<div id="talk">
<form id="talkForm" action="" method="GET" onsubmit="askCow(this)">
<input type="text" name="query" value="" />
<input type="button" name="talk" value="talk" onClick="askCow(this.form)" />
</form>
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js" type="text/javascript" charset="utf-8"></script>
<script type="text/coffeescript">
$j = jQuery
window.askCow = (form) ->
$j.get "/cowanswer",
{q: form.query.value},
(data) -> $j("#cow pre").html(data["cow-answer"])
return false
</script>
</html>

Just use onsubmit event of form
<form action="" method="GET" onsubmit="return askCow(this);">
<input type="text" name="query" value="" />
<input type="button" name="talk" value="talk"/>
</form>
askCow function should look something like this
function askCow()
{
//do ajax stuff
return false;
}

You can use jQuery Form Plugin .
And within the call, you can call askCow() .
var options = {
target: '#output', // target element(s) to be updated with server response
beforeSubmit: showRequest, // pre-submit callback
success: showResponse // post-submit callback
};
$('#theForm').submit(function() {
askCow(this);
$(this).ajaxSubmit(options);
return false;
});

Related

How to redirect user to another html page *ON LOCAL STORAGE* after clicking Submit button

<html>
<head>
<script>
function myFunction() {
location.replace("https://www.w3schools.com")
}
</script>
</head>
<body>
<form action="/action_page.php">
<input type="text" name="username" pattern="[a-zA_Z].{7,}" title="Unsuccessful Login, Username must contain at least one integer and one letter, and at least 7 or more characters">
<input type="button" name="B1" value="Submit" onclick="myFunction()">
</form>
</body>
Hi, for some reason when I click the submit button, the error does not appear and it just goes directly to the page. How do I make the program check the validation and when it is ok, redirect to a new page??
I would suggest you to go by this approach
create a test.php file with the following code :
<form method="post">
Username: <input type="username" name="username" pattern="(?=.*\d)(?=.*[a-z]).{7,}" title="Must contain at least one integer and one letter, and at least 7 or more characters">
<button type="submit" value="submit" class="btn btn-primary">Submit</button>
</form>
<?php
if (isset($_POST['submit'])) { ?>
<script language="javascript" type="text/javascript">
window.location = 'your location';
</script>
<?php
}
?>
This will give you the desired functionality.
NOTE: for this to run, you will need to install xampp or wamp if you are running windows and lamp if you are running linux.
So here's the answer according to your updated code :
<html>
<head>
<script>
function myFunction() {
window.location.href = 'https://www.w3schools.com';
return false;
}
</script>
</head>
<body>
<form onsubmit="return myFunction()">
<input type="text" name="username" pattern="^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+){5,}" title="Login Unsuccessful. Username must contain at least one integer and one character, and should be atleast 5 or more characters long" required>
<input type="submit" >
</form>
</body>
</html>
The return false; statement in myFunction is very important here. It will return a false to the obsubmit event which will prevent the form from being submitted. If you remove return false; statement then also window.location will work but it will appear as if it's not working as the form will get submitted and you will see the same page rather than a redirection to another page.
I have added the onsubmit event which will call the function only when all the inputs are valid

How would I post an html form but, still redirect to a thank you page after the form has posted? [duplicate]

This is my form code:
<form enctype="multipart/form-data" name="upload-file" method="post" action="http://example.com/upload">
<div class="formi">
<input id="text-link-input" type="text" name="url" class="link_form" value="your link" onfocus="if(this.value==this.defaultValue) this.value='';" onblur="if(this.value=='') this.value=this.defaultValue;" />
<input type="submit" value="OK" class="link_but" />
</div>
<div class="upl" title="Upload"><img src="http://example.com/example.png" alt="" style="vertical-align:middle;"/>Upload
<input type="file" name="file" size="1" class="up" onchange = "document.getElementById('text-link-input').value = String(this.value).replace('C:\\fakepath\\','')"/>
</div>
</form>
Now, I want to redirect the submitter to any page of my choice after the form data has been submitted, but the action resides on another website where I cannot edit. Is it possible to redirect the user to any page after he has submitted the data to that site?
From some Googling, this is what I have found. Adding this to form code:
onSubmit=window.location='http://google.com'
This didnt work. Maybe I didnt implement it correctly? This is what I did:
<form enctype="multipart/form-data" name="upload-file" method="post" onSubmit=window.location='http://google.com' action="http://example.com/upload">
Another person says adding a hidden field should work:
<input type="hidden" name="redirect" value="http://your.host/to/file.html">
How should I implement this is my code?
Suggestions and help awaited...
Try this Javascript (jquery) code. Its an ajax request to an external URL. Use the callback function to fire any code:
<script type="text/javascript">
$(function() {
$('form').submit(function(){
$.post('http://example.com/upload', function() {
window.location = 'http://google.com';
});
return false;
});
});
</script>

one textbox one button two different pages in html

i am trying to assign a textbox value to a php variable now problem is i want one button to work for two different pages i.e if i enter in a text box 'a'and click on button it should redirect to 'a.php' page if i write in a text box 'b' it should redirect to 'b.php'.
so one textbox,one button two different pages.
Code :
<html><head>
<meta charset="UTF-8" />
<script>
function submitForm(action)
{
document.getElementById('a').action = action;
document.getElementById('a').submit();
}
function submitForm1(action)
{
document.getElementById('b').action = action;
document.getElementById('b').submit();
}
</script>
</head>
<body >
<h3><font face="verdana" size="3"><b>Enter Text:</b></h3>
<input type="text" align="right" style="font-size:15pt;height:32px;"><br><br>
<form action="b.php" name="b" id="b" method="post">
<form action="a.php" name="a" id="a" method="post">
<input type="submit" onclick="submitForm('a.php')" value="TRY" name="a">
<input type="button" onclick="submitForm1('b.php')" value="TRY" name="b">
</form>
</form>
</body>
</html>
You can change the action onsubmit based on the text inside the input.
<html>
<head>
<script type="text/javascript">
function onsubmit_handler(){
var myForm = document.getElementById('myForm');
var data = document.getElementById('data').value;
if(data == "a")
myForm.setAttribute('action', 'a.php');
else if(data == "b")
myForm.setAttribute('action', 'b.php');
else
myForm.setAttribute('action', 'error.php');
}
</script>
</head>
<body>
<h3>Enter Text:</h3>
<form id="myForm" method="post" onsubmit="onsubmit_handler()">
<input type="text" id="data" name="data" value="">
<input type="submit" value="Post">
</form>
</body>
</html>
Test code here : http://jsfiddle.net/Eg9S4/
use this codes buddy
<html><head>
<meta charset="UTF-8" />
<script>
function submitForm()
{
var link=document.getElementById('a');
var str1 = "a.php";
var n = str1.localeCompare(link);
if(n==1)
{
window.open("main.html");
}
else if(n==-1)
{
window.open("index.html");
}
}
</script>
</head>
<body >
<h3><font face="verdana" size="3"><b>Enter Text:</b></h3>
<input type="text" align="right" style="font-size:15pt;height:32px;"><br><br>
<input type="submit" onclick="submitForm()" value="TRY" name="a">
</form>
</form>
</body>
</html>
The problem looks to be that you're using getElementById but the input elements don't have an ID (they only have a name).
I'd also recommend attaching an onSubmit event to the form and removing the onClick events from the buttons.
Edit: After looking at the code in detail I saw that there were some other issues that were probably hindering the opersation. The most notable one was that you can't nest form tags. There were some other CSS and validation issues.
Here is some working code:
Test Page
function SubmitForm(el) {
// Get the form element so that we can set the action later
var form = document.getElementById('theForm');
// Set the action for the form based on which button was clicked
if (el.id == "A")
form.action = "a.php";
if (el.id == "B")
form.action = "b.php";
// You dont need to submit the form. It's alreasdy happening, so there is no need for an explicit call.
}
</script>
<h3 style="font-family: Verdana; font-weight: bold;">Enter Text:</h3>
<input type="text" style="font-size:15pt;height:32px;"><br><br>
<form method="post" onsubmit="SubmitForm();" action="fake.html" id="theForm">
<input type="submit" id="A" value="A" onclick="SubmitForm(this);">
<input type="submit" id="B" value="B" onclick="SubmitForm(this);">
</form>

HTML Form Redirect After Submit

This is my form code:
<form enctype="multipart/form-data" name="upload-file" method="post" action="http://example.com/upload">
<div class="formi">
<input id="text-link-input" type="text" name="url" class="link_form" value="your link" onfocus="if(this.value==this.defaultValue) this.value='';" onblur="if(this.value=='') this.value=this.defaultValue;" />
<input type="submit" value="OK" class="link_but" />
</div>
<div class="upl" title="Upload"><img src="http://example.com/example.png" alt="" style="vertical-align:middle;"/>Upload
<input type="file" name="file" size="1" class="up" onchange = "document.getElementById('text-link-input').value = String(this.value).replace('C:\\fakepath\\','')"/>
</div>
</form>
Now, I want to redirect the submitter to any page of my choice after the form data has been submitted, but the action resides on another website where I cannot edit. Is it possible to redirect the user to any page after he has submitted the data to that site?
From some Googling, this is what I have found. Adding this to form code:
onSubmit=window.location='http://google.com'
This didnt work. Maybe I didnt implement it correctly? This is what I did:
<form enctype="multipart/form-data" name="upload-file" method="post" onSubmit=window.location='http://google.com' action="http://example.com/upload">
Another person says adding a hidden field should work:
<input type="hidden" name="redirect" value="http://your.host/to/file.html">
How should I implement this is my code?
Suggestions and help awaited...
Try this Javascript (jquery) code. Its an ajax request to an external URL. Use the callback function to fire any code:
<script type="text/javascript">
$(function() {
$('form').submit(function(){
$.post('http://example.com/upload', function() {
window.location = 'http://google.com';
});
return false;
});
});
</script>

Problem in form submit through javascript

I am submitting form via javascript by using 'document.FormName.submit()' . But this is giving me error of 'submit is not a function'. I m using IE8
<script typr="text/javascript">
function submitForm()
{
document.theForm.submit()
}
</script>
<body>
<form name="theForm" method="post">
<input type="text" name= "name">
<input type="button" name="submit" value="submit" onclick="submitForm()">
</form>
</body>
Please help me ?
problem is with your button name
i have use this its work fine
<script type='text/javascript'>
function submitForm()
{
document.theForm.submit();
}
</script>
<form name="theForm" method="post" action="default.php">
<input type="text" name="t" id="t"/><br/>
<input type="button" name="t1" value="submit" onClick="submitForm();">
</form>
use
document.forms[index].submit()
instead
Try this:
document.getElementsByName("theForm")[0].submit();
And you need to put the script after you declared the elements so :
<form name="theForm" onSubmit= "submitForm()">
<input type="text" name= "name">
<input type="submit" name="submit" value="submit">
</form>
<script type="text/javascript">
function submitForm()
{
document.getElementsByName("theForm")[0].submit();
}
</script>
Or you can use events like document onload if you want to kepp your scripts in the page head.
If your form name is FormName
try this
Action
by the way your code is missing a semicolon at the end of the statement
and a spelling mistake here
<script typr="text/javascript">
typr
Here is the problem
change it typr is nothing should be type
<script typr="text/javascript">
to
<script language="javascript">
OR
<script type="text/javascript">
I just copy your code and executed in IE8 and it worked fine, so how is not working on your IE8. May be it is because of your hierarchy. So you please give id to form and try document.getElementById to access your form and then submit method. Do some thing like this"
<script type='text/javascript'>
function submitForm()
{
document.getElementById('your_form').submit();
}
</script>
<form name="theForm" id="your_form" method="post" action="default.php">
<input type="text" name="t" id="t"/>
<input type="button" name="t1" value="submit" onClick="submitForm();">
</form>
document.getElementById("theForm").submit();
It works perfect in my case.
you can use it in function also like,
function submitForm()
{
document.getElementById("theForm").submit();
}
Set "theForm" as your form ID. It's done.