1

get does not working in this code I tried this one and have

Notice: Undefined index: charttype

<form method="POST" action="index.php" name="charttype">
  <select name="charttype" id="charttype" class="custom-select" value="charttype">
    <option selected value="0">Choose...</option>
    <option value="1">Pie Chart</option>
    <option value="2">Scatter Chart</option>
    <option value="3">Bar Chart</option>
    <option value="4">Line Chart</option>
  </select>
</form>

<?php 

$chart_type = "";
$chart_type = $_GET['charttype'];  
?>
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
Abdullah
  • 15
  • 3
  • Until your form is submitted in some way - there may not be any `$_GET` or `$_POST` values. You should always either check if the value exists or that a 'submit' button has been clicked. – Nigel Ren Jun 23 '19 at 08:06

1 Answers1

1

You have defined the POST request inside the form tag, you can not access it by using $_GET

 <form method="POST" action="index.php" name="charttype">

to get both $_GET & $_POST use $_REQUEST

if($_POST){
    $chart_type = $_POST['charttype'];
}

Change your code to following

<form method="GET" action="" name="charttype">
  <select name="charttype" id="charttype" class="custom-select" value="charttype">
    <option selected value="0">Choose...</option>
    <option value="1">Pie Chart</option>
    <option value="2">Scatter Chart</option>
    <option value="3">Bar Chart</option>
    <option value="4">Line Chart</option>
  </select>
  <input type="submit" name="submit" value="submit">
</form>
<?php
 if(isset($_GET['submit'])){
  echo $chart_type = $_GET['charttype'];
 }     
?>  
Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20