Solved

jQuery select vaue to php function

Posted on 2013-07-01
4
577 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 34

Expert Comment

by:gr8gonzo
Comment Utility
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
Comment Utility
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 34

Accepted Solution

by:
gr8gonzo earned 500 total points
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now