Viewed   57 times

I use select as below:

<select name="taskOption">
    <option>First</option>
    <option>Second</option>
    <option>Third</option>
</select>

How do I get the value from the select option and store it into a variable for future use, in PHP?

 Answers

5

Use this way:

$selectOption = $_POST['taskOption'];

But it is always better to give values to your <option> tags.

<select name="taskOption">
  <option value="1">First</option>
  <option value="2">Second</option>
  <option value="3">Third</option>
</select>
Sunday, December 18, 2022
1

First of all, the correct form in html is what you have for #22 (no quotes).

Secondly, the entire point of doing this is because it will convert it into an array. When this form is posted, an array is created INSIDE of $_POST called login. To access it, try this:

echo $_POST['login']['username']; //echos username
echo $_POST['login']['password']; //echos password

Here's a quick overview of how the nesting looks:

'_POST' =>
    array
      'login' => 
        array
          'username' => string 'myusername' (length=10)
          'password' => string 'mysecretpassword' (length=16)

Try doing this to get a good idea of what's going on and get output like above:

echo "<pre>";
var_dump($_POST);
echo "</pre>";

You'll be able to see all the nesting.

Monday, December 19, 2022
 
nneko
 
4

There is a way to get the value from different options. check this plunker

component.html

 <select class="form-control" #t (change)="callType(t.value)">
      <option *ngFor="#type of types" [value]="type">{{type}}</option>
    </select>

component.ts

this.types = [ 'type1', 'type2', 'type3' ];
   this.order = {
      type: 'type1'          
  };  

  callType(value){
    console.log(value);
    this.order.type=value;
  }
Tuesday, December 27, 2022
 
derick
 
3

Use a DOM parser like SimpleXML to split the HTML code into nodes, and walk through the nodes to build the array.

For broken/invalid HTML, SimpleHTMLDOM is more lenient (but it's not built in).

Thursday, August 4, 2022
 
2

For an easy way you can Make following changes to your code:

HTML:

<form action='' method='' id="myform">
Name:<input type='text' name='name' id='name'>
Age:<input type='text' name='age' id='age'>
message:<textarea name='message' id='message'></textarea>
<input type='submit' value='Send'>
</form>

PHP:

(your_page_with_php_script)

<?php
if(isset($_POST['name']) && isset($_POST['age']) && isset($_POST['message'])){

$a = $_POST['name'];
$b = $_POST['age'];
$c = $_POST['message'];

   if($a != NULL && $b != NULL && $c != NULL)
   {
      echo "Success ".a." ".b." ".c;
   }


};
?>

Script: (Include jquery first)

$('#myform').submit(function(){

var name = $('#name').val();
var age = $('#age').val();
var message = $('#message').val();


$.ajax({
  type: "POST",
  url: "your_page_with_php_script.php",
  data: "name="+name+"&age="+age+"&message="+message,
}).done(function( msg ) {
  alert( "Data Saved: " + msg );
});

});
Friday, November 25, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :