Ghost in the machine?

I’ve done this a million times. What am I doing wrong?

When I run the following code & hit the submit button, the $_POST array shows all its values using print_r but when I try to extract the data I get nothing.

<?php // filename.php

if (isset($_POST['submit'])) {

	echo '<pre>';
	print_r ($_POST);
	echo '</pre>';

	echo 'file = ' . $_POST['img_data']['file'];
	echo '<br/>path = ' . $_POST['img_data']['path'];
	
	die();
}

?>

<!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>Title</title>

<SCRIPT LANGUAGE="JavaScript" SRC="includes/scripts/common.js"></SCRIPT>

<style text/css>Define Styles</style>

</head>
<body>

<form action="<?=$_SERVER['PHP SELF']; ?>" method="post">


<input type="hidden" name="img_data['file']" value="spider.jpg" />
<input type="hidden" name="img_data['path']" value="../images/spider.jpg" />

<input type="submit" name="submit" value="Save New Item" /><br/>

</form>

</body>
</html>

This is what I get when I run this script and submit it:

Array
(
    [img_data] => Array
        (
            ['file'] => spider.jpg
            ['path'] => ../images/spider.jpg
        )

    [submit] => Save New Item
)
file =
path =

I must have something wrong here. What is it???

–Kenoli

I solved my problem. It was single quotes in the html name arrays.

Hours looking in the wrong place.

–Kenoli

Didn’t see that you have already solved your problem.


<?php // filename.php

if (isset($_POST['submit'])) { 

	echo '<pre>';
	print_r ($_POST);
	echo '</pre>';

	echo 'file = ' . $_POST['img_data']['file'];
	echo '<br/>path = ' . $_POST['img_data']['path'];
	
	die();
}

?>

<!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>Title</title>

<SCRIPT LANGUAGE="JavaScript" SRC="includes/scripts/common.js"></SCRIPT>

<style text/css>Define Styles</style>

</head>
<body>

<form action="<?=$_SERVER['PHP SELF']; ?>" method="post">


<input type="hidden" name="img_data[file]" value="spider.jpg" />
<input type="hidden" name="img_data[path]" value="../images/spider.jpg" />

<input type="submit" name="submit" value="Save New Item" /><br/>

</form>

</body>
</html>

[QUOTE=tom8;5144391]Didn’t see that you have already solved your problem.

I felt silly when I realized what I had overlooked, but I did discover the odd anomaly that when I put quotes around the array keys, it does pass the array, as it can be displayed with print_r but the array values cannot be accessed directly. I am curious about why this is.

–Kenoli

Take a look this page in php.net, it gives you explanation and examples:

http://www.php.net/manual/en/language.variables.external.php

Thanks, though I didn’t see anything about the fact that if you put html array keys in single quotes the array is passed in the POST to print_r in php but the individual array elements are not accessible. One would think it would show nothing in print_r as well. Seems like an odd anomaly.

–Kenoli

Not really - print_r doesn’t care what an item array index is called - it just loops through the array(s) to display what’s in there. It’s when you attempt to access the index that it matters since ‘indexname’ is different than indexname since the singlequotes are included in the index name.

Just one of the problems that can occur when the single quote is treated the same as the double quote when dealing with character/string data.

This is caused by a HTML anomaly probably something to do with “strictness” (for which I cannot find you a good explanation to link to), not PHPs handling of the incoming var - which on the whole it does rather well I think.

Just don’t add an illegal space in your array key :wink: - you might get a shock.


// html form element
<input type=text name=var[oh dear] value="asdf"/>

//var_dump($_POST) gives me:
array
  'var_oh' => string 'asdf' (length=4)

Nice try but no cigar.