Reputation:
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 :)
Upvotes: 0
Views: 222
Reputation: 171
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
Upvotes: 0
Reputation: 4631
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
Upvotes: 0
Reputation:
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
}
Upvotes: 0
Reputation: 4858
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>
Upvotes: 1
Reputation: 11413
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.
Upvotes: 0
Reputation: 1063
it needs to always be value="1" - if the checkbox is not checked then it won't be submitted at all by the browser
Upvotes: 0