Hiren Siddhpura
Hiren Siddhpura

Reputation: 189

how to post array varibale in form data with post method

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?

Upvotes: 1

Views: 149

Answers (3)

Bartek Zawada
Bartek Zawada

Reputation: 111

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.

Upvotes: 0

tshimkus
tshimkus

Reputation: 1181

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>

Upvotes: 0

Vivek ab
Vivek ab

Reputation: 680

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>

Upvotes: 1

Related Questions