Avatar of burnedfaceless
burnedfaceless asked on

Run a php script with a form select

Is it possible to have a php script called when a select is selected? Without a submit button?

How can I do this? Even if it takes ajax or something?

Avatar of undefined
Last Comment

8/22/2022 - Mon
Dan Craciun

You can use the onclick or onchange events for your select to make an ajax call.

No need for a submit button.

Here's how to call a js script on onchange:

var myDiv = document.getElementById("myDiv");
document.getElementById("mySelect").onchange = function(){
  your code here


Can you provide an ajax example?

I already know JavaScript.
Dan Craciun

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Chris Stanyon

If you intend to use jQuery, then just bind the change event directly:

$('#mySelect').change(function() {
      url: 'myScript.php',
      type: 'post',
      data: { selectValue: $(this).val() }
   .done(function(response) {
      //do something with response

Open in new window

This will fire the AJAX call when an element with an ID of mySelect is changed. It will call a file called myScript.php and pass the value of the SELECT in the POST array as $_POST['selectValue']
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes

Thanks, I'm about to pick up a book on AJAX. I just realized this will do a lot of things I was wondering about.
Ray Paseur

Make a Google search for "Learning jQuery" and follow the links.  A book on AJAX would have been a good idea in 2002.  The whole industry has gone beyond that now and jQuery powers a great many AJAX requests.  You might also be interested in "bootstrap.js"

Thanks I'll read AJAX and visit links and determine what fits our needs.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

I'll examine the js file

But again Ray this is debugging the form distortion so I'm contingent upon loading a php echo
Ray Paseur

this is debugging the form distortion so I'm contingent upon loading a php echo
It's quotes like that which make me want to lead you toward learning the fundamentals.  Web development is like most technologies -- it has its own language.  Without the language, you're crippled and with it you'll make progress in leaps and bounds but only after you learn the vocabulary and syntax.

One really good way to learn a language is by "immersion" so if you can find a local web development user group, I encourage you to join, make presentations, participate in code reviews and generally become active.

Best of luck with your project, ~Ray
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.

Ray I'm right on this. I will accept your advice any day of the week but this is the only way to do what my boss asked.

JavaScript writing html is gimmicky.

Php is great because you can keep the double quotes.

Your intentions are good I know, but this is the easiest solution to the problem.

Thanks for your input ~ Brian