Get selected values in a dual listbox PHP - php

I want to output the selected values from the right hand listbox. However I am not successful in getting the values of selected values. The error returns from the submit function of js. Can anyone tell me why does it return:
An object doesn't support this property
in the submit function? How can i get the values.. will i use ajax or just returned value in PHP?
<form name="frmListSubmit" method="post" id="myform" onsubmit="return submit()">
<table cellpadding="2" cellspacing="0" border="0" id="tblMain" class="edit">
<tr valign="top">
<td> </td>
<td>
<SELECT id="s" size="10" name="source" multiple>
<?php
while($row = mysql_fetch_assoc($getEmployeeList))
{
?>
<option name="list" value="<?php echo $row['id'];?>"><?php echo $row['name']; ?></option>
<?php
}
?>
</td>
</SELECT>
<td valign="center">
>>
<<
</td>
<td>
<SELECT id="d" size="10" name="destination" multiple>
<option name="list" value=""> </option>
</SELECT>
<input type="submit" name="submit" value="submit" />
</td>
</tr>
</table>
</form>
and here is the javascript code:
function listbox_moveacross(sourceID,destID)
{
var src=document.getElementById(sourceID);
var dest=document.getElementById(destID);
for(var count=0;count<src.options.length;count++)
{
if(src.options[count].selected==true)
{
var option=src.options[count];
var newOption=document.createElement("option");
newOption.value=option.value;
newOption.text=option.text;
newOption.selected=true;
try
{
dest.add(newOption,null);src.remove(count,null);
}
catch(error)
{
dest.add(newOption);src.remove(count);
}
count--;
}
}
}
function listbox_selectall(listID,isSelect)
{
var listbox=document.getElementById(listID);
for(var count=0;count<listbox.options.length;count++)
{
istbox.options[count].selected=isSelect;
}
}
function submit()
{
listbox_selectall('d', true);
return true;
}
I have something like this in PHP and it returns only one ID after submission.
if(isset($_POST['submit']))
{
echo $_POST['destination'];
}

This is the weird code:
function listbox_selectall(listID,isSelect)
{
var listbox=document.getElementById(listID);
for(var count=0;count<listbox.options.length;count++)
{
istbox.options[count].selected=isSelect;//<-- ?
}
}
Maybe, you wanted to say:
listbox.options[count].selected=isSelect;
You said, that only 1 value from "destination" is submitted to PHP. To allow multiple values to be submitted, add [] to the names of the <select>-s:
<SELECT id="s" size="10" name="source[]" multiple>
<SELECT id="d" size="10" name="destination[]" multiple>
Also, there is no need to use name attribute for options. You don't use it anywhere.

Related

Php Jquery Set Multiple selected values from database

I have three multiple select menus. I am trying to select the options returned from a database query. It is not setting any options. I console log the arrays returned by php and they are displayed as:["5233", "7148"]["5233", "5437", "5316"]["7029", "7852", "5525"].
I am not concerned about the form submission at this point. I only want to display the values returned from the db query as selected. I am sure there is something I am missing but can't seem to figure it out. I appreciate all the help I get. Thank you in advance!!
First are my 3 database arrays that are created:
Second is my html:
Third is my javascript/jQuery:
I have three multiple select menus. I am trying to select the options returned from a database query. It is not setting any options. I console log the arrays returned by php and they are displayed as:["5233", "7148"]["5233", "5437", "5316"]["7029", "7852", "5525"].
I am not concerned about the form submission at this point. I only want to display the values returned from the db query as selected. I am sure there is something I am missing but can't seem to figure it out. I appreciate all the help I get. Thank you in advance!!
First are my 3 database arrays that are created:
Second is my html:
Third is my javascript/jQuery:
<?
$huntNum = $hunts[$i][$fm_hunt_fields['__id']];
$cookRole = 'Cook';
$cookVols = get_volunteersByHunt($huntNum, $cookRole);
foreach ($cookVols as $cVols) {
//create new cook array that only contains ID of person
$exCooks[] = $cVols['_id_acct'];
}
$guideRole = 'Hunt Guide';
$hgVols = get_volunteersByHunt($huntNum, $guideRole);
foreach ($hgVols as $hVols) {
$exHg[] = $hVols['_id_acct'];
}
$supportRole = 'General Support';
$gsVols = get_volunteersByHunt($huntNum, $supportRole);
foreach ($gsVols as $gVols) {
$exGs[] = $gVols['_id_acct'];
}
?>
<script>
var existing_cooks = <?= json_encode($exCooks); ?>
var existing_hgs = <?= json_encode($exHg); ?>
var existing_gss = <?= json_encode($exGs); ?>
</script>
<form action="" name="vol-update" id="vol-update" class="vol- update" method="post">
<input type="hidden" id="id_hunt" name="id_hunt" value="<?= $huntNum; ?>" />
<input type="hidden" name="action2" id="action2" value="1" />
<table class="tyhp-account">
<tr>
<td>
<div class="floatL cont_lft_side">
<label for="cooks"><strong>Cooks</strong></label>
<select name="cook[]" class="form-control vCooks" multiple="multiple">
<?php
foreach ($cooksArray as $cook) {
?>
<option
<?
if (in_array($cook['ID'], $exCooks)) {
echo "selected='selected'";
}
?>
value="<?= $cook['ID']; ?>" >
<?= $cook['name_last'] . $cook['name_first']; ?>
</option>
<?
}
?>
</select>
</div>
</td>
<td>
<div class="floatL cont_lft_side">
<label for="hunt_guides"><strong>Hunt Guides</strong></label>
<select name="huntGuide[]" class="form-control vHg" multiple="multiple">
<?php
foreach ($guidesArray as $guide) {
?>
<option
<?
if (in_array($guide['ID'], $exHg)) {
echo "selected='selected'";
}
?>
value="<?= $guide['ID']; ?>" >
<?= $guide['name_last'] . $guide['name_first']; ?>
</option>
<?
}
?>
</select>
</div>
</td>
<?php
$allVols = getAllVolunteers();
?>
<td>
<div class="floatL cont_lft_side">
<label for="supp"><strong>General Support</strong></label>
<select name="gsupport[]" class="form-control vSupp" multiple="multiple">
<?php
foreach ($allVols as $allVol) {
?>
<option
<?
if (in_array($allVol['__id'], $exGs)) {
echo "selected='selected'";
}
?>
value="<?= $allVol['ID']; ?>" >
<?= $allVol['name_last'] . $allVol['name_first']; ?>
</option>
<?
}
?>
</select>
</div>
</td>
</tr>
<tr>
<td> </td>
<td style="text-align:center">
<input type="submit" name="action" id="upVol" class="btn btn-primary" value="Update Volunteers">
</td>
<td> </td>
</tr>
</table>
</form>
//Volunteer information for hunts
<script>
var cooks = existing_cooks;
var hunt_guides = existing_hgs;
var gen_support = existing_gss;
console.log(cooks);
console.log(hunt_guides);
console.log(gen_support);
//Cooks multiSelect
$(".vCooks").val(cooks);
$('.vCooks').multiselect({
columns: 2,
placeholder: 'Select Cooks'
});
//Hunt Guide multi-select
$(".vHg").val(hunt_guides);
$('.vHg').multiselect({
columns: 2,
placeholder: 'Select Hunt Guides'
});
//General Support multi-select
$(".vSupp").val(gen_support);
$('.vSupp').multiselect({
columns: 2,
placeholder: 'Select General Support'
});
return false;
</script>

Form action using php

I want to put my select value as my form action + string(/index.php)
but it seems that I can't get the value of the select until I click my submit button.
<tr>
<td>Select Project: </td><td><select name="myproject" onchange="">
<option>Exam1</option>
</select></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="login" id="login" value="Submit"
formaction="<?php $myproject = $_POST['myproject']; echo $myproject."/index.php"; ?>"
formmethod="POST"/>
</td>
</tr>
That is correct. You are mixing server and client side.
After reading your comment I believe you want this (plain JS chosen)
<script>
window.onload=function() {
document.getElementsByName("myproject")[0].onchange=function() {
var path = this.value;
if (path) this.form.action=path+'/index.php';
}
}
</script>
with the HTML now this:
<form method="post" action="">
Select Project: <select name="myproject">
<option value="">Please select</option>
<option value="exam1">Exam1</option>
</select>
<input type="submit">
</form>
To do it on the server, have a look at Post to another page within a PHP script
$myproject = !empty($_POST['myproject']) ? $_POST['myproject'] : "defaultaction.php";

How can store all value in array pressing button inside form.

I have the following html code:
Region:<span id="region">Rajkot</span><br>
Activity:<span id="Select">Cricket</span><br>
Radius:<input type="text" id="radius" value="50000"></input><br>
Gender:<select name="Gender" id="Sex">
<option value="1">Male</option>
<option value="2">Female</option>
<option value="3">Both</option>
</select><br>
Date:<input type="text" id="date" value="2013:04:03 11:54:00"></input><br>
<input type="button" value="Go">
I want to store all the values in array on button click.
I want to have the following functionality:
When I click on go button, all values of html are store in array.
Put everythign inside form ( I assume you already have one)
<form>
Region:<span id="region">Rajkot</span><br>
Activity:<span id="Select">Cricket</span><br>
Radius:<input type="text" id="radius" value="50000"></input><br>
Gender:<select name="Gender" id="Sex">
<option value="1">Male</option>
<option value="2">Female</option>
<option value="3">Both</option>
</select><br>
Date:<input type="text" id="date" value="2013:04:03 11:54:00"></input><br>
<input type="button" value="Go">
</form>
$('form').serializeArray(); will do the trick for you.
Reference
Use serializeArray()
$('form').submit(function() {
$('form').serializeArray();
return true;
});
Simple way to do it using your structure:
FIDDLE
//javascript
function gatherData(){
var arrData = [];
arrData['region'] = $('#region').html();
arrData['Select'] = $('#Select').html();
arrData['radius'] = $('#radius').val();
arrData['Gender'] = $('#Sex').val();
arrData['date'] = $('#date').val();
return arrData;
}
$(document).ready(function(){
//add Id or class to the button for better selector
$('input[type=button]').click(function(){
var data = gatherData();
console.log(data);
//alert(data);
});
});
consider using form as other peple sugested.
<?php
session_start();
if(!isset($_POST["submit"])){
$_SESSION["abc"]=array("C", "C++","JAVA","C#","PHP");
}
if(isset($_POST["submit"])){
$aa=$_POST['text1'];
array_push( $_SESSION["abc"],$aa);
echo "hello";
foreach( $_SESSION["abc"] as $key=>$val)
{
echo $val;
}
}
?>

PHP Populate Text box from Select - Array fields

I need to populate the relevant text box with the results of a Select drop down.
My Javascript so far is
<script language="JavaScript">
var mytextbox = document.getElementById('error');
var mydropdown = document.getElementById('errormsg_id');
mydropdown.onchange = function(){
mytextbox.value = this.value;
}
</script>
My select box and text boxes are built as arrays from a query so there is nultiple of them.
I need to be able to populate the corresponding text box with the results from the Select next to it.
Any ideas?
Thanks
<td width="1%">
<select style="width:100%" name="errormsg_id[]" id="errormsg_id[]">
<?php do { ?>
<option value="<?php echo $row_rserrormsg['errormsg_id']?>"
<?php if ($row_rsdates['errormsg_id'] == $row_rserrormsg['errormsg_id']) { echo("selected='selected'"); } ; ?> >
<?php echo $row_rserrormsg['error_message']?></option>
<?php } while ($row_rserrormsg = mysql_fetch_assoc($rserrormsg)); ?>
<?php mysql_data_seek($rserrormsg, 0);
$row_rserrormsg = mysql_fetch_assoc($rserrormsg);?>
</select>
</td>
<TD align="center" class="adminuser">
<input style="width:96%;text-align:left;" autocomplete="on" title="Enter Error Message" type="text" name="error[]" id="error[]" value="<?php echo $row_rsdates['error_message']; ?>" maxlength="100"/>
</TD>
Assuming You will be using jQuery and Your HTML is like this:
<table>
<tr>
<td><select class="my_select" name="errormsg_id1[]" id="errormsg_id1">...</select></td>
<td><input name="errormsg_id1_txt" id="errormsg_id1_txt" title="..." />
</tr>
...
<tr>
<td><select class="my_select" name="errormsg_idX[]" id="errormsg_idX">...</select></td>
<td><input name="errormsg_idX_txt" id="errormsg_idX_txt" title="..." />
</tr>
</table>
this could be done using jQuery:
$(document).ready(function(){
$('.my_select').change(function(){
$('#'+$(this).attr('id')+'_txt').val($(this).val());
});
});
Hope it is correct, have no time to test it...
Here is a JSFiddle/DEMO on how to get it to work with sample selects:
I think this approach is a little better than #shadyyx since it removed the need for you to manually increment the input and select field names.
<table>
<tr>
<td><select class="select" name="errormsg_id[]">
<option val="test1">Test1</option>
<option val="test2">Test2</option>
<option val="test3">Test3</option>
</select></td>
<td><input class="error_text" name="errormsg_id_txt[]" title="..." />
</tr>
...
<tr>
<td><select class="select" name="errormsg_id[]">
<option val="test1">Test1</option>
<option val="test2">Test2</option>
<option val="test3">Test3</option>
</select></td>
<td><input class="error_text" name="errormsg_id_txt[]" title="..." />
</tr>
</table>
And the JQuery:
$(document).ready(function(){
$('.select').change(function(){
$(this).parent().parent().find('.error_text').val($(this).val());
});
});
NOTICE: If you are using plain Javascript, see this DEMO
<table>
<tr>
<td><select class="select" name="errormsg_id[]" id="errormsg_id1" onChange="update('errormsg_id1')">
<option val="test1">Test1</option>
<option val="test2">Test2</option>
<option val="test3">Test3</option>
</select></td>
<td><input class="error_text"name="errormsg_id_txt[]" id="errormsg_id_txt1" title="..." />
</tr>
...
<tr>
<td><select class="select" name="errormsg_id[]" id="errormsg_id2" onChange="update('errormsg_id2')">
<option val="test1">Test1</option>
<option val="test2">Test2</option>
<option val="test3">Test3</option>
</select></td>
<td><input class="error_text" name="errormsg_id_txt[]" id="errormsg_id_txt2" title="..." />
</tr>
</table>
And Javascript:
function update(element){
var e = document.getElementById(element);
var id = element.substring(element.length-1);
document.getElementById("errormsg_id_txt"+id).value = e.value;
}
You need to think of way to uniquely identify your corresponding set of elements. The most convenient way I would suggest would be to use the primary key value of the data row.
And example would be like
while($row = mysqli_fetch_assoc($result)) {
$id = $row['id']; //A primary key field
echo '<input type="text" name="text'.$id.'" id="text'.$id.'" />';
echo '<select name="select'.$id.'" id="select'.$id.'" />...</select>";
}
Next, call a function from the select menu, so that only the relevant text box is updated. Update the above select portion to read something similar to this.
echo '<select name="select'.$id.'" id="select'.$id.'" onchange="updateBox(\'text'.$id.'\', this.value)" />...</select>";
Now, create a very simple simple function to update the text box.
function updateBox(element, value) {
el = document.getElementById(element);
el.value = value;
}

Inserting Multiple Rows by Form into a Single DB

First time doing this, watch it be a really obvious/easy fix...
Basically I'm trying to insert multiple rows with a single insert query, however, I clearly fail and as such it wont work.
The form is generated by searching a table for the relevant entries it relates to, so if your in department A it will grab your name and form an input line on the form. This all works fine I can generate the form however the issue them comes when I try to insert the form.
Currently it does this thing where rather than inserting the data entered via the form it is entering the letter A across most fields (COUNT, STATUS and TYPE are immune to this as they are restricted to only certain data types via the tables set up itself).
This is currently how I have the form set in the script:
<?php
if ($action == "editrecord") {
?>
<form name="form1" method="post" action="">
<table>
<tr><td colspan="2" align="left">
<?php //Getting Title for Record
$resultTITLE = queryDB("SELECT * FROM merit_subdepts WHERE subdeptID = '$subdeptID'");
while($rowTITLE = mysql_fetch_array($resultTITLE)) {
$recordTITLE = $rowTITLE["subdeptNAME"];
echo "$recordTITLE";
}
?>
</td></tr>
<tr><td>Member Name</td><td>Choose Award</td><td>Reason</td><td></td></tr>
<?php
$resultMERIT = queryDB("SELECT * FROM merit_members WHERE memberPRIMARY = '$subdeptID'");
while($rowMERIT = mysql_fetch_array($resultMERIT)) {
$memberID = $rowMERIT["memberID"];
$memberNAME = $rowMERIT["memberNAME"];
?>
<tr>
<td><?php echo $memberNAME; ?></td>
<td><select name="count[]" class="ui-state-default">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
</td>
<td><input name="reason[]" type="text" id="reason" class="blackInput" value="Monthly Award <?php echo date("F, Y") ?>"></td>
<td><input name="type[]" type="hidden" id="type" value="Civil">
<input name="awarder[]" type="hidden" id="awarder" value="<?php $usersname; ?>">
<input name="datea[]" type="hidden" id="datea" value="<?php $stamp83; ?>">
<input name="status[]" type="hidden" id="status" value="Check">
<input name="id[]" type="hidden" id="id" value"<?php $memberID; ?>">
<input name="dept[]" type="hidden" id="dept" value"<?php $recordTITLE; ?>">
</td>
</tr>
<?php } //end of sub-division membership list ?>
<tr><td colspan="4"><center>
<div class="butDiv"><input name="submitRECORDS" type="submit" id="submitRECORDS" value="Submit Records"></div>
</center>
</td>
</tr>
</table>
</form>
<?php
} //End of Edit Merit Records
?>
This is the insert query used:
if ($submitRECORDS) {
for($i = 0; $i<count($_POST['id']); $i++) //Getting total # var submitted and running loop
{ $fromID = $_POST['id'][$i]; //var
$fromTYPE = $_POST['type'][$i]; //var
$fromCOUNT = $_POST['count'][$i]; //var
$fromDATEA = $_POST['datea'][$i]; //var
$fromAWARDER = $_POST['awarder'][$i]; //var
$fromREASON = $_POST['reason'][$i]; //var
$fromDEPT = $_POST['dept'][$i]; //var
$fromSTATUS = $_POST['status'][$i]; //var
if ($fromID != "0") {
queryDB("INSERT INTO merit_precord (NAME,TYPE,COUNT,DATEA,AWARDER,REASON,DEPT,STATUS) VALUES ('$fromID','$fromTYPE','$fromCOUNT','$fromDATEA','$fromAWARDER','$fromREASON','$fromDEPT',$fromSTATUS')"); } }
$action="";
$msg=1;
}
Assistance much appreciated before I rip my hair out. :)
This may not be the only source of your problems (only the first I notice), but when using <?php $varname ?>, nothing will actually be printed to the screen. Instead you need to use <?php echo $varname; ?>
<td><input name="type[]" type="hidden" id="type" value="Civil">
<input name="awarder[]" type="hidden" id="awarder" value="<?php echo $usersname; ?>">
<input name="datea[]" type="hidden" id="datea" value="<?php echo $stamp83; ?>">
<input name="status[]" type="hidden" id="status" value="Check">
<input name="id[]" type="hidden" id="id" value"<?php echo $memberID; ?>">
<input name="dept[]" type="hidden" id="dept" value"<?php echo $recordTITLE; ?>">
</td>
The A you're seeing inserted is the first letter of Array. When the above values are inserted empty and you try to access them in the for loop with [], the string Array is accessed by character index like an array.
Your INSERT statements, as mentioned in comments, are vulnerable to SQL injection. Escape them with mysql_real_escape_string().
$fromID = mysql_real_escape_string($_POST['id'][$i]);
$fromTYPE = mysql_real_escape_string($_POST['type'][$i]);
$fromCOUNT = mysql_real_escape_string($_POST['count'][$i]);
...
...
$fromSTATUS = mysql_real_escape_string($_POST['status'][$i]);

Categories