0

Let me explain further. I have a select form on my first php page (lets call this page first.php). I do have a submit button. I am catching the array on the second page (lets cal this page sec.php) with $_POST and then setting it to a PHP variable. However, I can not get it to print. Here's what my code/mark up looks like on first.php

<label>Product:</label>
    <select name="arr[]">
        <option value="Mobile">Mobile</option>
        <option value="Social">Social</option>
        <option value="Online">Online</option>
    </select>

Note = I know I don't need to have an array for this. But I want to keep it this way.

Here's what my code looks like on sec.php:

<?php
    $arr= $_POST['arr'];
?>

I want it to print in this HTML table:

<tr>
    <td width="200"> <?php echo $url[0]; ?></td>
    <td width="200"> <?php echo $sMonth[0] . "/" . $sDay[0] . "/" . $sYear[0]; ?></td>
    <td width="200"> <?php echo $eMonth[0] . "/" . $eDay[0] . "/" . $eYear[0]; ?></td>
    <td> <?php echo $tBudget[0]; ?></td>
    <td> <?php echo $dBudget[0]; ?></td>
    <td> <?php echo $model[0]; ?></td>
    <td> <?php echo $bid[0]; ?></td>
    <td> <?php echo $target[0]; ?></td>
    <td> <?php echo $status[0]; ?></td>
    <td width="200"> <?php echo $arr[0]; ?></td>
    <td> <?php echo $tUnits[0]; ?></td>
</tr>

NOTE = all other variables and values print just FINE (by using the same method and concept as the $arr variable) But ONLY the $arr variable is unable to print).

What am I doing wrong?

2
  • What does var_dump($arr); show? How about var_dump($_POST);? Commented Dec 17, 2013 at 19:58
  • var_dump($arr) says this: dasarray(1) { [0]=> string(6) "Mobile" } Commented Dec 17, 2013 at 20:05

3 Answers 3

2

Change

<select name="arr[]">
...
<td width="200"> <?php echo $arr[0]; ?></td>

to

<select name="arr">
...
<td width="200"> <?php echo $arr; ?></td>

You'll only want to use array naming when you have a multiple select option

Sign up to request clarification or add additional context in comments.

10 Comments

Can you elaborate on that further? it's not multiple. User selects ONE option, that option will be put into the array.
@user3108505 exactly, since you only passing ONE value, you don't need to name your select arr[] and you only read it in your php like this: $arr instead of arr[0] since it's not an array. Read my updated answer.
My other variables are implemented the same way using the same concept and implementation. They all print except for arr
@user3108505 is your select into the <form> tags of your form?
Yes it is. So is my other select tags that I have used for my other variables/arrays.
|
0

Actually, it's probably something like that:

   <select name="arr[]" multiple>
      <option value="Mobile">Mobile</option>
      <option value="Social">Social</option>
      <option value="Online">Online</option>
   </select>

1 Comment

but having "multiple" would cause all 3 values to be inserted. I don't want that. I want the selected option to be inserted into the first elemental position only.
0

u need to use for loop to get values in your array check this

<form action="<?php echo @$_SERVER['PHP_SELF'];?>" method="POST"> 
            <select name="arr[]" multiple>
                <option>Radio</option>
                <option>TV</option>
                <option>Keyboard</option>
                <option>DVD Player</option>
                <option>Screen</option>
            </select>
            <input type="submit" value="GO!">
        </form>
<?php
$data=@$_POST['arr'];
$len = count($data); // getting length of ur array that u need to condition ur loop
for($y=0;$y<$len;$y++){
    echo "$data[$y]"."<br />";
}
?>

1 Comment

if u wont use loop don't use multiple then , multiple let user choose many value that's why you make select arr[] as (array) , best way to print array values = looping , in you'r code you just print the first index of arr[0] lemme know why ?!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.