emvy03 Posted September 13, 2011 Share Posted September 13, 2011 Hi, I'm having trouble submitting a form using javascript. I have 2 javascript submit buttons (One uploads a story to the site and the other if for a search function) on my site, and another submit button assigned to 'story' that a user uploads. The problem I am having is with this last submit button. I'm using the same script as with the other two buttons and the submit function works in that the page reloads, nothing happens with regards submitting any code. I'm confident that the form works because there is no issue when using a standard issue submit button. The form code: <form name='voteForm' id='voteForm' action='vote2.php' method='POST'> <a href='javascript: submitvote()' id='submit'><img id='green' src='images/greenbtn.png' alt='' ></a> <input type='hidden' id='pid1' name='pid1' value='$id' > <input type='hidden' id='page' name='page' value='$page' > <input type='hidden' id='title' name='title' value='$rumourID'> </form>" The Javascript: <script type="text/javascript"> function submitvote() { if(document.searchForm.onsubmit && !document.searchForm.onsubmit()) { return; } document.searchForm.submit(); } </script> Thanks for reading guys! Quote Link to comment https://forums.phpfreaks.com/topic/247071-submit-a-form-using-js/ Share on other sites More sharing options...
requinix Posted September 13, 2011 Share Posted September 13, 2011 Your form is called voteForm, not searchForm. And you don't need to trigger the onsubmit event by yourself. Quote Link to comment https://forums.phpfreaks.com/topic/247071-submit-a-form-using-js/#findComment-1268881 Share on other sites More sharing options...
emvy03 Posted September 14, 2011 Author Share Posted September 14, 2011 Hi, Sorry, that was a mistake when I copied and pasted the code; the correct code does have 'voteForm instead of 'searchForm', but I am still having the same problem. Quote Link to comment https://forums.phpfreaks.com/topic/247071-submit-a-form-using-js/#findComment-1269173 Share on other sites More sharing options...
AyKay47 Posted September 14, 2011 Share Posted September 14, 2011 your if conditional makes no sense to me, you aren't returning anything.. and can we see your updated code please.. also have you tried using firefox frebug to see if you can locate the errors? Quote Link to comment https://forums.phpfreaks.com/topic/247071-submit-a-form-using-js/#findComment-1269178 Share on other sites More sharing options...
emvy03 Posted September 14, 2011 Author Share Posted September 14, 2011 function submitvote() { if(document.voteForm.onsubmit && !document.voteForm.onsubmit()) { return; } document.voteForm.submit(); } </script> I'm using the exact same code for an upload form as well and that works with out any problems. Quote Link to comment https://forums.phpfreaks.com/topic/247071-submit-a-form-using-js/#findComment-1269192 Share on other sites More sharing options...
AyKay47 Posted September 14, 2011 Share Posted September 14, 2011 also have you tried using firefox frebug to see if you can locate the errors? Quote Link to comment https://forums.phpfreaks.com/topic/247071-submit-a-form-using-js/#findComment-1269205 Share on other sites More sharing options...
emvy03 Posted September 14, 2011 Author Share Posted September 14, 2011 No, I haven't tried firefox. I think the issue maybe to do with the fact that the button doesn't have a 'name' so the php script is triggered but has nothing to work from. The problem I now have is what to give a 'name'. Quote Link to comment https://forums.phpfreaks.com/topic/247071-submit-a-form-using-js/#findComment-1269212 Share on other sites More sharing options...
AyKay47 Posted September 14, 2011 Share Posted September 14, 2011 use firebug when trying to debug javascript code.. what do you mean by "the bottom doesn't have a name"? Quote Link to comment https://forums.phpfreaks.com/topic/247071-submit-a-form-using-js/#findComment-1269216 Share on other sites More sharing options...
Adam Posted September 14, 2011 Share Posted September 14, 2011 A named submit button's value won't be submitted unless you actually click it. What does the PHP code look like? If "vote2.php" only handles one form, then just check if any POST data was submitted to detect the submission: if (!empty($_POST)) { If it handles multiple forms, check if one of the required fields is not empty like above. Quote Link to comment https://forums.phpfreaks.com/topic/247071-submit-a-form-using-js/#findComment-1269221 Share on other sites More sharing options...
emvy03 Posted September 18, 2011 Author Share Posted September 18, 2011 Hi, Sorry I haven't replied replied in a while. I've managed to find a work around so thank you for all your help. Quote Link to comment https://forums.phpfreaks.com/topic/247071-submit-a-form-using-js/#findComment-1270390 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.