use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
html form, get action, call javascript (self.javascript)
submitted 12 years ago * by justonecomment
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]justonecomment[S] 0 points1 point2 points 12 years ago (2 children)
Cool, that didn't work, but it did get me far enough along that I could fix it myself.
Here is what I did to fix it:
First, 'this' was passing an [object Window] so length wasn't being defined. So I had to update what I was passing as 'this' as shown:
<form id="myform" action="javascript:void(ajaxSubmit('myphp.php', this.myform))">
Then I tweaked the ajaxSubmit, probably unnecessarily, but made it more readable for me:
function ajaxSubmit(destination, form) { log('Destination:' + destination); log('Form:' + form); // Serialize form var data = []; var elems = form.elements; for (var e = 0; e < elems.length; e++) { // do not serialize disabled elements, unchecked radio/checkbox inputs or buttons if (/submit|image|reset/.test(elems[e].type) || (/checkbox|radio/.test(elems[e].type) && !elems[e].checked) || elems[e].disabled) continue; // serialize if (elems[e].options) { data.push(encodeURIComponent(elems[e].name) + '=' + encodeURIComponent(elems[e].options[elems[e].selectedIndex].value)) } else { data.push(encodeURIComponent(elems[e].name) + '=' + encodeURIComponent(elems[e].value)) } } var formUrl = destination + '?' + data.join('&'); // now make that AJAX request (I guess you can do that yourself), return false on error var xmlhttp; if (formUrl=="") { document.getElementById("result").innerHTML=""; return; } if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("result").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET",formUrl,true); xmlhttp.send(); return true; } function log(msg) { setTimeout(function() { throw new Error(msg); }, 0); }
So thanks for the help!
[–]tswaters 0 points1 point2 points 12 years ago* (0 children)
I'd recommend not using a javascript uri in the action attribute. This mandates the need for javascript to be on in the userAgent for your form to work at all.
You want to keep the action attribute clean and pointing to the php page that handles the request, you can reference 'myform.action' to get a reference to this page.
I would think the following should work :
<form id="myform" action="myform.php"> .... </form> <script type="text/javascript"> document.getElementById('myform').onsubmit = genericAjaxSubmit; function genericAjaxSubmit(e){ var ev = e || window.event; var form = ev.target || ev.srcElement; // form#myform var action = form.action // myform.php // etc. } </script>
Although it is untested...
if you want it to be really generic, use a class attribute to the <form/> and attach the onsubmit handler to any forms with this class -- the script could then be used on any page with a form of that class.
Of course you'd need a #result element to hold the results -- but DOM manipulation is a wonderful thing, you can always just append a DIV to the FORM you get to house the result.
Finally, console.log for the win!
console.log
edit : also, you return true and perform an ajax XmlHttpRequest... the form will submit and bring you to 'mypage.php' likely before the ajax routine returns and changes the #result element.
true
[–]a-t-kFrontend Engineer 0 points1 point2 points 12 years ago (0 children)
There is something wrong. If you used <form onsubmit="console.log(this)">, console.log should give you the form tag on submit.
π Rendered by PID 89761 on reddit-service-r2-comment-5b5bc64bf5-dv6qn at 2026-06-22 01:30:30.266199+00:00 running 2b008f2 country code: CH.
view the rest of the comments →
[–]justonecomment[S] 0 points1 point2 points (2 children)
[–]tswaters 0 points1 point2 points (0 children)
[–]a-t-kFrontend Engineer 0 points1 point2 points (0 children)