473,795 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

If Checkbox Is Empty, Display Different Result

6 New Member
I have a simple php form that is submitted and the details are emailed to someone. The problem is that there is a checkbox and if the checkbox is clicked then it sends the result as "Yes". However when it's not clicked I'd like the result to appear as "No" but I can't seem to do this. This is my code:

Expand|Select|Wrap|Line Numbers
  1. <?php    $radiobutton = $_REQUEST['radiobutton'] ;  $select = $_REQUEST['select'] ;  $textarea = $_REQUEST['textarea'] ;  $textfield = $_REQUEST['textfield'] ;  $textfield2 = $_REQUEST['textfield2'] ;  $textfield3 = $_REQUEST['textfield3'] ;  $textfield4 = $_REQUEST['textfield4'] ;  $checkbox = $_REQUEST['checkbox'] ;       
  2.  // multiple recipients  
  3. $to  = 'email@email.com' . ', '; // note the comma  $to .= 'email@email.com';    // subject  $subject = 'Feedback';    // if (empty($cip_req)) {  //    $cip_req = "No";  //    }    // message  $message = "<b>Type Of Comment : </b>" . $radiobutton . "<br /><b>Comment Subject : </b>" . $select . "<br /><b>Comments : </b>" . $textarea . "<br /><b>Name : </b>" . $textfield . "<br /><b>Email : </b>" . $textfield2 . "<br /><br /><b>Phone : </b>" . $textfield3 . "<br /><b>Fax : </b>" . $textfield3 . "<br /><br /><b>Please contact me as soon as possible : </b>" . $checkbox;    // To send HTML mail, the Content-type header must be set  $headers  = 'MIME-Version: 1.0' . "\r\n";  $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";    // Additional headers  $headers .= 'To: Me <email@email.com>' . "\r\n";  $headers .= 'From: Website <email@email.com>' . "\r\n";    if (($checkbox)) {      $checkbox = No;  }    // Mail it  mail($to, $subject, $message, $headers);  header("Location: index.php");  ?>
Sep 27 '07 #1
3 4473
Atli
5,058 Recognized Expert Expert
Hi Pope. Welcome to The Scripts!

If a checkbox is not checked, it will not be included in the HTTP request, which means that it will not exists in the $_POST, $_GET or $_REQUEST super-globals.

So to test if a checkbox has been checked, use the isset() function to see if it exists.

For example:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     if(isset($_POST['checked'])) {
  3.         echo "<p>The box IS checked!</p>";
  4.     }
  5.     else {
  6.         echo "<p>The box IS NOT checked!</p>";
  7.     }
  8. ?>
  9. <form action="?" method="POST">
  10.     <input type="checkbox" name="checked" /><br />
  11.     <input type="submit" />
  12. </form>
  13.  
Sep 27 '07 #2
Pope Pius X2
6 New Member
Hey, thank you very much! So for example, if I wanted, would it be possible to do something like this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     if(!isset($_POST['checked'])) {
  3.         $checked = "No";
  4.     }
  5.     else {
  6.     }
  7. ?>
  8. <form action="?" method="POST">
  9.     <input type="checkbox" name="checked" /><br />
  10.     <input type="submit" />
  11. </form>
Sep 28 '07 #3
Atli
5,058 Recognized Expert Expert
Hey, thank you very much! So for example, if I wanted, would it be possible to do something like this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     if(!isset($_POST['checked'])) {
  3.         $checked = "No";
  4.     }
  5.     else {
  6.     }
  7. ?>
  8. <form action="?" method="POST">
  9.     <input type="checkbox" name="checked" /><br />
  10.     <input type="submit" />
  11. </form>
Sure that would work, tho you should remove the empty else block, unless you plan to use it?

This code, by itself, would no do much, but it will compile and run fine.
Sep 28 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
5041
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I double-checked the path to my error log. It is in /var/www/logs/php_error_log Thanks. :) -Wayne Stevenson
3
8903
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns a empty value instead of returning the browser type. Here is the line which i am using in my code and from manual: <?php echo $_SERVER; ?>
4
6174
by: Shufen | last post by:
Hi, I'm a newbie that just started to learn python, html and etc. I have some questions to ask and hope that someone can help me on. I'm trying to code a python script (with HTML) to get values from a html form that consists of about 10 checkbox and a textbox where user have to key in a value to perform a search. From python tutors, I learned that I have to use the following method:
4
4671
by: Bill | last post by:
I have a catalog of books which need to be categorized into different groups. Some of the books can be listed under more than one category. I'm creating a page where I can assign a group of books to a category by checking the checkbox next to their name (say I've looped out 100 of them, and check some, and not others) then submitting the form for processing, so that those titles where the checkbox is checked will get the categoryid number...
1
4512
by: dx | last post by:
I'm extremely frustrated with ASP.NET...again! To me this should be as simple as setting oCheckBox.Checked = True.. yet for some reason it isn't. I have a user control (ascx) that that has a checkbox and I can't get it to default to checked. I tried radiobuttons and experienced the same result.. can't start them as checked. The really frustrating thing is that I set the attributes of other input controls in the Init() with no problem. ...
4
7504
by: mamun | last post by:
Hi All, I have the following situation and am looking for answer in C#. I have a datagrid and putting checkbox next to each record. In the header I have a Delete button. I want users to checkchekboxes and click the Delete button. That will show a confirmation dialog message with the items they choose to delete.
8
4463
by: Sid | last post by:
I hope someone could help me with this. I am trying to setup a criteria to decide when to allow/not allow user to click on the check box. logically it looks simple but I am not able to incorporate in my data access page. I have a numerical field called tier status and right next to it I have a checkbox. I would like to allow user to check and uncheck the checkbox as long as the tier
10
4492
by: LionsDome | last post by:
Hello, I have a vb.net page which a bunch of checkboxes. A user can select a checkbox(s) and hit the submit button to store those values in a SQL Server table. This works fine with no problem the first time the user submits. However when user submits a second time while changing some of the selected boxes the page only re-submits the previosly selected checkbox values. It's like its storing it somewhere in the cache or something. My...
0
1714
by: remya1000 | last post by:
I have a field called Departments in my database. and I have 3 monitors. So when Page_Load, I need to check number of departments I have in database. And depends upon that number of departments I have, I need to calculate number of checkbox I need to display. NumCheckBox = NumDepartments * 3 And then I need to create a table like structure with 4 fields. First column -Department Name and remaining 3 columns will be Monitor 1-3. and...
0
10437
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.