user3546104
user3546104

Reputation: 11

GET the value of the selected option using php then show it with echo

I'm Beginner on PHP, Javascript, I would like to get selected value from select/option and stock it into a php variable ( $varpeoplesnames ), to use it in a future SQL request and show it with echo

my code:

<select name="peoplesnames">
  <option value="1">john</option>
 <option value="2">sarah</option>
 <option value="3">samantha</option>
</select>
<?php   (....)    echo $varpeoplesnames;

How can I get the text of the selected option to show it with echo in php?

Upvotes: 0

Views: 191

Answers (1)

fiction
fiction

Reputation: 1078

Wrap it in a form with action being the link to your php file and a submit button

HTML:

<form action='yourPhpFile.php' method='GET'>
    <select name="peoplesnames">
        <option value="1">john</option>
        <option value="2">sarah</option>
        <option value="3">samantha</option>
    </select>
    <input type='submit'></input>
</form>

PHP

<?php 
$varpeoplesnames = $_GET['peoplesnames'];
echo $varpeoplesnames;
?>

Upvotes: 1

Related Questions