horseatingweeds Posted August 13, 2007 Share Posted August 13, 2007 I've written a script to populate a list of check box elements but it doesn't work for the first one. Can anyone see what I'm doing wrong. The error is that the variable $pup is undefined in the 'Doberman Puppies' element. Here's the code. From the form page which is included. <fieldset> <legend>What do Your Activities Provide?</legend> <input type='checkbox' name='chkActivities[]' value='Doberman Puppies' <?php popActivities($pup); ?> /><label>Doberman Puppies</label><br /> <input type='checkbox' name='chkActivities[]' value='Adult Dobermans' <?php popActivities($adult); ?> /><label>Adult Dobermans</label><br /> <input type='checkbox' name='chkActivities[]' value='Trained Dobermans' <?php popActivities($trained); ?> /><label>Trained Dobermans</label><br /> <input type='checkbox' name='chkActivities[]' value='Stud Service' <?php popActivities($stud); ?> /><label>Stud Service</label><br /> <input type='checkbox' name='chkActivities[]' value='Doberman Rescue' <?php popActivities($rescue); ?> /><label>Doberman Rescue</label><br /> </fieldset> The next two parts are on the main php page. $pup = ''; $adult = ''; $trained = ''; $stud = ''; $rescue = ''; There is other code between these two pieces but they are on the same main page. function popActivities($a) { if (isset($_POST['chkActivities'])) { foreach($_POST['chkActivities'] AS $activity) { if ($activity == 'Doberman Puppies') { GLOBAL $pup; $pup = "checked='checked'"; } if ($activity == 'Adult Dobermans') { GLOBAL $adult; $adult = "checked='checked'"; } if ($activity == 'Trained Dobermans') { GLOBAL $trained; $trained = "checked='checked'"; } if ($activity == 'Stud Service') { GLOBAL $stud; $stud = "checked='checked'"; } if ($activity == 'Doberman Rescue') { GLOBAL $rescue; $rescue = "checked='checked'"; } } } echo $a; } Quote Link to comment https://forums.phpfreaks.com/topic/64585-understanding-a-variable-problem/ Share on other sites More sharing options...
btherl Posted August 13, 2007 Share Posted August 13, 2007 Can you please post your full code? I can't see anything wrong with what you've posted, so I suspect the problem is in how you put the parts together. PS, the HTML for a checked checkbox is just "checked", not "checked='checked'". So your code can be simplified to: $pup = "checked"; Quote Link to comment https://forums.phpfreaks.com/topic/64585-understanding-a-variable-problem/#findComment-322018 Share on other sites More sharing options...
horseatingweeds Posted August 13, 2007 Author Share Posted August 13, 2007 Here are the two pages: <?php if (isset($_POST['submit'])) { echo "<p class='error'>" . $valid . "</p>"; } ?> <form name='form1' id='form1' enctype='multipart/form-data' action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post" > <div id='leftColumn'> <fieldset> <legend>Contact Information</legend> Listing Title:<br /> <input type='text' name='title' size='30' maxlength='70' value='<?php echo $_POST['title']; ?>' /><br /> <span>*</span> E-mail Address:<br /> <input type='text' name='email' size='40' maxlength='60' value='<?php echo $_POST['email']; ?>' /><br /> <span>*</span> Re-enter E-mail Address:<br /> <input type='text' name='reEnterEmail' size='40' maxlength='60' value='<?php echo $_POST['reEnterEmail']; ?>' /><br /> Phone Number:<br /> <input type='text' name='phone' size='20' maxlength='50' value='<?php echo $_POST['phone']; ?>' /><br /> </fieldset> <fieldset> <legend><span>*</span> Location</legend> <select size='1' name='state'> <?php build_select(); ?> </select> </fieldset> <fieldset> <legend>Current Availability of Puppies</legend> <input type='radio' name='radAvailability' value='Puppies Currently Available' <?php if ($availability == 'Puppies Currently Available') {echo "checked='checked'";} ?> /><label>Curently Available</label><br /> <input type='radio' name='radAvailability' value='Litter Currently Planned' <?php if ($availability == 'Litter Currently Planned') {echo "checked='checked'";} ?> /><label>Litter Planned</label><br /> <input type='radio' name='radAvailability' value='No Litters Planned' <?php if ($availability == '' || $availability == 'No Litters Planned') {echo "checked='checked'";} ?> /><label>No Litters Planned</label><br /> </fieldset> </div> <div id='rightColumn'> <fieldset> <legend>What do Your Activities Provide?</legend> <input type='checkbox' name='chkActivities[]' value='Doberman Puppies' <?php popActivities($pup); ?> /><label>Doberman Puppies</label><br /> <input type='checkbox' name='chkActivities[]' value='Adult Dobermans' <?php popActivities($adult); ?> /><label>Adult Dobermans</label><br /> <input type='checkbox' name='chkActivities[]' value='Trained Dobermans' <?php popActivities($trained); ?> /><label>Trained Dobermans</label><br /> <input type='checkbox' name='chkActivities[]' value='Stud Service' <?php popActivities($stud); ?> /><label>Stud Service</label><br /> <input type='checkbox' name='chkActivities[]' value='Doberman Rescue' <?php popActivities($rescue); ?> /><label>Doberman Rescue</label><br /> </fieldset> <fieldset> <legend>Details</legend> Your details should consist of less than 200 characters and could include your breeding philosophy, details on currently available Dobermans, or any other details about your Doberman activities. <textarea name='description' cols=32 rows=6 maxlength="200" onkeyup="return ismaxlength(this)"> <?php echo $_POST['description']; ?> </textarea> </fieldset> </div> <p><span>*</span> Required Field</p> <!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> <title>Listing Builder</title> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" /> <meta name="keywords" content='' /> <meta http-equiv='author' content='Benjamin Warrick' /> <script language="javascript" type="text/javascript" src="../scripts/search.js"></script> <script language="javascript" type="text/javascript" src="../scripts/matching_columns.js"></script> <script language="javascript" type="text/javascript" src="../scripts/dic.js"></script> <link rel='stylesheet' type='text/css' href='../styles/styles_1.css' media='screen' /> <link rel='stylesheet' type='text/css' href='styles/styles_input.css' media='screen' /> <!--[if lt IE 7]> <link rel='stylesheet' type='text/css' href='../styles/iestyles_1.css' media='screen' /> <style type='text/css' media='all'> </style> <![endif]--> <script language="javascript" type="text/javascript" /> function edit_back() { history.back(); } function ismaxlength(obj){ var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : "" if (obj.getAttribute && obj.value.length>mlength) obj.value=obj.value.substring(0,mlength) } </script> </head> <body> <div id='title'><a href='index.php'> <?php include($_SERVER['DOCUMENT_ROOT'] . '/gentledoberman/includes/title-image-form.php');?> <h1><a id='top'>Listing Form</a></h1> </div> <!-- Menu Start --> <!-- Menu End --> <!-- Left Bar Start --> <div id='leftbar' class='column'> <?php include($_SERVER['DOCUMENT_ROOT'] . '/gentledoberman/includes/leftbar-bottom.php');?> </div> <!-- Left Bar End --> <!-- Content Start --> <div id='content' class='column'> <?php $title = $_POST['title']; $email = $_POST['email']; $_state = $_POST['state']; $description = $_POST['description']; $availability = $_POST['radAvailability']; function build_select() { $state = ' UNITED STATES| Alaska| Alabama| Arkansas| American Samoa| Arizona| California| Colorado| Connecticut| D.C.| Delaware| Florida| Georgia| Hawaii| Iowa| Idaho| Illinois| Indiana| Kansas| Kentucky| Louisiana| Massachusetts| Maryland| Maine| Marshall Islands| Michigan| Minnesota| Missouri| Marianas| Mississippi| Montana| North Carolina| North Dakota| Nebraska| New Hampshire| New Jersey| New Mexico| Nevada| New York| Ohio| Oklahoma| Oregon| Pennsylvania| Puerto Rico| Rhode Island| South Carolina| South Dakota| Tennessee| Texas| Utah| Virginia| Virgin Islands| Vermont| Washington| Wisconsin| West Virginia| Wyoming| CANADA| Alberta| Manitoba| Alberta| British Columbia| Manitoba| New Brunswick| Newfoundland and Labrador| Nova Scotia| Northwest Territories| Nunavut| Ontario| Prince Edward Island| Quebec| Saskatchewan| Yukon Territory| UNITED KINGDOM| Avon| Bedfordshire| Berkshire| Buckinghamshire| Cambridgeshire| Cheshire| Cleveland| Cornwall| Cumbria| Derbyshire| Devon| Dorset| Durham| Essex| Gloucestershire| Greater London| Greater Manchester| Hampshire| Hereford & Worcestershire| Hertfordshire| Humberside| Isle of Man| Isle of Wight| Kent| Lancashire| Leicestershire| Lincolnshire| Merseyside| Norfolk| Northamptonshire| Northumberland| Nottinghamshire| Oxfordshire| Shropshire| Somerset| Staffordshire| Suffolk| Surrey| Sussex| Warwickshire| West Midlands| Wiltshire| Yorkshire| Other '; $state_arr = explode("|", $state); echo "<option value='" . $_POST['state'] . "'>" . $_POST['state'] . "</option>"; for ($i=0; $i < (count($state_arr)); $i++) { echo "<option value='" . $state_arr[$i] . "'>" . $state_arr[$i] . "</option>"; } } $valid = validate_form(); if (isset($_POST['submit'])) { if ($valid == '') { include ('listing-review.php'); include ('breeder-form.php'); echo "<input type='submit' name='submit' value='Update' />"; echo "</form>"; } else { echo "<h4>Some information was missing:</h4>"; include ('breeder-form.php'); echo "<input type='submit' name='submit' value='Next ->' />"; echo "<input type='reset' name='reset' value='reset' /></form>"; $error = $valid; } } else { echo "<h4>Please fill out the following form to creat your listing:</h4>"; include ('breeder-form.php'); echo "<input type='submit' name='submit' value='Next ->' />"; echo "<input type='reset' name='reset' value='reset' /></form>"; } function validate_form() { $response = ''; $title = $_POST['title']; $email = $_POST['email']; $reemail = $_POST['reEnterEmail']; $reg_exp = '^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,6})$'; $phone = $_POST['phone']; $state = $_POST['state']; if (((strlen(trim($title))) <= 0) || (!eregi("([A-Za-z0-9])", $title))) { $response = 'Please enter a title for your listing.'; } if (!eregi($reg_exp, $email)) { $response = 'Please enter a valid email address.'; } if (!($email == $reemail)) { $response = 'Please re-enter email addresses, they do not match.'; } if (($state == 'UNITED STATES') || ($state == 'CANADA') || ($state == 'UNITED KINGDOM')) { $country = 'the ' . $_POST['state']; if ($state == 'CANADA') { $country = $_POST['state']; } $response = 'Please enter a state within ' . $country . ' for your location.'; } /*if (!ereg("([0-9:punct:])", $phone)) { $response = 'Please enter a valid phone number.'; } */ return $response; } $pup; $adult; $trained; $stud; $rescue; function popActivities($a) { if (isset($_POST['chkActivities'])) { foreach($_POST['chkActivities'] AS $activity) { if ($activity == 'Doberman Puppies') { GLOBAL $pup; $pup = "checked='checked'"; } if ($activity == 'Adult Dobermans') { GLOBAL $adult; $adult = "checked='checked'"; } if ($activity == 'Trained Dobermans') { GLOBAL $trained; $trained = "checked='checked'"; } if ($activity == 'Stud Service') { GLOBAL $stud; $stud = "checked='checked'"; } if ($activity == 'Doberman Rescue') { GLOBAL $rescue; $rescue = "checked='checked'"; } } } echo $a; } ?> </div> <!-- Content End --> <!-- Footer Start --> <?php include($_SERVER['DOCUMENT_ROOT'] . '/gentledoberman/includes/footer-temperament.php');?> <!-- Footer End --> <!-- SiteSearch Google Start --> <?php include($_SERVER['DOCUMENT_ROOT'] . '/gentledoberman/includes/search-box.php');?> <!-- SiteSearch Google End --> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/64585-understanding-a-variable-problem/#findComment-322034 Share on other sites More sharing options...
btherl Posted August 13, 2007 Share Posted August 13, 2007 In your latest code, you have just this: $pup; $adult; $trained; $stud; $rescue; Which is different from what you posted in your original post: $pup = ''; $adult = ''; $trained = ''; $stud = ''; $rescue = ''; The code in your original post is correct. However, it must come before you include your form. Is your form "breeder-form.php" ? If so, you must set the values of $pup and so on before you include that form. In case this is what's confusing you, it doesn't matter that the function popActivities() is defined after $pup is defined. What matter is that $pup is defined before popActivities() is called, which is where your form is included. Quote Link to comment https://forums.phpfreaks.com/topic/64585-understanding-a-variable-problem/#findComment-322063 Share on other sites More sharing options...
horseatingweeds Posted August 13, 2007 Author Share Posted August 13, 2007 That helps me understand why placing the variables in the top of the page stopped the error message. The script still doesn't work for the first check element. I've changes the variables to = '' i was just messing with things. So, the problem currently is that the first check element is not being changes by the function. ??? Quote Link to comment https://forums.phpfreaks.com/topic/64585-understanding-a-variable-problem/#findComment-322073 Share on other sites More sharing options...
horseatingweeds Posted August 14, 2007 Author Share Posted August 14, 2007 I've made a page consisting only of the problemed code. It is meant to keep all the elements checked after posting. The function works for all elements except the first element. I've moved them around and have found it is the first element regardless. <?php $pup = ""; $adult = ""; $trained = ""; $stud = ""; $rescue = ""; function popActivities($a) { if (isset($_POST['chkActivities'])) { foreach($_POST['chkActivities'] AS $activity) { switch ($activity) { case 'Doberman Puppies': GLOBAL $pup; $pup = "checked='checked'"; break; case 'Adult Dobermans': GLOBAL $adult; $adult = "checked='checked'"; break; case 'Trained Dobermans': GLOBAL $trained; $trained = "checked='checked'"; break; case 'Stud Service': GLOBAL $stud; $stud = "checked='checked'"; break; case 'Doberman Rescue': GLOBAL $rescue; $rescue = "checked='checked'"; break; } } } echo $a; } if (isset($_POST['chkActivities'])) {foreach($_POST['chkActivities'] AS $activity) echo $activity . '<br />';} ?> <form name='form1' id='form1' enctype='multipart/form-data' action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post" > <input type='checkbox' name='chkActivities[]' value='Doberman Puppies' <?php popActivities($pup); ?> /><label>Doberman Puppies</label><br /> <input type='checkbox' name='chkActivities[]' value='Adult Dobermans' <?php popActivities($adult); ?> /><label>Adult Dobermans</label><br /> <input type='checkbox' name='chkActivities[]' value='Trained Dobermans' <?php popActivities($trained); ?> /><label>Trained Dobermans</label><br /> <input type='checkbox' name='chkActivities[]' value='Stud Service' <?php popActivities($stud); ?> /><label>Stud Service</label><br /> <input type='checkbox' name='chkActivities[]' value='Doberman Rescue' <?php popActivities($rescue); ?> /><label>Doberman Rescue</label><br /> <input type='submit' name='submit' value='Next ->' /> </form> <p>print_r($_POST['chkActivities']) produces this:</p> <?php print_r($_POST['chkActivities']) ?> Quote Link to comment https://forums.phpfreaks.com/topic/64585-understanding-a-variable-problem/#findComment-323173 Share on other sites More sharing options...
btherl Posted August 14, 2007 Share Posted August 14, 2007 I suggest the following simpler code: <?php $pup = ""; $adult = ""; $trained = ""; $stud = ""; $rescue = ""; foreach($_POST['chkActivities'] AS $activity) { switch ($activity) { case 'Doberman Puppies': $pup = "checked"; break; case 'Adult Dobermans': $adult = "checked"; break; case 'Trained Dobermans': $trained = "checked"; break; case 'Stud Service': $stud = "checked"; break; case 'Doberman Rescue': $rescue = "checked"; break; } } if (isset($_POST['chkActivities'])) {foreach($_POST['chkActivities'] AS $activity) echo $activity . '<br />';} ?> <form name='form1' id='form1' enctype='multipart/form-data' action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post" > <input type='checkbox' name='chkActivities[]' value='Doberman Puppies' <?php echo $pup; ?> /><label>Doberman Puppies</label><br /> <input type='checkbox' name='chkActivities[]' value='Adult Dobermans' <?php echo $adult; ?> /><label>Adult Dobermans</label><br /> <input type='checkbox' name='chkActivities[]' value='Trained Dobermans' <?php echo $trained; ?> /><label>Trained Dobermans</label><br /> <input type='checkbox' name='chkActivities[]' value='Stud Service' <?php echo $stud; ?> /><label>Stud Service</label><br /> <input type='checkbox' name='chkActivities[]' value='Doberman Rescue' <?php echo $rescue; ?> /><label>Doberman Rescue</label><br /> <input type='submit' name='submit' value='Next ->' /> </form> <p>print_r($_POST['chkActivities']) produces this:</p> <?php print_r($_POST['chkActivities']) ?> I do not know where the error was, but the code above works for me. Quote Link to comment https://forums.phpfreaks.com/topic/64585-understanding-a-variable-problem/#findComment-323234 Share on other sites More sharing options...
horseatingweeds Posted August 14, 2007 Author Share Posted August 14, 2007 Well crap.... Thanks btherl, That was simple a strait forward. I have to remember that when something isn't working I should assume I'm getting to complicated rather than assuming I'm to simple. I'll just be glad when I get these php basics understood. Good work! I still wish I understood what the problem was. It's something to do with the first element and the foreach statement I think. Maybe it's a bug? Quote Link to comment https://forums.phpfreaks.com/topic/64585-understanding-a-variable-problem/#findComment-323249 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.