aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Can you post data from textfields to a URL without having a <form> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Can you post data from textfields to a URL without having a <form>" Watch "Can you post data from textfields to a URL without having a <form>" New topic
Author

Can you post data from textfields to a URL without having a <form>

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

I have been trying to submit some data I have in a registration page. On my page, I am not using a <form> tag, and I would think with JQuery/JavaScript I could submit the text entered in the text field to a URL I have set up as a REST api for a create/POST.

Here is my page, which shows up in a fancybox.



Here is the JavaScript I have been trying to get it to work, but when I have a break point on my serverside code, the String with data is null.



I can change the id=Registration div to be a form instead, but I wanted to see if I could not need one.

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Well if JavaScript is turned off, your site will not work.

But you should be using

http://api.jquery.com/serialize/

to get the form fields and values for the data.

Eric
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Eric Pascarello wrote:Well if JavaScript is turned off, your site will not work.

But you should be using

http://api.jquery.com/serialize/

to get the form fields and values for the data.

Eric


Obviously, JavaScript is not turned off on my laptop.

To call serialize, would require me having a form tag.

Which I don't and would love it if I wasn't required to have a form tag.

I mean I am "submitting" textfields based on an <a> link. That should be enough, I would think. I am asking if I can do this without a <form>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Curious: what's your beef with the form tag? That seems like a really really odd attitude.

In any case, you can grab the value of the text field and "submit" it as a query string if it fits. Or you could make an Ajax call. Or you could create a form on the fly and submit it.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Bear Bibeault wrote:Curious: what's your beef with the form tag? That seems like a really really odd attitude.

In any case, you can grab the value of the text field and "submit" it as a query string if it fits. Or you could make an Ajax call. Or you could create a form on the fly and submit it.



Well, I plan on making ajax calls in this app, which is going to be a single page app. No refreshing or reloading an entire page.

I don't really have a beef with the form tag itself. As much as I have always had a beef with html. I really hate that we create this visual thing called web pages/sites and define it with a text document with xml (html). But I can accept some markup for just the content, nothing visual. I always want my xml to be the least verbose as possible because the more you have in a document the more you have to maintain, and xml is sucky at maintainability. So I also feel that a <form> tag is too verbose for the purposes of having a link to a URL. In my case here a link to the POST method in my Controller class that gets called via ajax.

I actually added a form tag, just because I couldn't figure out a clean way to make the textfield data into one json String to pass to my serverside with JQuery.

But, I wanted to do it with the least amount of html as possible, make it really really clean. I didn't want a <button> I wanted an link, I feel I have more control over it in css to make it look however I wanted.

Thanks

Mark
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

I must admit that I think you are going to be continually frustrated. HTML is what it is, and trying to get around it with tricks is just going to make things harder for you, not easier, in my opinion.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Bear Bibeault wrote:I must admit that I think you are going to be continually frustrated. HTML is what it is, and trying to get around it with tricks is just going to make things harder for you, not easier, in my opinion.


Well, I am actually trying it to be completely no tricks. I thought the <form> tag and having to cancel out the submit in order to do it in javascript was a trick and I wanted to avoid doing tricks. I want simple plain html, the simplest you could find. Since this project is completely my own and one that I am using to learn Web UI and its best practices and making it the easiest to maintain is my main goal with this project. It has to be simple to work on many different devices including PhoneGap mobile native apps. I think if I create pages that aren't simple, there will be some coupling somewhere that makes my task much harder.

Always thankful for your help Bear.

Mark
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

...and its best practices...


Using the FORM element to submit form data is a best practice. Preventing the default page refresh to do it via ajax is not a "trick". And using something like jQuery's form plugin makes it really easy to do. And I agree with Bear. If you can get to a place where you can embrace what HTML is you'll be a lot happier and have less of a struggle.

The other option is to create whatever will replace HTML and be famous.


GenRocket - Experts at Building Test Data
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Gregg Bolinger wrote:
...and its best practices...


The other option is to create whatever will replace HTML and be famous.


Trust me, I have thought long and hard about it, and I have ideas. I have been on it since 1997. And while HTML is still here, HTML 5 and where things are going with iCloud and some other things out there, it is getting closer to my vision I had back then. It will be really cool, and I feel that Apple is closer to it than anybody, followed closely by Google.

Image a computer with no Hard Drive, no Disk Drives of any kind, no USB ports, no sim cards, no power cord needed (battery doesn't run out), no Heavy Operating System (basically the browser is the OS) Just a monitor, keyboard, track or mouse, with a basic Motherboard (internet built in), oh and a camera. But you will still be able to do everything that you do today on a regular device same speed maybe faster (I can't even say computer anymore here). All at a price point below $300. Now you can say dumb terminal, network computer, or other labels they have used for something similar, but they really weren't similar, the difference though is very subtle.

Subtle is something Apple does really well. Look at the iPad as an example, the difference is subtle, but people were laughing when it was introduced as being just a larger iPod.

Anyway, I just hijacked my own thread and meant to post another question.

Mark
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

So, this is partly in the FAQ, but not completely.

I now have .js files In each file, I try to place all the common functions, variables in one file, then break all the others into a more organized structure, where if one page is the only page that needs a particular function, then I make a .js file just for that page. But for functions that are used in more than one page, but not all pages, I put that in a .js file. At least that is what I see as a well organized structure in my mind (I am always wrong). So if I have some vars in the common that are used in more fine grained js files, can I just use them in the fine grained js file because I would have the more generic common js file loaded first on those pages???

Then on the jsp pages how do I minimize the …
<script type="text/javascript" src="${pageContext.request.contextPath}/js/fancybox/source/jquery.fancybox.pack.js?v=2.0.4"></script>

lines in my page.

I do plan on using YUICompressor when I go live.

should I have .jsp files that only have the <script> tags that I then include in my full jsp pages?

The one thing I really want to avoid is duplication, too many js files unorganized that I can't find things, and slow page loads.

Thanks guys.

I love being the newbie and getting the expert advice here.

Mark
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Most sites have a common css and js file that has the basic code in it. Than they have a js/css for page specific stuff.

What I have is all of my JavaScript and CSS in separated out and a MAKE file that runs it through the google closure compiler and I merge those files together into one. This makes it easier to develop and quicker to serve up.

Your goal should be to limit the http calls that have to be made and make sure you have the correct caching set on the server so the common files are not requested on every page load.

Eric
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can you post data from textfields to a URL without having a <form>