I am trying to make a form submit.
Form is select a task, then show several rows with checkbox and select with values 1-10
If checkbox is selected is need to add values from select into database.
this is PHP code
if(isset($_POST) && $_SERVER['REQUEST_METHOD'] = 'POST'){
if(isset($_POST['submita'])){
foreach($_POST['nota'] as $key => $value)
if (isset($_POST['boxes'])){
foreach($_POST['boxes'] as $key => $value2){
if(isset($value2)){
$nota = htmlent($_POST['nota']);
$box = htmlent($_POST['boxes']);
$task = htmlent($_POST['task']);
$db->insert(array(
"task" => $task,
"nota" => $value,
"box" => $value2,
),
"erp_notes");
}
else
{
echo 'Select';
}
}
}
}
}
HTML
<input type="checkbox" name="boxes[]" value="<?=$row['id'];?>"></input>
<?=$row['name'];?>
<select name="nota[]" >
<option value="">Select</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
I try many time but nothing.
In the very first if you have = when you mean ==.
You haven't posted the entire HTML so I'll have to assume there is a submita element there that's being set. Your best bet is to do var_dump($_POST) to see what you are actually getting, and then build your code to match.
Your code is incorrect because it loops over every boxes for each nota, when what I suspect you want is to just check the checkbox that corresponds to that element.
You probably want something more like this:
if (isset($_POST['nota']))
{
for ($i = 0; $i < count($_POST['nota']); ++$i)
{
if (isset($_POST['boxes'][$i]))
$db->insert(array('task' => $task, 'nota' => $_POST['nota'][$i], 'box' => $_POST['boxes'][$i], 'erp_notes');
}
}
i was edited cod , but work like in comment from first answer
PHP
if(isset($_POST) && $_SERVER['REQUEST_METHOD'] == 'POST'){
if (isset($_POST['nota']))
{
for ($i = 0; $i < count($_POST['nota']); ++$i)
{
if (isset($_POST['boxes'][$i]))
$task = htmlent($_POST['task_id']);
$db->insert(array(
"nota" => $_POST['nota'][$i],
"date" => time(),
"box" => $_POST['boxes'][$i],
"task" => $task
),
"erp_note");
}
}
}
HTML
<form class="grid_12" action="" method="post" enctype="multipart/form-data">
<input type="checkbox" name="boxes[]" value="<?=$row['id'];?>"></input><?=$row['box_name'];?>
<select name="nota[]" multiple><option value="">select</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select>
<input name="task" type="text" value="<?=$row['task_id'];?>" style="display:none;">
</form>
Related
I am new in PHP.
Please check the following code; multiple select is not working. Not sure why. Thanks in advance.
<form action="test.php">
<select name="fin[]" multiple ="multiple" class = "multiple_select" id = "selected_options_fin" style="height: 200px">
<option value="week_period">Week Period</option>
<option value="comments_approved">Comments Approved</option>
<option value="comment_replies">Comment Replies</option>
<option value="avarage_response_time">Average Response Time</option>
<option value="avarage_response_time_in_minutes">Average Response Time in Minutes</option>
<option value="commenter_visits">Commenter Visits</option>
<option value="percentage_of_new_visits">% New Visits</option>
<option value="number_of_goal_completions"># of Goal Completions</option>
<option value="conversions_rate">Conversions Rate</option>
</select>
<input type="submit" name = "submit1" Value = "SUBMIT" id="submit_fin">
</form>
<?php
if (isset($_POST['fin'])) {
$fin_array = $_POST['fin'];
$loop_count = 0;
foreach ($fin_array as $key => $value) {
$loop_count++;
echo 'Column $loop_count || Array Key = $key || Value = $value<br />';
}
exit();
}
Sorry for bothering
First line need to have
form action="test.php" method="post"
I have this following codes.How do i send this form to php so in php i can access it like studentName course2 year3.Basically i have a table which looks like this studentName|course|year.I am looking for the right way to organize the data in order to insert it to my table.I have tried sending the form not as an array but i have to write 3 queries to insert 3 inputs.Is there any way to loop through the data and insert?
<form id="myform" method="POST">
<label>college:</label><input type="text" id="clg" name="clg"/><br/>
<label>Student:-</label><input type="text" name="name[]" id="name" />
<select name="taskOpen[]">
<option value="1">course1</option>
<option value="2">course2</option>
<option value="3">course3</option>
<option value="4">course4</option>
</select>
<select name="year[]">
<option value="1">year1</option>
<option value="2">year2</option>
<option value="3">year3</option>
<option value="4">year4</option>
</select>
<label>Student:-</label><input type="text" name="name[]" id="name" />
<select name="taskOpen[]">
<option value="1">course1</option>
<option value="2">course2</option>
<option value="3">course3</option>
<option value="4">course4</option>
</select>
<select name="year[]">
<option value="1">year1</option>
<option value="2">year2</option>
<option value="3">year3</option>
<option value="4">year4</option>
</select>
<input type="button" value="search" id="btn"/>
</form>
Here is what i have in php--
if(isset($_POST['name']) && isset($_POST['taskOpen'])
&& isset($_POST(['year'])){
$name=$_POST['name'],$course=$_POST['taskOpen'],$year=$_POST['year']
}
else {
echo "something";
}
foreach($name as $student){
$sql=$conn->prepare("INSERT INTO `student`(`studentName`) VALUES(:s_n)");
$sql->execute(array(":s_n"=>$student));
}
foreach($course as $courseN){
$sql=$conn->prepare("INSERT INTO `student`(`course`) VALUES(:co)");
$sql->execute(array(":co"=>$courseN));
}
foreach($year as $yearD){
$sql=$conn->prepare("INSERT INTO `student`(`year`) VALUES(:ya)");
$sql->execute(array(":ya"=>$yearD));
}
Here is what i think you are going for...
HTML
<!-- THE NAMES OF INPUT ELEMENTS WRAPPED IN A college_data-->
<form id="myform" method="POST">
<label>college:</label><input type="text" id="clg" name="college_data[clg]"/><br/>
<label>Student:-</label><input type="text" name="college_data[name]" id="name"/>
<select name="college_data[taskOpen1]">
<option value="1">course1</option>
<option value="2">course2</option>
<option value="3">course3</option>
<option value="4">course4</option>
</select>
<select name="college_data[year1]">
<option value="1">year1</option>
<option value="2">year2</option>
<option value="3">year3</option>
<option value="4">year4</option>
</select>
<label>Student:-</label><input type="text" name="name[]" id="name"/>
<select name="college_data[taskOpen2]">
<option value="1">course1</option>
<option value="2">course2</option>
<option value="3">course3</option>
<option value="4">course4</option>
</select>
<select name="college_data[year2]">
<option value="1">year1</option>
<option value="2">year2</option>
<option value="3">year3</option>
<option value="4">year4</option>
</select>
<input type="submit" value="search" id="btn"/>
</form>
PHP
<?php
$college = isset($_POST['college_data']['clg']) ? htmlspecialchars(trim($_POST['college_data']['clg'])) :null;
$studentName = isset($_POST['college_data']['name']) ? htmlspecialchars(trim($_POST['college_data']['name'])) :null;
$studentName2 = isset($_POST['college_data']['name2']) ? htmlspecialchars(trim($_POST['college_data']['name2'])) :null;
$taskOpen1 = isset($_POST['college_data']['taskOpen1']) ? htmlspecialchars(trim($_POST['college_data']['taskOpen1'])) :null;
$taskOpen2 = isset($_POST['college_data']['taskOpen2']) ? htmlspecialchars(trim($_POST['college_data']['taskOpen2'])) :null;
$year1 = isset($_POST['college_data']['year1']) ? htmlspecialchars(trim($_POST['college_data']['year1'])) :null;
$year2 = isset($_POST['college_data']['year2']) ? htmlspecialchars(trim($_POST['college_data']['year2'])) :null;
// TEST::: DELETE EVERYTHING BELOW THIS LINE ONCE YOU ARE DONE CONFIRMING THAT IT IS OK
// CLICK THE SUBMIT BUTTON TO SEE THE VALUES REFLECTED HERE AS SOON AS THE FORM GETS SUBMITTED...
var_dump($college);
var_dump($studentName);
var_dump($studentName2);
var_dump($taskOpen1);
var_dump($taskOpen2);
var_dump($year1);
var_dump($year2);
?>
VALIDATION
<?php
// FIRST CREATE AN ARRAY TO HOLD MESSAGES TO BE SHOWN TO THE USER
// SHOULD ANYTHING GO WRONG... & ALSO AN EMPTY ERROR MESSAGE STRING...
$arrErrorBag = array();
$strErrorMsg = "";
if(isset($_POST['submit'])) {
// LET'S ASSUME FOR THE MOMENT YOU HAVE SPECIAL CONSTRAINTS
// TO BE APPLIED TO EACH FIELD DURING YOUR VALIDATION PROCESS.
// WE WILL USE SIMPLE REGULAR EXPRESSIONS HERE FOR DEMONSTRATION
// BUILD VALIDATION REG-EXES FOR THE FIELD:: WE WANT ONLY A MINIMUM OF 10 CHARACTERS FOR STUDENT & COLLEGE
// AGAIN; YOU CAN GO WILD BUT WE ARE KEEPING IT SIMPLE
// PLUS, DOING IT LIKE THIS IS JUST SO THAT YOU GET THE DRIFT... ;-)
$rxColStudent = "#(.){10,}#si";
// FOR THE SELECTS, WE KNOW THAT THE VALUES ARE NUMBERS
// WHICH ARE BETWEEN 1 & 4; BUT WE TAKE IT TO "BETWEEN 1 & 9"
// AND WE INTEND USING ONE REGEX FOR ALL OF THEM SINCE THEY ARE SIMILAR.
$rxCourseYear = "#\d#";
// NOW WE HAVE THE REGEXES: THEN WHAT?
// VALIDATION BEGINS... ;-)
// TO MAKE IT EASIER ON OURSELVES, WE BUNDLE ALL THE STUDENT, STUDENT1 & COLLEGE FIELDS
// INTO AN ARRAY & LOOP THROUGH THE ARRAY, BUILDING THE ERRORS ON EACH ITERATION THROUGH THE LOOP.
// VALIDATE COLLEGE, STUDENT & STUDENT2...
$arrStringValFields = array(
"student" => array("Student Nr. 1", $studentName),
"student2" => array("Student Nr. 2 ", $studentName2),
"college" => array("College", $college),
);
// RUN THE LOOP & KEEP BUILDING THE ERRORS AS YOU VALIDATE THROUGH EACH ITERATION.
foreach ($arrStringValFields as $fieldName => $arrErrStringVal) {
if (!preg_match($rxColStudent, $arrErrStringVal[1])) {
$arrErrorBag[$fieldName] = $arrErrStringVal[0] . " is expected to have a minimum of 10 Characters. Please, check that you have not missed something.";
}
}
// TO MAKE IT AGAIN EASIER ON OURSELVES, WE BUNDLE ALL OTHER FIELDS THAT HAVE NUMERIC VALUES
// INTO AN ARRAY & LOOP THROUGH THE ARRAY, BUILDING THE ERRORS ON EACH ITERATION THROUGH THE LOOP.
$arrNumericFields = array(
"taskOpen1" => array("Open Task Nr. 1", $taskOpen1),
"taskOpen2" => array("Open Task Nr. 2 ", $taskOpen2),
"year1" => array("College Year Section 1", $year1),
"year2" => array("College Year Section 2", $year2),
);
// RUN THE LOOP & KEEP BUILDING THE ERRORS AS YOU VALIDATE THROUGH EACH ITERATION.
foreach ($arrNumericFields as $fieldName => $arrErrStringVal) {
if (!preg_match($rxCourseYear, $arrErrStringVal[1])) {
$arrErrorBag[$fieldName] = $arrErrStringVal[0] . " is expected to be a Single Digit & it should. Did you try to *NUKE OUR ASSES OFF*. Please, don't. We are your Brothers ;-)";
}
}
// MAMA MIA!!! SO MUCH FOR A SIMPLE 6-FIELD VALIDATION...
// HAPPILY ENOUGH; WE ARE THERE... ONLY; NOT JUST YET...
// CHECK THE ERROR BAG TO SEE IF IT CONTAINS ANYTHING.
// IF IT DOES; THEN WE HAVE TO FIND A WAY TO DISPLAY THIS TO THE END-USER.
if (!empty($arrErrorBag)) {
// TURN THE ERROR BAG ARRAY TO A STRING & ASSIGN IT BACK TO THE ERROR MESSAGE STRING FOR DISPLAY...
$strErrorMsg = "<span class='has-error'>" . implode("</span><br /><span class='has-error'>", $arrErrorBag);
}
else {
// WE HAVE REACHED THE CLIMAX OF OUR POLICE-WORK...
// SO WE EITHER STORE THE DATA TO THE DATABASE TABLE OR
// WE BAKE & CAKE IT FOR CHRISTMAS - YOUR CALL ;-)
}
}
?>
Now, that's OK. But how does the User get to know that there were Errors and how does s/he avoid typing-in the same information over again?
Our HTML File should have been built earlier to take that into consideration, but then again, EINTEIN WAS RIGHT!!! Human Stupidity is totally as Infinite as the Universe... but that's why we are here, right? To cut down the infinitude of our Stupidity and grow into real Humans;-) So we revise our HTML to take these into account.
HTML - REVISED: UNLEASH THE DRAGON!!!
<!-- WE ADD A SLOT FOR OUR ERROR MESSAGE: JUST BEFORE THE FORM. -->
<div class="error-slot"><?php echo $strErrorMsg; ?></div>
<form id="myform" method="POST">
<label>college:</label><input type="text" id="clg" name="college_data[clg]" value="<?php echo $college; ?>" /><br/>
<label>Student:-</label><input type="text" name="college_data[name]" value="<?php echo $studentName; ?>" id="name"/>
<select name="college_data[taskOpen1]">
<option value="1" <?php if($taskOpen1=="1"){echo "selected";} ?>>course1</option>
<option value="2" <?php if($taskOpen1=="2"){echo "selected";} ?>>course2</option>
<option value="3" <?php if($taskOpen1=="3"){echo "selected";} ?>>course3</option>
<option value="4" <?php if($taskOpen1=="4"){echo "selected";} ?>>course4</option>
</select>
<select name="college_data[year1]">
<option value="1" <?php if($year1=="1"){echo "selected";} ?>>year1</option>
<option value="2" <?php if($year1=="2"){echo "selected";} ?>>year2</option>
<option value="3" <?php if($year1=="3"){echo "selected";} ?>>year3</option>
<option value="4" <?php if($year1=="4"){echo "selected";} ?>>year4</option>
</select>
<label>Student:-</label><input type="text" name="college_data[name2]" value="<?php echo $studentName2; ?>" id="name"/>
<select name="college_data[taskOpen2]">
<option value="1" <?php if($taskOpen2=="1"){echo "selected";} ?>>course1</option>
<option value="2" <?php if($taskOpen2=="2"){echo "selected";} ?>>course2</option>
<option value="3" <?php if($taskOpen2=="3"){echo "selected";} ?>>course3</option>
<option value="4" <?php if($taskOpen2=="4"){echo "selected";} ?>>course4</option>
</select>
<select name="college_data[year2]">
<option value="1" <?php if($year2=="1"){echo "selected";} ?>>year1</option>
<option value="2" <?php if($year2=="2"){echo "selected";} ?>>year2</option>
<option value="3" <?php if($year2=="3"){echo "selected";} ?>>year3</option>
<option value="4" <?php if($year2=="4"){echo "selected";} ?>>year4</option>
</select>
<input type="submit" value="search" name="submit" id="btn"/>
</form>
From your table design in the database it looks like you want to insert a student, their course and the year they are taking it in. Unless you are going to validate the form so that the number of students matches the number of courses and numbers of years so you can easily loop and insert based on their indexes, i don't see how it can be easily done.
Example:
$_POST = Array(
'name' = Array ( 'Kelly', 'Tom', 'Jack' ),
'course' = Array ( 'Course1', 'Course2', 'Course3' ),
'year' = Array ( '2007', '2008', '2009' ),
);
for ( $i = 0; i < count($_POST['student_name']); i++ ) {
$sql = sprintf("INSERT INTO test ( student_name, course, year ) VALUES ( %s, %s, %d )",
$_POST['student_name'][$i], $_POST['course'][$i], $_POST['year'][$i]
);
$conn->query($sql);
}
<form action="a.php" method="post">
<select id="sel_1" name="sel[]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="sel_2" name="sel[]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit"/>
</form>
Now when i am trying to fecth the data like this
$offer = $_POST['sel'];
print_r($offer);
its displaying data like this:
Array
(
[0] => 1 // 1, 2 selected for sel_1
[1] => 2
[2] => 2 // 2, 3 selected for sel_2
[3] => 3
)
Shouldn't it come like this?
Array
(
[0] => Array(
[0] => 1
[2] => 2
)
[1] => Array(
[0] => 2
[2] => 3
)
)
I want to create string data like this(in the nxt a.php file):
for sel_1 data is created like "1, 2";
for sel_2 data is created like "2, 3";
How can i fetch the data in the above format.
I am trying this
for($i = 0; $i<count($offer) ; $i++)
{
for($j = 0; $j<count($offer[$i]); $j++)
{
$string = $tring. $offer[$i][$j];
}
}
Set the names sel1[] and sel2[] (different). In PHP you can use array_merge to obtain another array with the values from the first and from the second array:
$offer = array_merge($_POST['sel1'], $_POST['sel2']);
$string = '';
for($i = 0; $i < count($offer); $i++)
{
$string .= $offer[$i];
}
Try like this,it's working as per your requirement:
Instead of this
$offer = $_POST['sel'];
Put like this
$offer[] = $_POST['sel'];
Code:-
<form action="" method="POST">
<select id="sel_1" name="sel1[]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="sel_2" name="sel2[]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit"/>
</form>
<?php
error_reporting(0);
$offer=array();
$offer[] = $_POST['sel1'];
$offer[]= $_POST['sel2'];
echo "<pre>";
print_r($offer);
echo "</pre>";
?>
For output click here: Output
Try this:
<form action="a.php" method="post">
<select id="sel_1" name="sel[1][]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="sel_2" name="sel[2][]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit"/>
</form>
Worked for me
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test[]" multiple="multiple">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
<option value="four">four</option>
<option value="five">five</option>
</select>
<input type="submit" value="Send" />
</form>
<?php
$test=$_POST['test'];
if ($test){
foreach ($test as $t){echo 'You selected ',$t,'<br />';}
}
if($t=='one')
$price1=12;
if($t=='two')
$price2=2;
$total = $price1 + $price2;
echo $total;
When one & two are both selected i'd like the result to be 14. What is the best way to make an addition to obtain 14 as a result ?
You should make one variable that will contain the result, instead of making price1, price2 and etc.
For example:
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test[]" multiple="multiple">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
<option value="four">four</option>
<option value="five">five</option>
</select>
<input type="submit" value="Send" />
</form>
<?php
$test = isset($_POST['test']) ? $_POST['test'] : null; // we also want to check if it's set else we'll get an notice
$total = 0;
if ($test) {
foreach ($test as $t) {
echo 'You selected ',$t,'<br />';
switch ($t) {
case "one" : $total += 2; break;
case "two" : $total += 12; break;
}
}
}
echo 'Total: ' .$total;
put that IF's into foreach loop
You must place the if $t = one , etc. inside the loop
Firstly, $test should be assigned using a ternary operator, that way we can make sure we don't cause PHP Warnings.
$test = isset($_POST['test']) ? $_POST['test'] : false;
if($test):
//good
endif;
Secondly, why even bother using string interpretation of an integer?
<option value="1">one</option> //note the values
<option value="2">two</option>
Lets make an array that has our answers.
$vals = array(
0 => 'Not an option!',
1 => 12,
2 => 2,
3 => 14,
4 => 26
);
Create a dummy variable we can use to concatenate the values posted.
$val = 0;
foreach($test as $t):
$val += $t;
endforeach;
Now $t will be the value of all the posted select items.
echo $vals[$val];
And that simple echo statement will supply you with your answer. This way you're not having to do multiple if/else statements, keeps it cleaner and more robust imo.
and here's a Codepad illustrating this
This question already has answers here:
Using $_POST to get select option value from HTML
(8 answers)
Closed 6 years ago.
I am trying to get the option selected using PHP, but I ran out of ideas!
Below is the code I have tried until now:
<select>
<option value="1">Yes</options>
<option value="2">No</options>
<option value="3">Fine</options>
</select>
<input type="text" value="" name="name">
<input type="submit" value="go" name="go">
So, what do I have to do?
Programmers are lazy...er....efficient....I'd do it like so:
<select><?php
$the_key = 1; // or whatever you want
foreach(array(
1 => 'Yes',
2 => 'No',
3 => 'Fine',
) as $key => $val){
?><option value="<?php echo $key; ?>"<?php
if($key==$the_key)echo ' selected="selected"';
?>><?php echo $val; ?></option><?php
}
?></select>
<input type="text" value="" name="name">
<input type="submit" value="go" name="go">
<select>
<option value="1" <?php if ($myVar==1) echo 'selected="selected"';?>>Yes</options>
<option value="2" <?php if ($myVar==2) echo 'selected="selected"';?>>No</options>
<option value="3" <?php if ($myVar==3) echo 'selected="selected"';?>>Fine</options>
</select>
<input type="text" value="" name="name">
<input type="submit" value="go" name="go">
This is a very simple and straightforward way, if I understand your question correctly.
you can use this..
<select name="select_name">
<option value="1"<?php echo(isset($_POST['select_name'])&&($_POST['select_name']=='1')?' selected="selected"':'');?>>Yes</option>
<option value="2"<?php echo(isset($_POST['select_name'])&&($_POST['select_name']=='2')?' selected="selected"':'');?>>No</option>
<option value="3"<?php echo(isset($_POST['select_name'])&&($_POST['select_name']=='3')?' selected="selected"':'');?>>Fine</option>
</select>
First of all give a name to your select. Then do:
<select name="my_select">
<option value="1" <?= ($_POST['my_select'] == "1")? "selected":"";?>>Yes</options>
<option value="2" <?= ($_POST['my_select'] == "2")? "selected":"";?>>No</options>
<option value="3" <?= ($_POST['my_select'] == "3")? "selected":"";?>>Fine</options>
</select>
What that does is check if what was selected is the same for each and when its found echo "selected".
I suppose that you are using an array to create your select form input.
In that case, use an array:
<?php
$selected = array( $_REQUEST['yesnofine'] => 'selected="selected"' );
$fields = array(1 => 'Yes', 2 => 'No', 3 => 'Fine');
?>
<select name=‘yesnofine'>
<?php foreach ($fields as $k => $v): ?>
<option value="<?php echo $k;?>" <?php #print($selected[$k]);?>><?php echo $v;?></options>
<?php endforeach; ?>
</select>
If not, you may just unroll the above loop, and still use an array.
<option value="1" <?php #print($selected[$k]);?>>Yes</options>
<option value="2" <?php #print($selected[$k]);?>>No</options>
<option value="3" <?php #print($selected[$k]);?>>Fine</options>
Notes that I don't know:
how you are naming your input, so I made up a name for it.
which way you are handling your form input on server side, I used $_REQUEST,
You will have to adapt the code to match requirements of the framework you are using, if any.
Also, it is customary in many frameworks to use the alternative syntax in view dedicated scripts.
I use inline if's
($_POST['category'] == $data['id'] ? 'selected="selected"' : false)
I have 2 php files and i made this, and it works.
(this is an example) the first code is from the one file and the second code from two file.
<form action="two.php" method="post">
<input type="submit" class="button" value="submit" name="one"/>
<select name="numbers">
<option value="1"> 1 </option>
<option value="2"> 2 </option>
<option value="3"> 3 </option>
</select>
</form>
if(isset ($_POST['one']))
{
if($_POST['numbers']=='1')
{
$a='1' ;
}
else if($_POST['numbers']=='2')
{
$a='2' ;
{
else if ($_POST['numbers']=='3')
{
$a='3' ;
}
}
This answer is not relevant for particular recepient, but maybe useful for others.
I had similiar issue with 'selecting' right 'option' by value returned from database.
I solved it by adding additional tag with applied display:none.
<?php
$status = "NOT_ON_LIST";
$text = "<html>
<head>
</head>
<body>
<select id=\"statuses\">
<option value=\"status\" selected=\"selected\" style=\"display:none\">$status</option>
<option value=\"status\">OK</option>
<option value=\"status\">DOWN</option>
<option value=\"status\">UNKNOWN</option>
</select>
</body>
</html>";
print $text;
?>
This is the solution that I've came up with:
<form name = "form1" id = "form1" action = "#" method = "post">
<select name = "DropDownList1" id = "DropDownList1">
<?php
$arr = array('Yes', 'No', 'Fine' ); // create array so looping is easier
for( $i = 1; $i <= 3; $i++ ) // loop starts at first value and ends at last value
{
$selected = ''; // keep selected at nothing
if( isset( $_POST['go'] ) ) // check if form was submitted
{
if( $_POST['DropDownList1'] == $i ) // if the value of the dropdownlist is equal to the looped variable
{
$selected = 'selected = "selected"'; // if is equal, set selected = "selected"
}
}
// note: if value is not equal, selected stays defaulted to nothing as explained earlier
echo '<option value = "' . $i . '"' . $selected . '>' . $arr[$i] . '</option>'; // echo the option element to the page using the $selected variable
}
?>
</select> <!-- finish the form in html -->
<input type="text" value="" name="name">
<input type="submit" value="go" name="go">
</form>
The code I have works as long as the values are integers in some numeric order ( ascending or descending ). What it does is starts the dropdownlist in html, and adds each option element in php code. It will not work if you have random values though, i.e: 1, 4, 2, 7, 6. Each value must be unique.