Jump to content

Recommended Posts

Hey freaks!

 

I have tried to make a javascript function that should submit the form its triggered from, which means, if the function is called from form thats named updateform, it should submit that form when pressing on a link.

 

ATM. i have nothing, i just deleted it all because of bugs.

 

I hope you can help me!

Link to comment
https://forums.phpfreaks.com/topic/241338-submit-form-with-javascript/
Share on other sites

I have tried to make a javascript function that should submit the form its triggered from

 

You're not making much sense. You want to .. trigger the submit event of a form, when it's submitted?

 

if the function is called from form thats named updateform, it should submit that form when pressing on a link

 

Where does the link come into it? Which link(s) should trigger the submit?

 

You need to be more clear.

I'm sorry, here's a snippet of my code.

 

<form id="updateform" name="updateform" action="lipsum.php" method="POST">
        <table border="0">
            <tbody>
                <tr>
                    <td><label>Navn:</label></td>
                    <td><input type="text" name="updatename" value="" /></td>
                </tr>
                <tr>
                    <td>E-mail:</td>
                    <td><input type="text" name="updateemail" value="" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <a href="" class="button">
                            <span class="send">Tilmeld opdateringsmail</span>
                        </a>
                    </td>
                </tr>
            </tbody>
        </table>
    </form>

 

When pressing on "Tilmeld opdateringsmail", it should redirect to lipsum.php as it says in the form tag?

<head>
<script type="text/javascript">

function submitForm(){

   document.updateform.submit();
}

</script>
</head>
<body>
<form id="updateform" name="updateform" action="lipsum.php" method="POST">
        <table border="0">
            <tbody>
                <tr>
                    <td><label>Navn:</label></td>
                    <td><input type="text" name="updatename" value="" /></td>
                </tr>
                <tr>
                    <td>E-mail:</td>
                    <td><input type="text" name="updateemail" value="" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <a href="" class="button">
                            <span class="send" onclick="submitForm()" onsubmit="this.disabled=true;return true">Tilmeld opdateringsmail</span>
                        </a>
                    </td>
                </tr>
            </tbody>
        </table>
    </form>

which route are you going to go, the html method or the javascript method?

 

When I read that I thought you were meaning the purely JavaScript method. Yours is in-fact still mixed HTML and JavaScript.

yeah good catch and you're right...lol

I guess I really meant the strictly HTML method or using javascript as well...although I could have used purely javascript...

OP if you would prefer the purely javascript way let us know....good catch MrAdam thanks

Thanks for the function, but i have one question?

 

function submitForm(form){

   document.form.submit();
}

 

Is that possible? Because, then i can use it at every form i need it.

And also, how should it be in the link?

<a href="javascript:submitForm(updateform);">Send</a>

or

<a href="javascript:submitForm('updateform');">Send</a>

 

Or should it even be onClick?

You would need to access the form through the forms array:

 

document.forms[form].submit();

 

Otherwise JS will try to look for a form named "form". I wouldn't advise using a link to submit the form though, as users with JS disabled won't be able to use it and you'll have no fall back. More and more common these days, with mobile devices and users trying to combat pop-ups and such. What's wrong with the fully supported submit button? You can even style it to look and behave like a link if you prefer:

 

<style type="text/css">
input.link-submit {
    background: transparent;
    border: 0;
    color: blue;
    cursor: pointer;
    display: inline;
    text-decoration: none;
}

input.link-submit:hover {
    text-decoration: underline;
}
</style>

 

<input type="submit" class="link-submit" value="Send" />

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.