Arcyno
Arcyno

Reputation: 4603

checkbox submit on href (html)

I would like to submit a form without a submit button but when I click on another href. Here is my code :

Page 1 :

<form name="myform" method="post" action="page2.php"> 
    <input type="checkbox" name="condition" value="1">
</form>
<a href="page2.php?item=n" onclick="document.forms['myform'].submit();"> link </a>

Page 2 :

 $_SESSION['condition'] = isset($_POST['condition'])? 1 : 0;

The problem is that $_POST['condition'] is never set, even when the box is checked.. It seems that my form is not submited from page1 to page2. Do you have any clue why that could happen ?

Edit :

Maybe it wasn't clear what I was trying to achieve so I edited my code : I am using href because I have a list of ref on which the user can click but I wanted to add an option with the checkbox. So I want to send the option with the arguments of the ref when the user clicks on the ref.

Upvotes: 0

Views: 1013

Answers (3)

user2182349
user2182349

Reputation: 9782

This should work:

<form name="myform" method="post" action="page2.php?item=n"> 
    <input type="checkbox" name="condition" value="1">
    <button type="submit" class="link">link</button>
</form>

You can style the button to look like a link by removing the border and adjusting the font.

Upvotes: 0

Luki
Luki

Reputation: 214

How about something like this?

<a href="page2.php?item=n" onclick="document.forms['myform'].attributes.action.value = this.attributes.href.value; document.forms['myform'].submit(); return false;"> link </a>

Upvotes: 1

steven
steven

Reputation: 4875

the submit() method uses the specified action of the form as target, not the href of your link, you should remove that

<form name="myform" method="post" action="page2.php"> 
    <input type="checkbox" name="condition" value="1">
</form>
<a href="javascript:void(0);" onclick="document.forms['myform'].submit();"> link </a>

Upvotes: 1

Related Questions