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?
-
1<?php echo $array_variable_1; ?> put echo in value fieldVivek ab– Vivek ab2018年12月03日 06:37:42 +00:00Commented Dec 3, 2018 at 6:37
-
but need to post full arrayHiren Siddhpura– Hiren Siddhpura2018年12月04日 13:40:51 +00:00Commented Dec 4, 2018 at 13:40
-
<option value="<?php echo json_encode($array_variable_5); ?>">id name 1</option> got answerHiren Siddhpura– Hiren Siddhpura2018年12月04日 13:41:21 +00:00Commented Dec 4, 2018 at 13:41
3 Answers 3
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>
-
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.Bartek Zawada– Bartek Zawada2018年12月03日 08:13:35 +00:00Commented Dec 3, 2018 at 8:13
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>
-
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.Bartek Zawada– Bartek Zawada2018年12月03日 08:02:53 +00:00Commented 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.tshimkus– tshimkus2018年12月03日 08:18:04 +00:00Commented Dec 3, 2018 at 8:18
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.