SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    Code:
    <?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:

    Code:
    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

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I solved my problem. It was single quotes in the html name arrays.

    Hours looking in the wrong place.

    --Kenoli

  3. #3
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Didn't see that you have already solved your problem.

    PHP Code:
    <?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>
    Last edited by tom8; Jul 1, 2012 at 04:31. Reason: Didn't see that you have already solved your problem..

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [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

  5. #5
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Take a look this page in php.net, it gives you explanation and examples:

    http://www.php.net/manual/en/languag...s.external.php

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    And . . .

    Quote Originally Posted by tom8 View Post
    Take a look this page in php.net, it gives you explanation and examples:

    http://www.php.net/manual/en/languag...s.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

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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 - you might get a shock.

    Code:
    // 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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •