Multi Select Box only shows last selected value - php

I have a multi-select box which allows me to select multiple values and then add them to a second select box when I click on an arrow. Now this part works fine and I can select multiple values. However when I pass the values of the second select box to variable, to a PHP page the variable only shows the value of the last item in the list and not all of them.
Javascript Code
<script type="text/javascript">
$().ready(function() {
$('#add').click(function() {
return !$('#select1 option:selected').remove().appendTo('#select2');
});
$('#remove').click(function() {
return !$('#select2 option:selected').remove().appendTo('#select1');
});
});
</script>
HTML CODE
<form action="insert_email_visitor_list1.php" method="get" name="form1" target="_self" id="form1">
<select multiple id="select1" class="mulitiselect" name="select1">
<option value="1">Date/Time</option>
<option value="2">Company</option>
<option value="3">Location</option>
<option value="4">Nof Pages / Visit</option>
<option value="5">Traffic Source</option>
<option value="6">Search Term</option>
<option value="7">Report</option>
<option value="8">Classification</option>
<option value="9">Owner</option>
<option value="10">Phone</option>
<option value="11">Town</option>
<option value="12">City</option>
<option value="12">Country</option>
<option value="12">Hostname</option>
</select>
<img src="images/add-arrow.png" alt="" />
selected Columns
<img src="images/remove-arrow.png" alt="" />
<select multiple id="select2" name="select2" class="mulitiselect"></select>
<div class="bottom-img"><img src="images/popup-bottomimg.png" alt="" /></div>
<button>
<img src="images/info-icon.png" alt="" />
</button>
<input type="submit" name="send" id="send" value="Submit" />
</form>
PHP CODE
$select2 = $_GET['select2'];
echo "$select2";
Basically I am just after some advice as to if i am doing this the correct way?
Thanks

The field you pass to PHP must be named with a [], e.g. select2[]. Without that, PHP will treat the passed in values as a single value, and only the LAST value passed in would ever bee put into $_GET.
Using [] on the name tells PHP to treat it as a multi-valued field and it will create an array in $_GET, e.g.
$_GET['select2'][0] => 'first option selected';
$_GET['select2'][1] => 'second option selected';
etc...
Remember that the multiple parameter is a purely client-side thing, and PHP has no way of knowing that it was a multi-select. It willy simply see the equivalent of
select2=foo&select2=bar&select2=baz
arrive in the POST/GET, and select the last value passed in.

Related

Passing a value outside the form to another page in url PHP

Im trying to build a web app where a user selects the value from a dropdown list and clicks the button next to the item. But only the value of select gets sent over to the next page.
echo "<td>".$row["idTaco"]."</td><td>".$row["nameTaco"]."</td><td>".$row["priceTaco"]."</td><td>".$row["descTaco"]."</td>";
?>
<td>
<form action="Includes/dodajVKosarico.inc.php?idTaco="<?php $row["idTaco"] ?> method="get">
<select name="kolicina">
<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>
</select>
<button type="submit" name="submit" class = "dodaj">Dodaj</button>
</form>
</td>
The row['idTaco'] works fine in the echo but when i try to send it using this form i cant pass the value to another page. Im sorry im still new to this.
Rather than using a query string value, one (arguably neater and easier to see) option is to just add the value again as a hidden field in your form, then it will get submitted along with the rest of the form data.
<form action="Includes/dodajVKosarico.inc.php" method="get">
<input type="hidden" name="idTaco" value="<?php echo $row["idTaco"] ?>"/>
...etc
Or you could just fix the broken quotes (and missing echo) in your existing code:
action="Includes/dodajVKosarico.inc.php?idTaco=<?php echo $row["idTaco"] ?>"
Everything is fine just missing an echo before your $row["idTaco"], You must do as follows:
<form action="Includes/dodajVKosarico.inc.php?idTaco=<?php echo $row['idTaco']; ?>" method="get">

Multiple selections into input box

I have a selection of countries that a user can choose from. They are displayed in a dropdown box. At the moment I can select one country and it will populate it in the input box, however i want to know if there is a way of selecting more than one and it puts them in the same box.
The data will be populated from a mysql database
Hope im making my self clear.
Here is my code so far:
JAVASCRIPT:
<script language="JavaScript" type="text/javascript">
<!--
function showSelected()
{
var selObj = document.getElementById('countries');
var txtTextObj = document.getElementById('txtText');
var selIndex = selObj.selectedIndex;
txtTextObj.value = selObj.options[selIndex].text;
}
//-->
</script>
HTML:
<form>
<p>
</form><select id="countries">
<option value="val0">Australia</option>
<option value="val1">England</option>
<option value="val2">France</option>
<option value="val3">Italy</option>
<option value="val4">South Africa</option>
</select>
<input type="button" value="Show Index" onclick="showSelected();" />
</p>
<p>
<input type="text" id="txtText" />
<br />
</p>
</form>
So what i would want is if you choose Australia, it shows Australia in the box, then go back to the dropdown and choose South Africa it puts it into the same box as: Australia, South Africa.
Thank you Guys
why no just append new selections to the text already in the inputbox.
txtTextObj.value += selObj.options[selIndex].text +', ';
Use Multiple select box and name as array:
<select id="countries" multiple="multiple" name="countries[]">
<option value="val0">Australia</option>
<option value="val1">England</option>
<option value="val2">France</option>
<option value="val3">Italy</option>
<option value="val4">South Africa</option>
</select>
Now the select will treat as array. Use javascript code to fetch the values selected and convert it to comma separated list and put in the input box.
<select id="countries" multiple="multiple">
<option value="val0">Australia</option>
<option value="val1">England</option>
<option value="val2">France</option>
<option value="val3">Italy</option>
<option value="val4">South Africa</option>
</select>
Use multiple="multiple" in your select box to make multiple selections.

How to return a URL and submit the form using onchange

Firstly, there may be a similar question answered amoungst the site but after carefully considering what solutions and problems I'm still stumped as to what to do.
I'm looking to return a URL and post my users to a page of the chosen URL from a select list.
Currently it works using the normal submit button using the following code;
<form id="formurl" name="formurl" method="post" action="" onSubmit="return getURL(this.url.value)">
<!-- begin postcode list -->
<select size="1" name="url" id="posctode_srch" onchange="this.form.submit()">
<option value="e1-office-space/">E1</option>
<option value="e7-office-space/">E7</option>
<option value="e10-office-space/">E10</option>
<option value="e11-office-space/">E11</option>
<option value="e14-office-space/">E14</option>
<option value="e15-office-space/">E15</option>
<option value="e16-office-space/">E16</option>
<option value="e17-office-space/">E17</option>
<option value="ec1-office-space/">EC1</option>
<option value="ec2-office-space/">EC2</option>
<option value="ec3-office-space/">EC3</option>
<option value="ec4-office-space/">EC4</option>
</select>
<input type="image" src="images/graphics/new/btn_go.png" class="go_btn" width="119" border="0" value="submit" onclick="_gaq.push(['_trackEvent', 'Contact', 'AreaSearchForm',,, false]);" alt="Area search submit" />
</form>
I'm looking to use post but I am testing it with get to see what result is created when choosing an option. when the form is set to get i'm seeing ?url=e7-office-space%2F so am I safe to assume it's working to some degree but there is something missing for the output to just take the value of the option and nothing more?
Being new to this kind of stuff i'm guessing it's a php / html problem as the functionality is there it's just a misused element somewhere so if anyone can help me see where i'm going wrong i'd really appreciate it.
Thanks
You have three things -
setting the url
setting the google stuff
submitting the form if not in the submit event
In the code below you can omit the image if you can rely on javascript only
DEMO
window.onload=function() {
document.getElementById("formurl").onsubmit=function() {
var val = this.url.value;
if (val=="") {
alert("Please select a value");
return false;
}
this.action="http://bing.com/search?q="+val;
_gaq.push(['_trackEvent', 'Contact', 'AreaSearchForm',,, false]);
}
document.getElementById("posctode_srch").onchange=function() {
var val = this.value;
if (val=="") return;
this.form.onsubmit(); // not 100% sure but this should not trigger twice
this.form.submit(); // since this is not triggering onsubmit
}
}
using
<form id="formurl" name="formurl" method="post" action="">
<!-- begin postcode list -->
<select size="1" name="url" id="posctode_srch">
<option value="">Please select</option>
<option value="e1-office-space/">E1</option>
<option value="e7-office-space/">E7</option>
<option value="e10-office-space/">E10</option>
<option value="e11-office-space/">E11</option>
<option value="e14-office-space/">E14</option>
<option value="e15-office-space/">E15</option>
<option value="e16-office-space/">E16</option>
<option value="e17-office-space/">E17</option>
<option value="ec1-office-space/">EC1</option>
<option value="ec2-office-space/">EC2</option>
<option value="ec3-office-space/">EC3</option>
<option value="ec4-office-space/">EC4</option>
</select>
<input type="image" src="images/graphics/new/btn_go.png"
class="go_btn" width="119" border="0" value="submit"
alt="Area search submit" />
</form>

dropdown return value in html and php

I have dropdown list (menu) and a button created with this code:
<form name="period" action="all.php" method="POST">
<div align="center">
<select name=period_dropdown>
<option value="nill"></option>
<option value="48">48</option>
<option value="72">72</option>
<option value="96">96</option>
<option value="120">120</option>
</select>
<input type="submit" value= "OK" >
</div></form>
When option of dropdown is selected, must be on button instead of OK and when button is pressed to be assigned to variable $period1. So, when I select 72, button must have 72 instead of OK and when I click button, variable $period1 to get value 72 (integer). Please, no javascript. Just html and php.
Thanks
You have to use javascript for what you are trying to do. If you want the button to change on the fly without submitting the form, you have to do client-side scripting (javascript).
Either:
Change the value of button after the dropdown is selected and form is submitted
Use two lines of javascript to change the value of the button when the select box is changed
In all.php:
if (isset($_POST['period_dropdown'])) {
$period1 = $_POST['period_dropdown'];
//do something with $period1
}
?>
<form name="period" action="all.php" method="POST">
<div align="center">
<select id="period_dropdown" name="period_dropdown" onchange="updateButton()">
<option value="nill"></option>
<option value="48">48</option>
<option value="72">72</option>
<option value="96">96</option>
<option value="120">120</option>
</select>
<input id="period_button" type="submit" value= "OK" >
</div></form>
<script type="text/javascript">
function updateButton() {
document.getElementById("period_button").value = document.getElementById("period_dropdown").value;
}
</script>

HTML form submit to PHP script

I am making an HTML form. I want the results to appear in the PHP script.
<form action="chk_kw.php" method="post"> <br />
<select> name="website_string"
<option value="" selected="selected"></option>
<option VALUE="abc"> ABC</option>
<option VALUE="def"> def</option>
<option VALUE="hij"> hij/option>
</select>
<input type="submit" name="website_string" >
</form>
The problem is I cannot get the value passed to the PHP. if I use value:
<INPUT TYPE="submit" name="website_string" value="selected" >
It always passes the text in the quotes. In this case "selected". How do I pass one of the strings from the option?
Try this:
<form method="post" action="check.php">
<select name="website_string">
<option value="" selected="selected"></option>
<option VALUE="abc"> ABC</option>
<option VALUE="def"> def</option>
<option VALUE="hij"> hij</option>
</select>
<input TYPE="submit" name="submit" />
</form>
Both your select control and your submit button had the same name attribute, so the last one used was the submit button when you clicked it. All other syntax errors aside.
check.php
<?php
echo $_POST['website_string'];
?>
Obligatory disclaimer about using raw $_POST data. Sanitize anything you'll actually be using in application logic.
<form method="POST" action="chk_kw.php">
<select name="website_string">
<option selected="selected"></option>
<option value="abc">abc</option>
<option value="def">def</option>
<option value="hij">hij</option>
</select>
<input type="submit">
</form>
As your form gets more complex, you
can a quick check at top of your php
script using print_r($_POST);,
it'll show what's being submitted an the respective element name.
To get the submitted value of the element in question do:
$website_string = $_POST['website_string'];
It appears that in PHP you are obtaining the value of the submit button, not the select input. If you are using GET you will want to use $_GET['website_string'] or POST would be $_POST['website_string'].
You will probably want the following HTML:
<select name="website_string">
<option value="" selected="selected"></option>
<option value="abc">ABC</option>
<option value="def">def</option>
<option value="hij">hij</option>
</select>
<input type="submit" />
With some PHP that looks like this:
<?php
$website_string = $_POST['website_string']; // or $_GET['website_string'];
?>
Assuming you've fixed the syntax errors (you've closed the select box before the name attribute), you're using the same name for the select box as the submit button. Give the select box a different name.
For your actual form, if you were to just post the results to your same page, it should probably work out all right. Try something like:
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> method="POST>
Here is what I find works
Set a form name
Use a default select option, for example...
<option value="-1" selected>Please Select</option>
So that if the form is submitted, use of JavaScript to halt the submission process can be implemented and verified at the server too.
Try to use HTML5 attributes now they are supported.
This input
<input type="submit">
should be
<input name="Submit" type="submit" value="Submit">
whenever I use a form that fails, it is a failure due to the difference in calling the button name submit and name as Submit.
You should also set your enctype attribute for your form as forms fail on my web host if it's not set.

Categories