Form submit and Ajax call - forms

I am having a problem when submitting a form.
Here is the situation
<form ....>
<input type="text" id="ajax"> <img onclick="saveTextBox">
<input type="text">
<input type="text">
<input type="text">
</form>
In the above form when the img is click on I call an Ajax script to save the button.
That's OK.
What I need is when I am focus on textbox Ajax call ajaxscript (without click img).
I add an jQuery to manage key=13 and call the script BUT the problem is that run the Ajax script AND submits the form too.
How can I override the form sumit when I am focus on Ajax text box and hit enter key?

First. Scrap the image and replace it with a real submit button.
Then. Handle all the JS interaction in the onsubmit event for the form element. Cancel the default action, so that the JS runs and the normal form submission doesn't (when JS is available).
In short: be pragmatic, build on something that works, and intercept events at the right moment. Don't ignore the primary purpose of an element and create an entirely new way to trigger something it does already.

Return false.

Sample event function that will stop the submit event:
function submitHandler() {
// do ajax stuff here
return false;
}

Related

Possibility of a Form without a Submit Button

hope You all have a great day.Here I'm trying to create a form without submit buttons,but when press enter on the text-box the whole text-box gonna hide.why is this is happening?,is it actually possible to create a form without a submit button or is that is a mandatory thing?,thanks for Your valuable time.
<form >
<input type="number" name="cbarcode" id="cbarcode" autofocus/>
</form>
When you hit 'enter' it submits the form.
I believe this is a natural part of a 'form' behavior for accessibility. Input inside of a form is meant to be data the user sends or inserts somewhere which is likely the reasoning behind this.
You can add to the Html form onsubmit="return false" or return preventDefault() as an event handler / function with other handling code you may want to provide.
Try this:
<form onsubmit="return false;">
<input type="number" name="cbarcode" id="cbarcode" autofocus/>
</form>

Form action forwards me to another page showing JSON (JavaScript)

I have a simple (mostly-working) submission form:
<form action="http://127.0.0.1:3000/books" method="POST" class="addBook">
<input type="text" placeholder="Title" name="title" required>
<input type="text" placeholder="ISBN" name="isbn" required>
<button id="submitBook" type="submit" onclick="addingBooks()">Submit</button>
</form>
My form properly adds the input values to my database and it then appears in the DOM when I go back and refresh the page.
I have applied an preventDefault() function in my JavaScript code for the 'addingBooks()' function to stop it from submitting twice, but I am still unable to stop it from forwarding to the action page after submission which only shows the JSON of the submitted data.
If you need more then please let me know.
To prevent the default action of an event you'll need an event object to call preventDefault on. Some browser do in fact have a global event object but that might not be reliable.
The simplest option would be to return false from the onlick event, not from a function you call in it
<button id="submitBook" type="submit" onclick="addingBooks();return false;">Submit</button>
another simple and better solution would be to make the button be a regular button instead of a submit button and remove the preventDefault from addingBooks.
<button id="submitBook" type="button" onclick="addingBooks()">Submit</button>
another and even better solution would be to not use the onclick attribute at all, use the addEventListener function
document.getElementById('submitBook').addEventListener("click", function(e){
e.preventDefault();
//other stuff
});

Passing value outside form

I need to get the value from outside the form. My gsp looks like this
<g:textField name="email" value="${someInstance?.email}"/>
<input type="button" id="checkEmail" class="button" value="Validate" onclick="checkEmail()"/>
<span id="responseDiv"></span>
<g:form action="save">
someCode
<g:submitButton disabled="true" style="color:#999999;" class="save" name="save" action="save" id="saveButton" value="${message(code: 'default.button.save.label', default: 'Submit')}"/>
</g:form>
First, to check if an email is in use, click button (id="checkEmail") and onclick goes to js code checkEmail() which has remoteFunction that updates responseDiv. After that, user enters information to the form, and user clicks submitButton. When that submit button is clicked, I would like my gsp to send email value along with other information to the controller. Any suggestion?
Thanks in advance.
Easy, put the input inside the form.
If that isn't an option for some reason, use the callback to update a hidden field inside the form with something like $("#hiddenEmail").val(emailAddress);
PS. I like how you gave a span an id of responseDiv :)

Stopping a barcode scanner refreshing page

I am using a barcode scanner in an HTML form and have a problem with the page refreshing
The scanner's input goes into this form (html 5):
<form>
<label for "bar_code">Item</label>
<input
name="bar_code"
id="bar_code"
onchange = "processBarCode('getitemdetails.php', this.value);"
onfocus="setStyle(this.id);"
autofocus
>
</form>
The processBarCode is a AJAX function that checks if the barcode is in a mySQL database and if it is adds the item to a <form> further down the page, this works perfectly. The problem is that because the bar code scanner adds an enter to the end of the scan when the function returns it triggers a submit thus causing the page to refresh.
I need the added enter for the onchange to work automatically but still need to disable the page refresh.
Any ideas gratefully received!!
Change your form element to cancel the submit:
<form onsubmit="return false;">

How do I keep track of when what is the text entered in the form when the user navigates away from the page?

I have a piece of code like this.
<form method="get" action="{$self}" name="addcommentform">
<textarea title="{$enterComment}" name="comment" class="commentarea" </textarea>
<input class="Button" type="submit" value="{$postComment}" />
</form>
How do I keep track of when what is the text entered in the form's textarea when the user navigates away from the page? I want to prompt the user with a warning message so he/she doesn't lose the text.
Thanks.
You could use the javascript/jquery blur event, and if the user hasn't clicked the desired button have it display the form values. This might help JQuery Blur Validation Problem
Stu
Take a look at this Javascript code snippet:
http://www.boutell.com/newfaq/creating/disableclose.html
This takes advantage of the window's onbeforeunload event which fires when the user is about to leave the page.