Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    printing an array from a form

    I have a form setup and I'm passing the values to a php file. I'm trying to trouble-shoot by printing the array that I'm getting from the form like so:
    if($_GET){
    $array = $_GET;
    }else if($_POST){
    $array = $_POST;
    } else {
    echo "You must Access this file through a form."; // If someone accesses the file directly, it wont work
    }

    echo "{$array['2']}";
    echo "<br>";
    Will this print the value in the key=2 of the array (of the 3rd value in array) created from the GET or POST functions? Because I can't get it to print any value from the array.

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Are the fields dynamic and you not know what the names are?

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Location
    Connecticut, USA
    Posts
    400
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Start simpler. Just:

    PHP Code:
    if( isset( $_GET )) {
       
    print_r$_GET );

    and/or:

    PHP Code:
    if( isset( $_POST )) {
       
    print_r$_POST );

    Another way:

    PHP Code:
    if( isset( $_GET )) {
       foreach( 
    $_GET as $key=>$value ) {
          echo 
    "$key => $value<br>";
       }

    And what Nightfire is getting at is your form should have semantic field names, like for example, "username" or "password". In this case, you can reference those values by referring to them as $_POST["username"] or $_POST["password"] (or $_GET["username"] etc)

  • #4
    New Coder
    Join Date
    Feb 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the fields are dynamic. submitting form looks like so:

    <input type="checkbox" name="answer[]" value="2"/>
    input type="checkbox" name="answer[]" value="3"/>
    <input type="radio" name="Topics" value="No"/>

  • #5
    New Coder
    Join Date
    Feb 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks

  • #6
    Regular Coder
    Join Date
    Mar 2006
    Location
    Connecticut, USA
    Posts
    400
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by geewhiz
    the fields are dynamic. submitting form looks like so:
    Right. Let's assume the method is POST. In this case:

    PHP Code:
    if( isset( $_POST["answer"] )) {
       foreach( 
    $_POST["answer"] as $key=>$value ) {
          echo 
    "$key => $value<br>";
       }

    Or, since the $keys are just going to be numbered indices and you will know those already, just do:

    PHP Code:
    if( isset( $_POST["answer"] )) {
       foreach( 
    $_POST["answer"] as $value ) {
          echo 
    "$value<br>";
       }


  • #7
    New Coder
    Join Date
    Feb 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Will this correctly return the count of answer? Like so:

    count($answer)
    I'm using it in an loop like so:

    for ($i=0; $i < count($answer); $i++) {

    echo "The value of i is $i" . "<br>";
    I get no values back for $i, never gets to the echo. It must be something wrong with the loop?
    Last edited by geewhiz; 04-28-2006 at 08:00 PM.

  • #8
    Regular Coder
    Join Date
    Mar 2006
    Location
    Connecticut, USA
    Posts
    400
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by geewhiz
    Will this correctly return the count of answer?
    only if you've assigned the posted form value to $answer like:

    $answer = $_POST['answer'];

    (or $_GET['answer'], whatever)

    otherwise, use

    PHP Code:
    for ($i=0$i count($_POST['answer']); $i++) {
       echo 
    "The value of i is $i" "<br>";



  •  

    Posting Permissions

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