0

I have a strange problem.

Situation: I have a form with the method 'post'. Inside that form I have 1 checkbox and a submit button. When i press the submit button an if statement will catch wether the post var is isset.

Problem: When i press the submit button, it randomly doesn't work. So I press it now and it works or it won't.

Here is the code I use to catch the post

if ( isset ( $_POST['mail_subscribe'] )  ) {
    //My code here
}

And here is the code from the form (I'm using smarty)

<form action="/my/url.html" method="post">
    <input type="checkbox" name="mail_subscribe" class="mail_subscribe" id="{$var.graph1.ID}" {if $var.mailing == 1}checked="checked" value="0"{else}value="1"{/if} />
    <br /><br />
    <input type="submit" value="save" id="mailing_submit"/>
</form>

When i submit this form sometimes it works and more often it doesn't. If you guys need more information let me know. I'll update the question asap.

ANSWER

I found the answer to my problem. Thank you @RobertMaysJr for that.

When a checkbox is not checked, and it is the only field inside the form, the form will never submit.

Now that I know that I'm changing the way I'll be doing this :)

4

6 回答 6

2

use this...

if ( isset ( $_POST['mailing_submit'] )  ) {
    //My code here
}

And

<form action="/my/url.html" method="post">
    <input type="checkbox" name="mail_subscribe" class="mail_subscribe" id="{$var.graph1.ID}" {if $var.mailing == 1}checked="checked" value="0"{else}value="1"{/if} />
    <br /><br />
    <input type="submit" value="save" id="mailing_submit" name="mailing_submit"/>
</form>
于 2012-09-05T07:43:26.510 回答
1

it needs to always be value="1" - if the checkbox is not checked then it won't be submitted at all by the browser

于 2012-09-05T07:41:35.247 回答
0

The problem is that the key mail_subscribe will exist in $_POST only when the checkbox is checked.

So, the code will not execute all the time.

于 2012-09-05T07:41:50.983 回答
0

shouldn't your form action should have .php extension file:

<form action="/my/url.php" method="post">
    <input type="checkbox" name="mail_subscribe" class="mail_subscribe" id="{$var.graph1.ID}" {if $var.mailing == 1}checked="checked" value="0"{else}value="1"{/if} />
    <br /><br />
    <input type="submit" value="save" id="mailing_submit" name="mailing_submit"/>
</form>

And

if ( isset($_POST['mailing_submit'])  ) {
  //your code here
}
于 2012-09-05T07:45:48.953 回答
0

instead of checking checkbox value check for submit button

if ( isset ( $_POST['mail_subscribe'] )  ) {
    //My code here
}

instead of that check

if ( isset ( $_POST['mailing_submit'] )  ) {
    //My code here
}

this will ensure form submission. if checkbox is checked then only your code will work

于 2012-09-05T07:46:26.493 回答
0

use this jquery is easier

$('#yourcheckbox').click(function(){
  if($(this).is(':checked')){
       $.post('http://your form action url',{your form data },function(result){
               alert(result);
       }
  }

});

I have random post data method which replace all your form data into fake data during send read here http://www.jackart4.com/article.html

于 2012-11-28T05:10:56.890 回答