• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • 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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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