SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    array in hidden text values

    Greetings! I have here code that send the array value to other form.

    Code:
    <p><input type = 'hidden' name = 'Lname[]' value = '<?php echo $Lname;?>'></p>
    <y problem is when I tried to echo my variable it shows only its single value of my array. Is there any way to get rid of these.

    Code:
    $Lname = $_POST['Lname'];
    foreach($Lname as $L_val => $Lname){ 
        echo $Lname.'<br/>';
       }
    Thank you.

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your question is not clear. please elaborate on what you want the result to be and then show us the results of your current var_dump($_POST)

  3. #3
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    56 Post(s)
    Tagged
    0 Thread(s)
    If PHP needs to give a javascript an array the best approach is to use json_encode

    Code php:
    <script type="text/javascript">
    var MyArray = <?= json_encode( $MyArray ) ?>;
    </script>

    HTML input elements cannot hold more than one value. If you do need to encode an array into a form for some reason you'll have to create an input element for each value as follows.

    Code php:
    <?php foreach ( $array as $key => $value ): ?>
    <input type="hidden" name="myarray[<?= $key ?>]" value="<?= $value ?>" >
    <?php endforeach ?>

  4. #4
    SitePoint Addict FizixRichard's Avatar
    Join Date
    May 2003
    Location
    UK
    Posts
    372
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    If PHP needs to give a javascript an array the best approach is to use json_encode

    Code php:
    <script type="text/javascript">
    var MyArray = <?= json_encode( $MyArray ) ?>;
    </script>

    HTML input elements cannot hold more than one value. If you do need to encode an array into a form for some reason you'll have to create an input element for each value as follows.

    Code php:
    <?php foreach ( $array as $key => $value ): ?>
    <input type="hidden" name="myarray[<?= $key ?>]" value="<?= $value ?>" >
    <?php endforeach ?>

    ^ This, however. The OP's input is hidden, so it would suggest to me they are looking for a way to move an array of variables from one place to another (script wise) along with the main form, for which I would probably suggest using a session to achieve.
    FIZIX - Full Service Digital Agency - Engaging websites, apps and games.
    Follow us @FIZIXAgency

  5. #5
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    56 Post(s)
    Tagged
    0 Thread(s)
    Using session certainly achieves this within the PHP pages themselves. Since the OP hasn't clarified what they need to do it's hard to know what answer is best. But as session is such an obvious solution I posted with the assumption that PHP needs to share something with javascript. And who knows, the page might be building a form that will be submitted to a server not under the programmer's control for some reason.

  6. #6
    SitePoint Addict FizixRichard's Avatar
    Join Date
    May 2003
    Location
    UK
    Posts
    372
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    True, I just read the op and assumed they were just trying to pass a variable from one script to another either with or along with a form.

    But maybe I have came to that conclusion hastily.
    FIZIX - Full Service Digital Agency - Engaging websites, apps and games.
    Follow us @FIZIXAgency

  7. #7
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    I used this code but an error occurred.
    <?php foreach ( $array as $key => $value ): ?>
    <input type="hidden" name="myarray[<?= $key ?>]" value="<?= $value ?>" >
    <?php endforeach ?>


    Warning: Invalid argument supplied for foreach() in C:\


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
  •