how to post array varibale in form data with post method - php

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?

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>

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>

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.

Related

How do i get the select tag value with codeigniter?

how do i get the select tag value, i am using the syntax below but not working.
Controller
'role' => $this->input->post('rol'));
View
these are the options in my select tag
<select name="rol">
<option value="Employee">Employee</option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
$role = $this->input->post("rol");
Just try it to post like below:
<form action="your_controller_action" method="POST">
<select name="rol" id="pos_select" class="form_input">
<option value="Employee">Employee </option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
<input type="submit" name="submit" value="Post it">
</form>
Then in your controller:
public function youControllerAction() {
if( $this->input->post) {
echo $this->input->post("rol");
}
}
You will get the selected option value.
how did you write your selection dropdown list!did you take it as i posted below?
<select name="rol" id="pos_select" class="form_input">
<option value="Employee">Employee </option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
$role = $this->input->post("rol");
put this code in view. Only you have to call controller function in action. In this example I am getting option values from database
<form method="post" action="<?php echo base_url()."main/project_filter_skills"; ?>">
<select class="demo-default " id="skilltags" name="skilltags[]" data-placeholder="Choose skills">
<option value="">Choose skills</option>
<?php foreach ($all_skill_tags as $skilltags) { ?>
<option value="<?php echo $skilltags->secondCat_id;?>">
<?php echo $skilltags->secondCat_title;?>
</option>
<?php } ?>
</select>
</form>
now put this in modal or controller to get the vlaue
$skilltags= $this->input->post('skilltags');

php form several select with same name only the one selected

I have a form with several <select> that have the same name and an onChange="document.forms['form_mes_reservations'].submit();" on all of them. Therefore, when I select an option from one of them, the page reloads and there is some PHP treatment done.
This is an example :
<form method="post" id="form_process">
<select name="processed" onChange="document.forms['form_process'].submit();">
<option value="1_choose">Please choose...</option>
<option value="1_yes">Yes</option>
<option value="1_no"></option>
</select>
<select name="processed" onChange="document.forms['form_process'].submit();">
<option value="2_choose">Please choose...</option>
<option value="2_yes">Yes</option>
<option value="2_no"></option>
</select>
<select name="processed" onChange="document.forms['form_process'].submit();">
<option value="3_choose">Please choose...</option>
<option value="3_yes">Yes</option>
<option value="3_no"></option>
</select>
<select name="processed" onChange="document.forms['form_process'].submit();">
<option value="4_choose">Please choose...</option>
<option value="4_yes">Yes</option>
<option value="4_no"></option>
</select>
</form>
What I'd like to know is this : is there a way to know which select has triggered the onChange ? Let me explain : in the $_POST['processed'], I get ALL the selected options, so if I have only selected "3_yes", I'll get something like this :
1_choose
2_choose
3_yes
4_choose
Is it possible to only have "3_yes" as a result ? Potentially, this is going to be a very long list of selects and I don't want to check all of them for changes...
Just make all select elements as array and check which is selected:
HTML:
<form method="post" id="form_process" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="processed[]" onChange="document.forms['form_process'].submit();">
<option value="1_choose">Please choose...</option>
<option value="1_yes">Yes</option>
<option value="1_no"></option>
</select>
<select name="processed[]" onChange="document.forms['form_process'].submit();">
<option value="2_choose">Please choose...</option>
<option value="2_yes">Yes</option>
<option value="2_no"></option>
</select>
<select name="processed[]" onChange="document.forms['form_process'].submit();">
<option value="3_choose">Please choose...</option>
<option value="3_yes">Yes</option>
<option value="3_no"></option>
</select>
<select name="processed[]" onChange="document.forms['form_process'].submit();">
<option value="4_choose">Please choose...</option>
<option value="4_yes">Yes</option>
<option value="4_no"></option>
</select>
</form>
PHP:
<?php
foreach($_POST['processed'] AS $key => $value){
if(strpos($value, 'choose') !== false){
unset($_POST[processed][$key]);
}
}
print_r($_POST['processed']);
?>
Output:
Array
(
[1] => 2_yes
)

Get select value in the same page

I have this select button:
<form method="post">
<label>
<select id="label" name="challengeX" style="margin-top:150px;">
<option selected value="0"> Global Statistics </option>
<option value="1">Challenge 1</option>
<option value="2">Challenge 2</option>
<option value="3">Challenge 3</option>
</select>
</label>
</form>
In the same page, few lines bellow, I have:
<div id="challenge">
<?php
$Ch = $_POST['challengeX'];
echo "Challenge: ".$Ch;
showSomething($Ch,1,1);
?>
</div>
But when I change the option, it didn't change the post value.
$Ch = $_POST['challengeX'];
won't get value until you submit the form. Add a submit button to your form.
<form method="post" action="">
<label>
<select id="label" name="challengeX" style="margin-top:150px;">
<option selected value="0"> Global Statistics </option>
<option value="1">Challenge 1</option>
<option value="2">Challenge 2</option>
<option value="3">Challenge 3</option>
</select>
</label>
<input type="submit" value="Submit">
</form>
And access the form values in the same page only if the form is submitted.
<div id="challenge">
<?php
if(isset($_POST)) // checks whether any value is posted
{
$Ch = $_POST['challengeX'];
echo "Challenge: ".$Ch;
showSomething($Ch,1,1);
}
?>
</div>
you need to submit form on change of dropdown.
Change html like this:
<form method="post">
<label>
<select id="label" name="challengeX" style="margin-top:150px;" onchange="this.form.submit()">
<option selected value="0"> Global Statistics </option>
<option value="1">Challenge 1</option>
<option value="2">Challenge 2</option>
<option value="3">Challenge 3</option>
</select>
</label>
</form>
for your php execution you can try like this:
<div id="challenge">
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$Ch = $_POST['challengeX'];
echo "Challenge: ".$Ch;
showSomething($Ch,1,1);
function showSomething($Ch,1,1){
//do your stuff
}
}
?>
</div>
PHP runs on the server and delivers HTML (or other data) to the browser.
If you want to process the form data with PHP then you must include it in a new HTTP request (typically by submitting the form) and have PHP generate a new page using that data.
Alternatively, if you want to process the data on the client then you will need to use a programming language that is supported in the browser. Unless you want to start using plugins (or browser specific technologies), that limits you to JavaScript. You can bind a change event listener to the select element and then manipulate the DOM with the results.
In the way you can use Javascript. If you dont want to use any server like PHP or something
<form method="post">
<label>
<select id="label" name="challengeX" style="margin-top:150px;">
<option selected value="0"> Global Statistics </option>
<option value="1">Challenge 1</option>
<option value="2">Challenge 2</option>
<option value="3">Challenge 3</option>
</select>
<input type="submit" /> /* add this line */
</label>
</form>
By Javascript
<select id="label" name="challengeX" style="margin-top:150px;" onChange="getSelect(this)">
<option selected value="0"> Global Statistics </option>
<option value="1">Challenge 1</option>
<option value="2">Challenge 2</option>
<option value="3">Challenge 3</option>
</select>
<div id="resultSelect"></div>
<script>
function getSelect(thisValue){
document.getElementById('resultSelect').innerHTML=thisValue.options[thisValue.selectedIndex].text;
}
</script>

POST Variables on a FORM PHP

I'm posting variables from my form on to another form, all the variables that are being posted on an INPUT field are showing fine. When a variable is needed to post on a SELECT how can I do this?
I tried within the <SELECT> tags to use value="<?php echo $myvariable; ?>" but this returns a blank.
To set the selected option within a <select> tag, use the selected attribute. In this example, "Value 3" is selected:
<select name="myvariable">
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3" selected>Value 3</option>
</select>
We can use PHP with something like this:
<select name="myvariable">
<option value="1" <?php echo ($myvariable == '1' ? 'selected' : '')?>>Value 1</option>
<option value="2" <?php echo ($myvariable == '2' ? 'selected' : '')?>>Value 2</option>
<option value="3" <?php echo ($myvariable == '3' ? 'selected' : '')?>>Value 3</option>
</select>
Hope this helps :) x
Your usage is incorrect. Check select documentation over here.
Example usage
<!-- The second value will be selected initially -->
<select name="select">
<option value="value1">Value 1</option>
<option value="value2" selected>Value 2</option>
<option value="value3">Value 3</option>
</select>
So for your case you should say
<select name="select">
<option value="value1"><?php echo $myvariable; ?></option>
</select>

how to get the value of the dropdown jquery

I want to get the value of the dropdown. I am using msDropDown, but i am getting its value as undefined. The html as follows:
<select name="category[]"
id="webmenus_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>"
onchange="showValue(this.value)"
>
<option value="0" selected="selected" title="Please select hotel category"></option>
<option value="5_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>" title="/public/front_end/images/5star.png"></option>
<option value="4_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>" title="/public/front_end/images/4star.png"></option>
<option value="3_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>" title="/public/front_end/images/3star.png"></option>
<option value="2_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>" title="/public/front_end/images/2star.png"></option>
<option value="1_<?php echo $BPackageCityRelatedToCountry[$i]['city_id']; ?>" title="/public/front_end/images/1star.png"></option>
</select>
jquery alert($("input[name='category[]']").val()); alerts as undefined.
How can i get the value of the dropdown?
Thanks,
$(":input[name='category[]']").val()
You use select instead of input
Edit your HTML and set the select Tag to mutiple
<select name="test" multiple>
<option value="1">ITEM 1</option>
<option value="2">ITEM 2</option>
<option value="3">ITEM 3</option>
</select>
alert($('select[name="test"]').val());
// outputs 1,2,3 when you select all three options
try
$("class of select ").change(function() {
alert($(this).val());
});

Categories