HTML Button troubles

Programming — Alex @ 2:17 pm

I discovered a painful lesson today. I’m trying to put together the bookmark management system I mentioned earlier, using Ajax, just because I haven’t used PHP in months or dabbled in serious Javascript in years, and I’ve never used Ajax. Got to keep those skill sets up! Also I’m avoiding studying.

So, I got the MySQL database set up, the tables working, and wrote up the PHP backend, all of which went fairly smooth. Then I start delving into the Ajax side of things and everything starts going bonkers– figuring out how to do cross platform XML parsing in plain Javascript seems to be beyond me, so I get Sarissa (which is so lightweight I haven’t noticed it’s there, much less worried about its API), and that problem’s solved. Next, I run into the weirdest problem: all my POSTs are disappearing! It’s like any requests sent via POST are lost, and when I document the POST handling code in the PHP backend with error_log, nothing shows up in the errorlog. So, I spent *hours* trying to figure out what the problem is, and where it is: is it the Ajax, or the backend…

Turns out it’s neither. I’d noticed earlier that since I left the action attribute on the form empty, the interface page reloads itself everytime you click on the submission button– an annoying fact, but one I couldn’t figure out how to remedy, and which didn’t seem important. After all else failed, I paid more attention to this, and it seems that whenever the page reloads, it chops off the ongoing Ajax transaction and submits a GET query. This makes no sense to me, because the page is HTML, so it should be calling itself on reload, and the only place where the cgi is called is in Javascript code, so it shouldn’t know where to submit data. If any of that makes sense to you, please explain it to me.

But the solution is simple: specify the button type to be button and the page won’t reload itself when you click the button. Turns out that the default type of a button is submit (I was leaving it blank). It seems more logical to me to make the button do nothing by default.

Possibly relevant posts:

0 Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2008 ChapterZero | powered by WordPress with Barecity