Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

jQuery select vaue to php function

Posted on 2013-07-01
4
Medium Priority
?
610 Views
Last Modified: 2013-07-01
I want to know if I can pass a select dropdown value to a php function
In other words when a user selects from dropdown list have the selected value then sent to a php function along with other values
0
Comment
Question by:leozayas
  • 2
  • 2
4 Comments
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 39291318
Are you talking about passing it to PHP as part of a form submission or as part of an AJAX call or something else entirely?

As long as a <select> has a name attribute, it will be part of a form submission. If you need to access a dropdown value with Javascript, make sure the select dropdown has an id attribute and just use jQuery's val() call:

The HTML:
<select id='some_dropdown'> ... </select>

The jQuery Javascript:
alert($("#some_dropdown").val());
0
 

Author Comment

by:leozayas
ID: 39291438
No at the time of the function it will neither be a form submission nor ajax call.

I have a select type that the user selects a client from, which gives a numerical value of the client, later on in the page they also select from the keywords that they want to filter by.

This select calls a php function to build it, that chooses the keywords according to the client that was selected earlier. the php code is in a class file and take the form of getKeywords($client), it is here that I want to inject the client number that was chosen earlier.

Sorry for not being clear before

Thx
0
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 2000 total points
ID: 39291494
What you're describing -is- an AJAX call:
This select calls a php function to build it...

Open in new window


PHP runs on the server, Javascript runs on the client-side. They cannot talk to each other directly.

Imagine you own a restaurant. PHP is the chef in the back, and Javascript is the waiter in the front, who deals with the customers. Javascript can collect the order, but the waiter does not cook the meal right at the customer's table. Instead, the waiter writes down the order, he takes it back to the chef, and then the chef creates the meal, and gives back the finished results to the waiter. Then the water takes the prepared food to the customer.

This is how Javascript works with PHP. They don't talk directly. The waiter has no idea what the chef is doing and the chef has no idea what the waiter is doing. The waiter simply gives the order to the chef and the chef knows how to read it, but they operate in different rooms and on different schedules.

AJAX is simply the programming method that describes that back-and-forth communication, where Javascript temporarily goes to the kitchen to drop off the order with PHP, and PHP hands back the results.

That said, it sounds like you are  already doing an AJAX call to the script containing the getKeywords() PHP function. If you want to add another parameter, you need to update the PHP function to take a second parameter, like:

function getKeywords($client, $another_parameter)
{
   ...update the contents to use $another_parameter...
}

Then you need to find out how you are sending the AJAX call and update it to include the value of the <select> box using $("#your_dropdown").val().
0
 

Author Closing Comment

by:leozayas
ID: 39291701
thank you, going by what you said, what i did was send an ajax call to a separate php file with the value of the select and built the new select there and sent it back.... :-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question