Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

jQuery select vaue to php function

Posted on 2013-07-01
4
Medium Priority
?
603 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

705 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