0

I made an HTML form to add a friend on my website and I want to be able to submit it with a link I made. I have tried to do onClick="form.submit();" but that didn't work.

PIC OF WEBSITE

    </head>
<body>
    <div class="Forum-Block">
        <div class="Top-Bar">
            <a href="http://site/Forum.php"><div class="Back-Box">Back</div></a>
            <a href="http://site/ShowFriends.php"><div class="FriendsB-Box">Friends</div></a>
            <a href="http://site/ShowSentRequests.php"><div class="RequestsB-Box">Sent Requests</div></a>
            <a href="http://site/AddFriend.php"><div class="SendRequestB-Box">Send Request</div></a>
        </div>
        <div class="FriendRequestSend-Box">
            <form method="post">
                <input type="text" name="friendname"/>
                <br/>
                <a href="http://site/AddFriendPHP.php" onclick="form.submit();"><div class="FriendRequestSend-Button">Send</div></a>
            </form>
        </div>
    </div>
</body>
RyanS
  • 627
  • 1
  • 10
  • 26
Marcus Mardis
  • 83
  • 1
  • 1
  • 8
  • Why didn't it work, what error do you get, have you tried anything else. http://stackoverflow.com/help/how-to-ask – Mathemats Nov 19 '15 at 23:31

3 Answers3

1

The problem is that form isn't defined as a javascript variable anywhere. You can give your <form> tag a name like "myFormName", then get it from the DOM using document.myFormName. Once you have the DOM element for the form, then you can call submit() on it.

I should also mention that it's generally considered bad practice to put javascript inline in tags. You can do it, but it's not ideal. Instead, you should add it in a <script> tag or separate .js file.

<head>
    <script>
        document.getElementById('mySubmitLink').onclick = function() {
            document.getElementById('myForm').submit();
        }
    </script>
</head>
<body>
...
<form id="myForm" method="post">
    <input type="text" name="friendname"/>
    <br/>
    <a href="http://site/AddFriendPHP.php" id="mySubmitLink"><div class="FriendRequestSend-Button">Send</div></a>
</form>
Mar
  • 7,765
  • 9
  • 48
  • 82
0

See: How to submit a form using javascript?

So basically give you form a name like this:

<form method="post" name="theForm">...</form>

and do something like this for the javascript:

document.theFormName.submit();

(So the link would be something like:

<a href="http://site/AddFriendPHP.php" onclick="document.theFormName.submit();"><div class="FriendRequestSend-Button">Send</div></a>
Community
  • 1
  • 1
Nathan
  • 321
  • 3
  • 7
-1

I would suggest using

<input type="submit" value="submit" />
prola
  • 2,793
  • 1
  • 16
  • 15