Chosen get multiple selected values without jquery - php

In Chosen I get multi-selected values using the following code in jQuery -
$("chosen-selector").chosen().val()
But if I want to get these multiple selected values of select within a form via PHP it gives only single option value -
$multiple_values = $_REQUEST['chosen_select_name']
$multiple_values variable returns only a single value. How can I get multiple selected values using PHP.

You need to make this field as array so that php can understand you will be passing multiple values. in html part make your field name from "chosen_select_name" to "chosen_select_name[]" and check the request.
Multi select behavior works same as checkbox logic.

Related

Processing multiple selections(from a drop down menu) in php and querying database

I am doing multiple selections from a dropdown input form. So, for example: select multiple values 1,2,3,4,5,6. These are IDs from a list of 50 IDs.
My database table has 2 columns - ID and value.
Now, my goal is to write php script with sql query that would take these input IDs(provided by the input form) and return the ID with MAXIMUM value.
I can do it when I know the input IDs.
For example the sql query would be :select id, value from tablename where value= (select MAX(value) from tablename where id IN (1,2,3,4));
However I am unable to find a way, how to handle this situation when I have multiple selections which can vary (since they are coming from an input form).
Should I handle it in the php or SQL? How should I approach this?
When posting your form, your multiselect will be sent as an array. Use the implode(glue, array) function and then insert the resulting string into your query.

Dynamically build AJAX request based onChange event in form

So I have several form elements which are currently acting as filters - I plan to use this as a basis for a search feature on my website.
I was thinking about linking a set of on change events to dyanmically build an AJAX query that selects fields from a set of tables but I'm not too sure how I can go about doing it.
On page load I have the following: http://jsfiddle.net/uVhzZ/. The pseudo SQL for this setup would be: SELECT * from ts_rooms WHERE capacity >= 50 (Any means no WHERE condition has been set)
However when the user starts to make changes, I would like this additions to be dynamically added to the SQL query - for instance if Lecture Style is set to Lab and capacity changes to 75 then pseudo SQL would change to SELECT * from ts_rooms WHERE capacity >= 75 AND lectureStyle="Lab";
How do I go about doing this?
First you gotta prepare your AJAX request, it can be done by creating an array with all selects you wanna use (you can look on ther .val() and if it has any value you add this value to that array using the select id as array key) then converting it to JSON.
PHP will receive this JSON string and convert it to array. Then you can look thru this array, and for each element you go building your where statement.
Then it's just a matter of taking query resultset and build another array from it, converting to JSON and responding to JS.
Put all your elements in a form, post it to your php search file. Created a sql query with the relevant constraints and return data in json and display it.

Multiple select fields PHP

I am working on a booking script in PHP and I need to get their values. To be clear the select is not multiple value select where you select many values in one select. It is just plain and simple select fields in a form. The problem is i can't be sure how many of them there are and what their names are. Is there a way to get their names and and values without knowing how many of them there are and what their names are when I click the submit button? I use a while loop to print them. I was suggested use sessions to achieve this but I am not exactly sure how I would implement it.
All data that the html part is tranfering to the php part of the page is sent to a variable ($_GET, $_POST). Which one is defined by the method you are using for the form itself. The variable itself is built as an array with fieldname as the key of the array element and the value (either value of the selected element for select fields, or the input value for input fields, ....) as value of the array element.
If you want to parse through all elements that you have in the form field, then you can use foreach (it gets a bit more complicated if you don't want to parse through all elements......then I would suggest that you make sure that all the fields that you want to process begin with a specific "tag" in their name, which only they have. As an example c_Field1, c_Field2, Field3, Field4,.... with c_ being the tag....you can then either use string functions on the key (which is equal to the fieldname) or you can use arrayfunctions to only get these fields).
As an example with foreach if you want to get all fields beginning with 'c_':
foreach ($_POST as $fieldname => $value)
{
if (str_replace('c_','',$fieldname)!=$fieldname)
{
//field to be processed so use $value
}
}
If you submit them, they are in the POST variable (well, could be GET, but probably not). That thing is an array, so you can use all the fancy array functions to find out what's inside
for instance:
http://php.net/manual/en/function.array-keys.php
or just a plain old foreach I guess.

multiple inserts via for loop and array with php (recieve via jQuery AJAX)

i'm sending values from one page via jquery's ajax to a php page. I'm hoping to store the recieved values into a db. so far this is my php page:
<?php
$supp_short_code = $_POST['supp_short_code'];
$om_part_no = $_POST['om_part_no'];
$description = $_POST['description'];
$price_each_nett = $_POST['price_each_nett'];
$cost_total = $_POST['cost_total'];
?>
The thing is, this php is going be receiving potentially multiples of each post with every "submit" so there could be 5 of each (ie. 5 items ordered). So where would i go from here to stick these values into a suitable mysql query. Also every order needs to have a unique id. Its actually getting the same id number to each order item that i'm struggling with, if that makes sense? Do these need to go into an array and then through a for-loop? Essentially one row in the db is these fields from the "post"...
The form fields need to be element arrays in HTML, and you will have to treat each $_POST element as an array. In your form's HTML add brackets to the field names, i.e. name="supp_short_code[]" When the form is submitted you can loop through these items and complete an insertion into the database.

PHP - Pulling single value from multidimensional array

this is probably quite simple but I could do with some help.
I am trying to create a small PHP function that will display a single value form a multidimensional array when the user used two dropdown boxes to select the row and column of the array.
So, the user will make a selection from the first dropdown box, which will contain the titles of the rows, and then make a selection from a second dropdown box, which will contain the titles of the columns. Once the selections have been made, the function then needs to output the value for the specific row and column selected.
I thought I had created an array that would work but, sadly no. I have 6 rows and 6 columns in my data table.
Also, is there a JQuery or Javascript alternative?
Just looking for a few pointers to get me on my way.
Thanks in advance,
Micanio
You could either do this on the server-side or through JS.
JS: Have the script update a hidden form field with the value using the onChange() event of the drop downs. Then simply grab that hidden field when the form is posted back to the server (of source always checking for valid data).
PHP: The form will provide the two values $_POST['field1'] and $_POST['field2'] (which of course you will sanitize before using). The script could define a multidimensional array that you could feed those two values into:
$finalValue = $mdArray[$SanitizedField1][$SanitizedField2];
From there just store the $finalValue however you'd like.
$data = array();
for ($i=0;$i<6;$i++) {
$data[$i] = array();
for ($j=0;$j<6;$j++)
$data[$i][$j] = rand(0,100);
}
This should create an array similar to what you have described.
You can then access it like so...
echo $data[0][3];
Your form would need two select fields, both will values 0-5 then you can take the both of them and use them to access a value in your array.
If I understand your question correctly, you need something like a user selects a drop down list. Once selected, the option populates a second list. A real world example would be a user selects on Country to fill in a form and it will populates the States drop down list.
This kind of functionality is usually done with javascript not server side php.
http://javascript.internet.com/forms/country-state-drop-down.html

Categories