Jump to content

Submit a form using JS


emvy03

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/247071-submit-a-form-using-js/
Share on other sites

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.