[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

Why are my checkbox names coming up as undefined?

Hi, you will see a div with check boxes in it

http://auroriella.com/edit_product.php?edit=bracelets&item=b3

They are the only form elements on my page that are coming up undefined and I cant figure out why!?  Any ideas?
0
FairyBusiness
Asked:
FairyBusiness
  • 8
  • 7
3 Solutions
 
Greg AlexanderLead DeveloperCommented:
Since you are dealing with more then one checkbox with the same name you need to have a [] at the end of your names so

<input type="checkbox" name="materials" value="8" />

becomes

<input type="checkbox" name="materials[]" value="8" />
0
 
Marco GasiFreelancerCommented:
That Notice means that there is some problem with the array of values submitted to the page. Check it or post the code of library.php: without the code is very difficult say where is the bug :-)

Cheers
0
 
FairyBusinessAuthor Commented:
Ok here is the function I used to create the check boxes

function materials() {
    global $conn, $materials;
	$query = "SELECT * FROM materials";
	$query .= " ORDER BY type ASC";
	$result = mysql_query($query, $conn);
	confirm_query($result);
	if(isset($_GET['edit'])) {
		$output = "";
		while ($rows = mysql_fetch_array($result)) {
			$output .= "<input type=\"checkbox\" name=\"materials[]\" value=\"" . $rows['id'] . "\" ";
			for($i = 0; $i <= count($materials)-1; $i++) {
				if($materials[$i] == $rows['id'] && !empty($rows['type'])) {
					$output .= "checked ";
				}
			}
		$output .=  "/>" . $rows['type'] . "<br />";
		}
	}
	else {
		$output = "<h4>Materials</h4>";
		$output .= "<ul>";
		while ($rows = mysql_fetch_array($result)) {
			for($i = 0; $i <= count($materials)-1; $i++) {
				if($materials[$i] == $rows['id'] && !empty($rows['type'])) {
					$output .= "<li>" . $rows['type'] . "</li>";
				}
			}
		}
		$output .= "<li>" . metal_name() . "</li>";
		$output .= "</ul>";
	}
	return $output;
}

Open in new window


changed them to materials[] but they are still undefined. ..
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Greg AlexanderLead DeveloperCommented:
Is your problem when you submit, they do not show up in the $_POST data or are you trying to validate them with javascript and they are not in the DOM.. could you be more specific about the problem?
0
 
FairyBusinessAuthor Commented:
I'm not trying to do anything with them yet until there are no errors!

This is what I had in the function so far:

function update_materials() {
      global $conn, $message, $errors, $error;
      if(empty($errors) && isset($_POST['submit'])) {
            $materials = $_POST['materials'];
            print $materials;
      }
}


But then I stopped when I got the error of them being undefined.  Eventually I will be inserting them into a database table. . .
0
 
Greg AlexanderLead DeveloperCommented:
Try this, the meterials are now coming through as an array because of the []:
function update_materials() {
      global $conn, $message, $errors, $error;
      if(empty($errors) && isset($_POST['submit'])) { 
            $materials = $_POST['materials'];
			foreach($materials as $value){
				print "$value";	
			}
            
      }
}

Open in new window


0
 
FairyBusinessAuthor Commented:
Nope, that just added on another error message lol
0
 
Greg AlexanderLead DeveloperCommented:
Its certainly not how you are creating the checkboxes, its how you are processing them.. When you run the below on your function, what do you see... leave it up for me to look at if you don't care

function update_materials() {
      echo "<pre>";
      print_r($_POST);
      echo "</pre>";

      global $conn, $message, $errors, $error;
      if(empty($errors) && isset($_POST['submit'])) { 
            $materials = $_POST['materials'];
                        foreach($materials as $value){
                                print "$value"; 
                        }
            
      }
}

Open in new window

0
 
FairyBusinessAuthor Commented:
0
 
FairyBusinessAuthor Commented:
Actually, it doesnt. It only lists 4
0
 
Greg AlexanderLead DeveloperCommented:
Ahh, you need to move the

<form method="post" action="">

above the material checkboxes
0
 
FairyBusinessAuthor Commented:
I did but its still acting up!
0
 
Greg AlexanderLead DeveloperCommented:
Looks perfect to me, well at least it is displaying
Array
(
    [name] => Copper & Royal Purple Bracelet
    [content] => This copper and royal purple bracelet radiates with depth of color and elegance. Pair with our other Carmen jewelry for pure enchantment.
    [materials] => Array
        (
            [0] => 26
            [1] => 8
            [2] => 28
            [3] => 5
            [4] => 9
        )

    [price] => $35.00
    [color] => copper & purple
    [qty] => 1
    [metal_id] => 5
    [submit] => Update
)

Open in new window


that is what you were wanting, correct?
0
 
FairyBusinessAuthor Commented:
wow I'm totally not seeing that!!
0
 
Greg AlexanderLead DeveloperCommented:
Hmm... I clicked the link you put in the post above, then picked a few materials and clicked the update... there are several other issues, but the question was answered... at least this one lol :)
0
 
FairyBusinessAuthor Commented:
I exited out of the page and came back and its showing now :))  Thanks again!!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now