Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5
This is the forum to discuss the Wrox book Beginning PHP4 by Wankyu Choi, Allan Kent, Chris Lea, Ganesh Prasad, Chris Ullman; ISBN: 9780764543647
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #11 (permalink)  
Old March 29th, 2004, 04:18 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

As we've mentioned before, there are many problems with the kind of code you (cyberwolf) posted.

First of all, $License will only exist if 1) register_globals is off *AND* the "License" input field was submitted.

If the checkbox doesn't exist, then $License doesn't exist either. The first time you attempt to access $License, PHP will throw a NOTICE level warning that you're attempting to access the value of a nonexistant variable. This is the "undefined variable" warning you see if error_reporting is E_ALL.

PHP will create this variable and give it a default empty value. For strings, this value is interpreted as the empty string (""). For numbers, it's zero. For booleans, it's FALSE.

Therefore, empty($License) will return true if License wasn't submitted, only because PHP creates it. That also means that assigning the empty string to it is pointless -- PHP already created it with the value of the empty string.


I harp on this issue because PHP has (for a couple years now) uses default configuration settings which would prevent your script from working. As Rich and I have posted countless times, the proper way to do it is:

$License = isset($_POST['License'])? $_POST['License'] : "";

The use of isset() prevents undefined variable warnings from being logged.



Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #12 (permalink)  
Old February 5th, 2006, 09:59 AM
Registered User
 
Join Date: Feb 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Another way to test the checkbox is to also test to see if the form has been submitted. Thus in this example, by default this checkbox is checked, but if the end user un-checks the box and forgets to complete any of the other fields and submits the form then the form is redrawn with the correct status of that checkbox (checked or un-checked).

Code:
<?php

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; 
charset=iso-8859-1" />
<title>Sign Up For News...</title>
</head>
<body>
<p>Enter your information below to sign up for our news...</p>
<form name="register" id="register" method="post" 
action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="fname">First Name:</label> 
<input name="fname" id="fname" type="text" size="32" 
maxlength="32" value="
<?php 
    if(isset($_REQUEST['fname'])&&$_REQUEST['fname']!=""){
        print($_REQUEST['fname']);
    } 
?>" />
<label for="lname">Last Name:</label> 
<input name="lname" id="lname" type="text" size="32" 
maxlength="32" value="
<?php 
    if(isset($_REQUEST['lname'])&&$_REQUEST['lname']!=""){
        print($_REQUEST['lname']);
    } 
?>" />
<label for="email">E-mail Address:</label> 
<input name="email" id="email" type="text" size="32" 
maxlength="32" value="
<?php 
    if(isset($_REQUEST['email'])&&$_REQUEST['email']!=""){
        print($_REQUEST['email']);
    } 
?>" />
<input name="optin" id="optin" type="checkbox" value="yes" 
<?php 
    if( (isset($_REQUEST['submit'])) && (!isset($_REQUEST['optin'])) ){ 
        print ''; 
    }elseif( (isset($_REQUEST['submit'])) && (isset($_REQUEST['optin'])) ){ 
        print 'checked="checked"'; 
    }else{ 
        print 'checked="checked"'; 
    } 
?> /> 
<label for="optin">Yes include me in your e-mailings.</label>
<input name="submit" id="submit" value="Register" 
type="submit" class="submit-button" />
</body>
</html>
Reply With Quote
  #13 (permalink)  
Old March 1st, 2006, 01:34 PM
Registered User
 
Join Date: Mar 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi, JOANNCAE, how was it that you fixed the problem?

im new to php, i have created a form that send info to a database, however i have also put a checkbox in.. when the checkbox is selected all is ok, however the checkboxe returns nothing (neither a name nor a value) if unchecked.

How do i solve this? sorry if this is already mentioned above but it all seems a bit complicated...

(i can supply my code if needed)
Alfie

Reply With Quote
  #14 (permalink)  
Old March 6th, 2006, 07:36 AM
Registered User
 
Join Date: Mar 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Anyone?

Reply With Quote
  #15 (permalink)  
Old April 23rd, 2006, 06:07 AM
Authorized User
 
Join Date: May 2005
Location: , , .
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by alfie
 hi, JOANNCAE, how was it that you fixed the problem?

im new to php, i have created a form that send info to a database, however i have also put a checkbox in.. when the checkbox is selected all is ok, however the checkboxe returns nothing (neither a name nor a value) if unchecked.

How do i solve this? sorry if this is already mentioned above but it all seems a bit complicated...

(i can supply my code if needed)
Alfie

Post back your code please... I suspect it's a simple matter of validation.

Reply With Quote
  #16 (permalink)  
Old May 25th, 2006, 04:54 AM
eda eda is offline
Registered User
 
Join Date: May 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi guys...
the one that posted by nikolai on 24/09/2003 is similar as what i want... but i need the button "select all" and "unselect all" for the checkbox.one more thing is once we submit, i need the value display in the text area(the text area is in the different window with checkbox form) .. could somebody help me?

thanks in advance...



Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
checkbox checked by default by html:checkbox sachin.tathod Struts 3 December 4th, 2006 03:41 PM
php - checkbox msrinivas PHP How-To 0 March 7th, 2006 03:17 AM
How to insert checkbox values to mysql using php method PHP Databases 0 February 21st, 2006 10:47 PM
Error: movie.php & commit.php on p182-186, ch6 willburke BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 October 12th, 2004 02:48 PM
php and javascript checkbox conflict sam PHP How-To 7 June 8th, 2004 06:06 PM



All times are GMT -4. The time now is 05:05 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.