i am using chosen for my select dropdown. what i want is when i post data save it in an array to post data to database,
Problem is that when i post data it gets only one value instead of multiple values. I tried using gettype and posted value is a string not an array
Below is the code
<select id="tableset" multiple="multiple" class="form-control select2 table_id" name="table_id[]">
<option value="">Table</option>
<?php foreach ($tables as $tbls) { ?>
<option value="<?php echo $tbls->id; ?>"><?php echo $tbls->name; ?></option>
<?php } ?>
</select>
php code
foreach ($_GET['table_id'] as $selectedOption){
echo $selectedOption;
}
this prints last item selected , can you help me to specifiy whats wrong here
If you wouldn't mind, you can try these.
function SelectedValues(){
var result=[]
$("#table_id:selected").each(function(){
result.push($(this).val();
});
return result
}
function functionForSendingAjax(){
var option = SelectedValues();
jsondata = json.stringify({"data":option});
ajax({
data:jsondata;
//url:
//success:
});
}
Related
Not sure I have worded the title correctly, so apologies if it's confusing!
Here is my problem
I have a dynamic select that is populated via data on an ajax call. I have a function that runs with a "change" of the first select. This works perfectly fine if the user manually selects the first dropdown items, however I am stuck on how to get this to run properly when they are editing.
This is what I want to achieve:
Load the page based on the ProdID and populate other form fields (this is happening now, so don't need help with that ;-))
Check what's pulled in from getData.php
Populate the CatID select and the mark the current selection as "selected".
If the user changes either of the select boxes, the function runs as it does currently.
Here is my function:
$(function() {
$("#topcatid").change(function() {
$("#catid").load("getData.php?choice=" + $("#topcatid").val());
});
});
Here is my html:
<select name="topid" id="topid" data-placeholder="Choose a category...">
<option value="" <?php if (!(strcmp( "", ($rsProd->getColumnVal("topcatid"))))) {echo "selected=\"selected\"";} ?>>Select</option>
<?php while(!$rsCat->atEnd()) {?>
<option value="<?php echo($rsCats->getColumnVal(" topcatid ")); ?>"<?php if (!(strcmp($rsCat->getColumnVal("topid"), ($rsProd->getColumnVal("topid"))))) {echo "selected=\"selected\"";} ?>>
<?php echo($rsCat->getColumnVal("catName")); ?>
</option>
<?php $rsCat->moveNext(); } $rsCat->moveFirst(); ?>
</select>
<select name="catid" required class="chzn-select" id="catid" style="width:350px" tabindex="2" data-placeholder="Choose a sub-category...">
<option value="">Select from above...</option>
</select>
If the user manually changes the TopID select, the function runs, goes and gets the data and populates "catid". However this is on a page that is an edit/update page so I need the function to run as per my ideal scenario above.
Your comments and code edits would be very much appreciated.
Thanks
Nick
For me you should do this in a calback function that will be executed just after the load.
$("#topcatid").change(function() {
$("#catid").load("getData.php?choice=" + $("#topcatid").val(), function() {
// Manage the old and the new catid
});
});
You can also use the jquery.ajax function that will give you more options for functions before and after the query:
$.ajax({
url: ""getData.php?choice=" + $("#topcatid").val()",
beforeSend: function( xhr ) {
// Do something
}
})
.done(function( data ) {
// Do something with data
});
I have a query to select data from table(database) to show in List-view (table), than I want make code search data in list-view(table) by select-box without button submit.
this is my code in select box
<select onchange="selectrun(this);">
<option value="">Select</option>
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
And this is my scrip
function selectrun(sel){
var id= sel.value;
$.ajax({
type:"POST",
url:"./tab.php",
data:{id:id,task:'search'},
success: function(response){
//(I don't know what i should write for pass to php code)
//what I return in response is a query because I want it's execute at my main page,that why I want pass it to $querr_select in php code but I don know my solution is good or not because I never do with ajax
}
});
}
This is my code in main page
$query_select = "SELECT * FROM `table`";
$result=pg_query($query_select ) or die(pg_last_error());
while($row_info=pg_fetch_array($result)){
//code for display view
}
*Note: in tab.php,I just pass id from main page to page tab.php for write a query to select in condition in where; when I alert response I get SELECT * FROM table WHERE ID ='1' And I want pass it to $query_select, is my idea but not work yet :(
I think what you are asking is how to display the result of an Ajax query. Is that correct?
<select onchange="selectrun(this);">
<option value="">Select</option>
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
<!-- A new HTML div for displaying Ajax call response: -->
<div id="response-area"></div>
<script>
function selectrun(sel){
var id= sel.value;
$.ajax({
type:"POST",
url:"./tab.php",
data:{id:id,task:'search'},
success: function(response){
//Jquery sends response to browser div by setting html.
$('#response-area').html(response);
}
});
}
</script>
tab.php:
A basic concept of how you might return HTML via Ajax. This isn't great programming in terms of mixing HTML and PHP, but it it probably does what you want.
Assuming that your database table contains fields called 'field1' and 'field2', you can iterate through the array using the field names as array keys. Note that pg_fetch_array has additional parameters to select an associative array rather than a numerically indexed one.
<?php
$query_select = "SELECT * FROM `table`";
$result=pg_query($query_select ) or die(pg_last_error());
echo "<table>";
while($row_info=pg_fetch_array($result, NULL, PGSQL_ASSOC)){
echo "<tr>
<td>
$row_info[field1]
</td>
<td>
$row_info[field2]
</td>
</tr>";
}
echo "</table>";
?>
The modified code above should show you the response returned from tab.php when you change the option selected.
i have created the drop down menu using php and i want to use the selected value of menu on url of the current page. How can i do this without redirecting to the next page. the code for creating dropdown menu is:
echo '<select name="Company">';
if($data['count']>0){
foreach($data['results'] as $key=>$value){
echo "<option value='".$value['CID']."'>".$value['Name']."</option>";
}
}
echo "</select>";
Hum? Dont know what you exactly want. You want to create a dropdown menu and use the selected value on the current php page?
That wont work. Ill show you how it works:
PHP renders page -> gives it to you.
When you see the page, the php-script is already done. So you have to call your page again and give it the value via $_POST ...
echo <form action="page.php" method="post">
echo '<select name="Company">';
if($data['count']>0) {
foreach($data['results'] as $key=>$value){
echo "<option value='".$value['CID']."'>".$value['Name']."</option>";
}
}
echo "</select></form>";
... or $_GET (this only works in combination with JS!) ...
echo "<a href='page.php?company=companyname'>Im a link!</a>"
You need to get drop down value using javascript or JQuery
In Javascript:
<script type="text/javascript">
var myselect=document.getElementById("Company")
for (var i=0; i<myselect.options.length; i++){
if (myselect.options[i].selected==true){
alert("Selected Option's index: "+i)
break
}
}
</script>
In JQuery
$('select[name=Company]').val()
It's not entirely clear how you want to use this value, but you can refer to it on the same page using javascript. Consider the code below, which will update the line "Your company is:" whenever a new option is selected from the dropdown box.
<script type="application/javascript">
function update_box() {
selected_value = document.getElementById('company_dropdown').value;
document.getElementById('your_company').innerText = selected_value;
}
</script>
<form action="page.php" method="post" id="my_form" name="my_form" onchange="update_box()">
<select name="company_dropdown" id="company_dropdown">
<option value='A'>A</option>
<option value='B'>B</option>
<option value='C'>C</option>
</select>
</form>
Your company is: <span id="your_company" name="your_company"></span>
If I am getting it right, you want to use the selected value from dropdown on the same page url via querystring.
You can do that like following:-
echo '<select name="Company" onchange="location.href = location.href+'?var=' + this.value">';
if($data['count']>0){
foreach($data['results'] as $key=>$value){
echo "<option value='".$value['CID']."'>".$value['Name']."</option>";
}
}
echo "</select>";
When you will change the value in dropdown, current page will refresh with the selected option value in querystring. You can use the querystring value as following:-
<?php echo isset($_GET['var']) ? $_GET['var'] : ''; ?>
You can also do it via use of hash value without refreshing the page
When ever you change the dropdown on the onchange event you can write the code
onchange="window.location.hash(this.value);"
it will set a hash value in the url and then you can write a function which will detect the change in the hash value as the page is not refreshing you need to track the change in the hash value and then you can show the selected menu on the basis oh the hash value.
use this to detect the change in the hash value
$(document).ready(function() {
$(window).bind('hashchange', function(e) {
var hashStr = window.location.hash;
hashStr = hashStr.substring(1, hashStr.length);
hashArray = hashStr.split('&');
// here you will get the array of all the parameters you passed in the array
});
});
I have the following code that programatically generates a dropdown box in php for me.
echo "<select name=\"choice\">";
echo "<option selected=\"selected\" disabled=\"disabled\">Number of Columns</option>";
for ($i = 1; $i <= 10; $i++) {
echo "<option>$i</option>";
}
echo "</select>";
I need to use whatever item the user selects later but I'm not actually sure where the selected value is stored? I know this sounds silly but how do I access the selected item through HTML once the user has made a choice?
Thanks
You don't use HTML to gather the value. HTML is an output language. You use either Javascript or PHP to access those items.
A jQuery Example would look like this:
<form action="submitToPhp.php">
<select id="dropdown" name="dropdown">
<option value='1'>first</option>
<option value='2'>second</option>
</select>
</form>
<script>
$('#dropdown').change(function() {
var selectedItem = $(this).val();
alert(selectedItem);
// Outputs the value of the selected dropdown
});
</script>
A PHP version would be submitting that form to a PHP script:
<?php
$post = $_POST;
$selectedItem = $post['dropdown'];
echo $selectedItem;
// Outputs the value of the selected dropdown item
You could always locate the select element through the parent form element.
eg:
alert(document.forms[0].choice.value);
Im starting in javascript so, I need to do this : From a list box(already has mysql data using php) I want to get the current selection id (select,selected I think in Java) and fill a listbox with data using the id of the first listbox.But this must happens on onChange property of the listbox1 but on selection.
Here is what I have so far
Html code
Select a Region
$result_disp = mysql_query($query_disp, $cn);
$res=mysql_select_db("xxxx",$cn) or die("Note: " . mysql_error());
$res=mysql_query("select Region_ID, Region_Description from regions");
while($query_data = mysql_fetch_array($res))
{
?>
<option value="<? echo $query_data["Region_ID"]; ?>"
<?php if ($query_data["Region_ID"]==$_post['Region_ID']) ?>>
<? echo $query_data["Region_Description"]; ?></option>
<? } ?>
<select name="Dep" id="Items">
<option>Département</option>
</select>
javascript
function setSelect(id,value) {
var sel = document.getElementById(id);
var option, options = sel.options;
var i = options.length;
while (i--) {
option = options[i];
option.selected = (option.value == value)? true : false;
}
}
thanks in advance!
If you want to manipulate and query the DOM, use the great javascript library jQuery.
Once you have jQuery downloaded and included in your page, you can then do things like this:
<select id='myFirstSelect'>
<option value="1"/>
<option value="2"/>
</select>
javascript: (Bind a function to the 'changed'-event)
$('#myFirstSelect').changed(function(){
var selectedValue = $(this).val();
//do something with selectedValue, e.g. $('#someOtherSelect').val(selectedValue);
});
I haven't tested this code, it's just to give you an idea how much faster and more elegant you get ahead, once you get into jQuery (keywords: Selectors and Events)