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

FOSOAuthServerBundle, Authorization header doesn't work but access_token in query works
Combine 2 differect foreach [duplicate]
Why Nginx logs the error: upstream sent invalid status “0 0” while reading response header from upstream?
Intervention package make method over http not https?
Cannot POST /index.php
Codeigniter passing data from controller to view using the indicies as variables doesn't seem to be working
Get has_one relation in SilverStripe
how to upload file from app using cloudinary
PHP database connection inside if statement
CakePHP3 extending AppController namespace to third party vendor class [duplicate]
Class 'lluminate\Redis\RedisServiceProvider' not found in laravel 5.2
PHP how to use str_replace in a foreach loop
Remove consecutive values in two dimensional array php
Printing on firefox using absolute positioned HTML elements not printing
How to get php-resque in laravel5.2
Same table model association twice in cakephp 3.2

Categories

HOME
android-fragments
softlayer
ios-simulator
bing-search
google-sheets-query
dji-sdk
drag-and-drop
google-search-console
cdi
onvif
reportviewer
elastic-load-balancer
business-intelligence
orc
grocery-crud
pywinauto
robolectric
codefluent
dashdb
fido-u2f
sipp
firemonkey-style
uri
winsock
quickcheck
android-collapsingtoolbar
serversocket
custom-keyboard
amazon-kms
landscape
ftp-server
jade4j
djcelery
elastica
xll
mars-simulator
sspi
rdp
newtons-method
latexml
sendinput
webalizer
rm
extjs4.1
watir-webdriver
webvtt
nslayoutconstraint
nivo-slider
stateless-session-bean
lvalue
ogg
haraka
deviare
oci
maximize
vaadin4spring
utf-16
polymerfire
seadragon
onepage-checkout
wikimedia-commons
boost-proto
textpattern
chicagoboss
http.client
srv-record
enaml
featuretoggle
swagger-maven-plugin
sequence-sql
com0com
actionpack
fuseesb
debug-symbols
thoughtworks-go
adwords-apiv201402
meteor-collections
radius-protocol
spring-security-acl
mute
non-ascii-characters
jquery-cycle
proc-open
tridion-2011
sfinae
gamesalad
fitch-proofs
uploading
zend-server-ce
net-use
cewolf
css-friendly
solandra
3-tier
dm
main-method
autobench
remote-working
rootkit
synthesizer

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