php


PHP MAIL CHECKBOX $POST


I'm running a form through an AJAX post call, but the value on my checkbox remains the same, regardless if I check or uncheck it when the mail script is processed.
I want the value to change when unchecked. So unchecking the checkbox changes the value to "no", etc.
Here's my code, what am I doing wrong?
<input type="checkbox" name="newsletter" id="newsletter" checked="checked" value="1"/>
Ajax Call is here:
http://pastebin.com/gFGbWZ1D
Php Mail Script here:
http://pastebin.com/d6hCVmzQ
Cheers,
Mike
The value of the checkbox will always be 1 regardless of whether or not it is checked when viewing the value of it using jQuery.
What you need to do is see if the checkbox is checked:
var newsletter = $("#newsletter").is(":checked")?1:0;
$('#newsletter').click(function(){
if($(this).val() == 1) {
$(this).val('no');
// I'd recommend doing it this way for consistency sake
$(this).val(0);
}
});
You have to check the value of the checkbox in your PHP Mail script, and handle it slightly differently.
My approach would be to do this:
if(isset($_POST['newsletter'])&&$_POST['newsletter']==1) $newsletter="yes"; else $newsletter="no";
You are using the PHP to determine the value in this case, since a checkbox can only have one value! If it's checked, the value will be that of the html value, but in my example we are only using the value of the HTML field in our if statement:
if (isset($_POST['newsletter'])) //checks to see if this element exists on form submit, and
$_POST['newsletter']==1 //checks to see if the checkbox is checked. If you echo back the value of $_POST['newsletter'] it will always be one, so you need to adjust the logic of your mail script so that you are using the style of "if checked, then yes; else no" type of thing.

Related Links

How to add form data to a bootstrap modal in the same page by using jQuery?
WP - How to get all the comment_post_ID in bulk comment approval
How to grab a random MYSQL Column Value using PHP and echo it?
Secure way to allow a specific PHP script to read/write a directory?
php searching empty fields in the db [duplicate]
PHP strtotime returns incorrect month [duplicate]
Listing out team members with select options
Restart foreach loop in PHP
Can't map request body json into injected response object
Can't access local php website (Linux)
php code not work, need some change [duplicate]
Connect to multiple PHP code through one Java async task
24 hour format for the actual time?
how can i install bcompiler library for php on windows?
How to link to other php file in wordpress theme folder?
Codeigniter Insert Item into array

Categories

HOME
cocoapods
firebase
verification
clearcase
redmine
postgresql-9.4
activex
braintree
biztalk
apk
semantic-web
mongoid
swift2
azure-data-lake
repair
joomla3.2
phpseclib
scheduler
log4j2
colors
feature-extraction
xbap
phonegap-build
google-cloud-logging
l20n
word2vec
infrared
pe
sharedpreferences
npoi
laravel-4.2
credit-card
progid
caesar-cipher
mongoengine
rails-postgresql
pdfminer
jade4j
plane
grails3.2.0
vungle-ads
section508
cloudwatch
webvtt
asp.net-web-api-routing
msxml
ltrace
http4s
jpda
django-testing
mathml
jenkins-workflow
between
declarative
perforce-integrate
yii2-model
ajaxmin
gravatar
lovefield
helm
featuretoggle
css-paged-media
ngcordova
textscan
ampersand
flow-js
ipod
dbmigrate
baseadapter
pass-by-value
xcode6.3.1
full-text-indexing
django-sites
sensormanager
illuminate-container
progress-db
google-code-prettify
ember-table
contextswitchdeadlock
clrprofiler
arrow
monocross
dropshadow
memory-pool
microblogging
differentiation
data-dump
firefly-mv
tablet-pc
grails-validation
ognl
tessellation
urchin
ixmldomdocument
accumulator
server-load
rendering-engine

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App