Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

370
Views
How to make php function according to the drop down menu list selection without a button

Hello I am working with php and I have this drop down menu list code:

<form method="POST" >
    <select name="users" id="users">
        <option <?php if(isset($_POST['users']) && $_POST['users'] == '2') echo "selected='selected'";  ?> value="2">User one</option>
        <option <?php if(isset($_POST['users']) && $_POST['users'] == '3') echo "selected='selected'";  ?> value="3">User Two</option>
    </select>
    <input type="submit" class="fadeIn fourth"  name="refresh" value="Refresh">
</form>

and then there is this php code that starts as follows:

<?php

if(isset($_POST['refresh'])){
        ....
?>

Now I want to remove the button from the html and want to make the php code execute automatically when selected from the drop-down list menu. I tried something with javascript but wasnt what I needed it was just printing the value as an alert.

this is the code of javascript i tried

var x = document.getElementById('users').selectedIndex; 
var text = document.getElementsByTagName("option")[x].text;//its a text 
document.getElementById("pr").innerHTML = text; 

How do i make it to submit the form?

Tried this other javascript but this makes my page stuck on refresh I put this onchange="fuksion()" on <form>

 <script>
      function funksion() {
      document.getElementById("user").submit();
       }
      funksion();
     </script>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Add an eventlistener (change) to submit the form:

<form action="" method="POST">
  <select name="users" id="users">
    <option <?php if (isset($_POST['users']) && $_POST['users'] == '2') echo "selected";  ?> value="2">User one</option>
    <option <?php if (isset($_POST['users']) && $_POST['users'] == '3') echo "selected";  ?> value="3">User Two</option>
  </select>
</form>

<script>
  let usersSelect = document.getElementById('users');
  users.addEventListener('change', function() {
    this.form.submit();
  })
</script>

<?php
if (isset($_POST['users'])) {
  echo "You selected " . $_POST['users'];
}
?>

On first load of the page the first option will be selected, so the change event will only trigger if you select the second option. To circumvent this, add an "empty" option:

<select name="users" id="users">
  <option value="">-- choose one --</option>
  <option <?php if (isset($_POST['users']) && $_POST['users'] == '2') echo "selected";  ?> value="2">User one</option>
  <option <?php if (isset($_POST['users']) && $_POST['users'] == '3') echo "selected";  ?> value="3">User Two</option>
</select>
about 4 years ago · Juan Pablo Isaza Report

0

use this code you will get value on select

form.php page

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Get Selected Option Value</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
    $("select.value").change(function(){
        var selectedValue = $(this).children("option:selected").val();
        //alert("You have selected  " + selectedValue);
        
        $.ajax({
        type: "POST",
        url: "query-process.php",
        data: { type: selectedValue }, // serializes the form's elements.
        success: function(data)
        {
          alert(data); // show response from the php script.
        }
    });
    });
});
</script>
</head> 
<body>
    <form>
        <label>Select value:</label>
        <select class="value">
             <option <?php if(isset($_POST['users']) && $_POST['users'] == '2') echo "selected='selected'";  ?> value="2">User one</option>
        <option <?php if(isset($_POST['users']) && $_POST['users'] == '3') echo "selected='selected'";  ?> value="3">User Two</option>
        </select>
    </form>
</body>
</html>

query-process.php -->> in this page you will get value by this

print_r($_POST['type']);

after getting the value, you have to run your SQL query to submit the form to your database and also you can run the query for searching value in the database

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!