1

i have one array variable call : $array_variable

$array_variable_1 = array('1','2','3','4' etc ....);
$array_variable_2 = array('1','2','3','4' etc ....);
$array_variable_3 = array('1','2','3','4' etc ....);
$array_variable_4 = array('1','2','3','4' etc ....);
$array_variable_5 = array('1','2','3','4' etc ....);

i want to post this all data by selecting drop down like below:

<select name="fieldforpost">
 <option value="<?php $array_variable_1; ?>">id name 1</option>
 <option value="<?php $array_variable_2; ?>">id name 2</option>
 <option value="<?php $array_variable_3; ?>">id name 3</option>
 <option value="<?php $array_variable_4; ?>">id name 4</option>
 <option value="<?php $array_variable_5; ?>">id name 1</option>
</select>

and how to get this data in PHP file:

$output_array = $_POST['fieldforpost'];

final word: i can't post this data to my php file any buddy have idea how to do this operation?

asked Dec 3, 2018 at 6:08
3
  • 1
    <?php echo $array_variable_1; ?> put echo in value field Commented Dec 3, 2018 at 6:37
  • but need to post full array Commented Dec 4, 2018 at 13:40
  • <option value="<?php echo json_encode($array_variable_5); ?>">id name 1</option> got answer Commented Dec 4, 2018 at 13:41

3 Answers 3

1

Just put echo in values

<select name="fieldforpost">
 <option value="<?php echo $array_variable_1; ?>">id name 1</option>
 <option value="<?php echo $array_variable_2; ?>">id name 2</option>
 <option value="<?php echo $array_variable_3; ?>">id name 3</option>
 <option value="<?php echo $array_variable_4; ?>">id name 4</option>
 <option value="<?php echo $array_variable_5; ?>">id name 1</option>
</select>
answered Dec 3, 2018 at 6:41
1
  • This will not work. OP stores values in $array_variable not in $array_variable_1 and $array_variable_2 etc. He have to echo proper index of $array_variable not $array_variable_1. Commented Dec 3, 2018 at 8:13
0

In order to post form fields you need to set an action and post method for the form. For example, if you wanted to post the form to the same page you are on now you would use:

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
 <select name="fieldforpost">
 <option value="<?php $array_variable_1; ?>">id name 1</option>
 <option value="<?php $array_variable_2; ?>">id name 2</option>
 <option value="<?php $array_variable_3; ?>">id name 3</option>
 <option value="<?php $array_variable_4; ?>">id name 4</option>
 <option value="<?php $array_variable_5; ?>">id name 1</option>
 </select>
 <input type="submit" name="submit" value="Submit Form">
</form>

After submitting the form the page will reload and $output_array = $_POST['fieldforpost']; should work correctly (assuming a value was selected on the form).

Now, if you want to dynamically populate the options from the array you can do something like this:

<select name="fieldforpost">
 <?php 
 foreach($array_variable as $var) {
 echo "<option value=\"$var\">id name $var</option>";
 }
 ?>
</select>
answered Dec 3, 2018 at 6:49
2
  • Why are you setting the action if you want form sending the request to the same page? If you leave an action blank, form by default sends the request to the same page. Commented Dec 3, 2018 at 8:02
  • You are right, it is redundant. I have no idea what the rest of @hiren_sid's form looks like (if it exists at all) or if the intention was even to post back to the same page. In the provided code there was no action that would cause the field to post to $output_array (which is also not an array). The question had too many unknowns so my answer was a shot in the dark. Just trying to be helpful. Commented Dec 3, 2018 at 8:18
0

Your values are stored in array so you have to display it using array indexes.

<select name="fieldforpost">
 <option value="<?php echo $array_variable[0]; ?>">id name 1</option>
 <option value="<?php echo $array_variable[1]; ?>">id name 2</option>
 <option value="<?php echo $array_variable[2]; ?>">id name 3</option>
 <option value="<?php echo $array_variable[3]; ?>">id name 4</option>
 <option value="<?php echo $array_variable[4]; ?>">id name 1</option>
</select>

Remember that in PHP array indexes starts at 0 not 1. Or you can use the foreach loop:

<select name="fieldforpost">
 <?php foreach($array_variable as $variable) : ?>
 <option value="<?php echo $variable; ?>">id name <?php echo $var; ?></option>
 <?php endforeach; ?>
</select>

If you want to read the form data:

$output_array = $_POST['fieldforpost'];

Make sure that your form has method set to POST. If you leave the method blank, it will use GET.

answered Dec 3, 2018 at 8:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.