简体   繁体   中英

Get parameter from selected value to PHP

I have this code in my file

$cdresult=$conn->query("SELECT * FROM tahun");
            echo "<select id=\"cd\" name=\"cd\">";
            do{
                $cdTitle=$cdrow["thn"];
                $cdValue=$cdrow["id_tahun"];
                echo "<option value='$cdTitle'>
                $cdTitle
            </option>";
            }while ($cdrow=$cdresult->fetch_assoc());
            echo " </select>";
            $selected_val=$_POST['cd'];
                echo "You have selected : $selected_val";

how to get the value of the option when i selected it from my site, because when i use

$selected_val=$_POST['cd'];           
echo "You have selected : $selected_val";

i got this error Notice: Undefined index: cd in C:\\xampp\\htdocs\\xxx\\xxx\\note.php on line 99

i use this script for get the value so i can make validation for my query. thank you

$_POST['cd'] will be undefined if the browser never sent that information to your script. This happens for instance when you first load the page (when no form has been submitted).

Test whether the data is available before you try to use it. Change the last two lines to

if(isset($_POST['cd'])) echo "You have selected : $_POST[cd]";

I am not sure how the values are being submitted to this page, Your input field should be inside a for submitting the value.

Hence you are getting that warning, you can get rid of it by checking,

if ( true == isset( $_POST['cd'] ) ) {
    $selected_val = $_POST['cd'];
    echo "You have selected : $selected_val";
}

PHP Dealing with Form

try this code :

<html>

 //function for get selected element
<script type="text/javaScript">
  function getval() {
        var val = document.getElementById("cd").value;
        document.getElementById('selected').innerHTML = '<span>' + val +'</span>';


   }
</script>
<form action="" method="POST">
<?php 

  $cdresult=$conn->query("SELECT * FROM tahun");

              echo '<select id="cd" name="cd" onchange="getval()">';
              while ($cdrow = $cdresult->fetch_assoc()) {
                    echo '<option value="'.$cdrow['id_tahun'].'">'.$cdrow['thn'].'</option>';
                }
              // do{
              //       var_dump($cdrow);
              //     $cdTitle=$cdrow["thn"];
              //     $cdValue=$cdrow["id_tahun"];
              //     echo "<option value='$cdTitle'>
              //     $cdTitle
              // </option>";
              // }while ($cdrow=$cdresult->fetch_assoc());

              echo " </select>";
              $selected_val=$_POST['cd'];
                  echo "You have selected : $selected_val";

  ?>
  <button name="submit">submit</button>
  </form>
</html>

 function getval() { var val = document.getElementById("cd").value; document.getElementById('selected').innerHTML = '<span>' + val +'</span>'; console.log(val); } 
 <html> <select name="cd" id="cd" onchange="getval()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select> <div> you have selected value :<span id="selected"></span> </div> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM