multiple select value foreach in php - php

Ok i have the problem! :)
I have a two same name select in my page ( if statement handling ) and i forgot add value options for one.... my bad :) Thanks for replys!
I have a multiple selection in my page:
<select multiple="multiple" name="diff[]">
<option value="1">Easy</option>
<option value="2">Medium</option>
<option value="3">Hard</option>
</select>
And i want to get the option-s value so:
foreach($_POST['diff'] as $key) {
echo $key;
}
I want to option values but i get this values -> Easy, Medium, Hard.
How i can acces the value="" attribute ?
Thanks dudes!

Your code should work, I guess the problem is how you post it.
Here is a little sample code, tested on my server. It gives the desierd ouput:
Edited Code:
<form class="form-inline" method="POST" action="">
<select multiple="multiple" name="diff[]">
<option value="1">Easy</option>
<option value="2">Medium</option>
<option value="3">Hard</option>
</select>
<button type="submit" class="button" name="mentes" id="mentes" title="Mentés az adatbázisba">Send</button>
</form>
<?php
if(!empty($_POST["diff"])) {
foreach($_POST['diff'] as $key) {
echo $key;
}
}
?>
When I select "easy" and press "Go" -> Output: 1
This works for all the other inputs as well.

Related

Get value of select, from a list of users

i have a table of users, where a user has a name, a surname, a right, and a "commit changes" button. This way, i want to assign rights to a person. Every column has the userID in it, and the whole thing is created from a database.
This is my dropdown to assign a right.
<form id="myForm" method="post" autocomplete="off">
<select name="rightsDropdownOptions" method="post">
<option value="admin">Admin</option>
<option value="masterEditor">MasterEditor</option>
<option value="modulEditor">ModulEditor</option>
<option value="teacher">Lehrer</option>
<option value="teacherAndEditor">Lehrer+Editor</option>
</select>
</form>
This is my php-code, which is executed by clicking the commitchangestorightsbutton
if ( isset($_POST['commitChangesOnRightsButton']) ){
$user = $_POST['commitChangesOnRightsButton'];
echo "<script>alert(".$user.");</script>";
$var = $_POST['rightsDropdownOptions'];
if($var == "1"){
echo "<script>alert(".$var.");</script>";
}
}
If i click the button now i get the following error:
Undefined index: rightsDropdownOptions in C:\xampp\htdocs\Iiigel\PHP\AdminGivePermission.php
I've been working on this for a while now, and i dont knwo how to solve this, id be glad if someone could help.
As highlighted by #Dharman you are making mistake in your code firstly you have to define post parameter "commitChangesOnRightsButton" into your code like this:
<form id="myForm" method="post" autocomplete="off" action"">
<select name="rightsDropdownOptions" method="post">
<option value="admin">Admin</option>
<option value="masterEditor">MasterEditor</option>
<option value="modulEditor">ModulEditor</option>
<option value="teacher">Lehrer</option>
<option value="teacherAndEditor">Lehrer+Editor</option>
</select>
<button type="submit" value="Assign" name="commitChangesOnRightsButton" />
</form>
Hope this will help!

Extract html input with php

I am new to php and want to make a dropdown menu where you can select a site that you want, then when you click a button you will be redirected to that site. The button will take you to for example "google.com" if that is what you select, if you select "stackoverflow.com" the same button will take you there. I currently have no working php code as I am not sure where to start. I will include the html code below.
<form method="post" action="testttt.php">
<select id="mySelect" onchange="myFunction()">
<option value="SelectSite"> Select Site Please
<option value="Itslearning"> Itslearning
<option value="NDLA"> NDLA
</select>
<input type="submit" value="GO"/>
The form ends a bit further down, so please don't complain about there being no :P ANY HELP IS APPRECIATED :)
Try the below code , i have done the example which you want and its work perfectly . You can do it this in single file as i have done .
<?php
if(isset($_POST['btnsubmit']))
{
$options = $_POST["testing"];
header('Location: '.$options.'');
echo "Your option value".$options;
}
?>
<form method="post" action="#">
<select id="mySelect" name="testing" onchange="myFunction()">
<option value="SelectSite"> Select Site Please
<option value="http://www.google.com"> Itslearning
<option value="http://www.stackoverflow.com"> NDLA
</select>
<input type="submit" name="btnsubmit" value="GO"/>
</form>
testttt.php will receive values in the $_POST array, so things like 'echo $_POST["somefield"];' will get you data...
...BUT you have to name your form elements. Not '<select id="somefield"...' but '<select name="somefield"...'. you can still have id attributes if you want, but pass to $_POST happens by what you put in the name= attribute.
Once you know the URL you're redirecting to, header("Location: $url");
Hope that gives you the push you need :-)
can you not just use the myfunction function like this:
function myfunction(event){
return window.open(event.target.options[event.target.options.selectedIndex].value,'');
}
If you want to do it in php, your select need a name attribute :
<form method="post" action="testttt.php">
<select id="mySelect" name="mySelect" onchange="myFunction()">
<option value="SelectSite"> Select Site Please
<option value="Itslearning"> Itslearning
<option value="NDLA"> NDLA
</select>
<input type="submit" value="GO"/>
</form>
You can do that either in PHP :
if(isset($_POST['mySelect']) && !empty($_POST['mySelect'])){
header('Location: ' . $_POST['mySelect']);
}
Or in Javascript using the onchange attribute you already set up :
myFunction(){
var target = event.target;
document.location.href(target.options[target.options.selectedIndex].value);
}

MYSQL SELECT with user selected OPTION values

I am trying to filter out a MYSQL table according to user selected values in my php file list.php
My table users have field stdYr, in which elements have values of S00, S01, ... S40.
in html body, I have this chunk of code:
<form method="post" role="form" action="list.php">
<div>
<select multiple>
<option value="all">Select</option>
<option value="S00">74</option>
<option value="S01">75</option>
<option value="S02">76</option>
...
<option value="S40">114/option>
</select>
<button type="submit"></button>
</div>
</form>
And then, I want to select those with selected stdYr values.
The approach I could come up with is passing the values of option through $_POST:
if(isset($_POST['S00']) || isset($_POST['S11']) || ...
... || isset($_POST['S40'])) {
/*some code to get only those set values*/
$query="SELECT * FROM users WHERE stdYR = '/*some stuff*/'";
}
But with different possible selections of values, I couldn't figure out how exactly to do this...
And even with the solution, I see this approach very repetitive and silly.
How am I supposed to deal with such things?
I greatly appreciate your help! Thanks :D
Your form is incorrect. Your select MUST have a name attribute for it to get submitted to the server.
<select name="options[]" multiple>
^^^^^^^^^^^^^^^
<option ...>
...
</select>
Note the [] on the name field. That'll tell PHP to accept multiple values, making $_POST['options'] an array itself. Then it's a simple matter of:
foreach($_POST['options'] as $option) {
insert_into_db($option);
}
Use this way:
<form method="post" role="form" action="list.php">
<div>
<select name="MyOptions[]" multiple>
<option value="all">Select</option>
<option value="S00">74</option>
<option value="S01">75</option>
<option value="S02">76</option>
...
<option value="S40">114/option>
</select>
<button type="submit"></button>
</div>
</form>
print '<pre>';
print_r($_POST);
$selectOption = $_POST['MyOptions']; //get post result and then work as you wish
You could try this :
<select name="stdYR[]" multiple>
<?php
$stdYR = $_POST['stdYR'];
$stdYR_ct = count($stdYR);
$i=0;
while($i < $stdYR_ct){
$stdYR_SQL .= "'$stdYR[$i]',";
$i++;
}
if (preg_grep("/All/i", $stdYR) or ($stdYR_ct < 1)){
$stdYR_SQL = "";
}else{
$stdYR_SQL = eregi_replace(",$",'',$stdYR_SQL);
$stdYR_SQL = " AND stdYR IN($stdYR_SQL)";
}
$query = "SELECT * FROM users WHERE 1=1 $stdYR_SQL ";
?>

Form Selectbox with wrong Value

Hy there
I have some selecboxes on my site like this one:
<div class="registrationdate">
<select class="filter registrationdate" name="rdat" id="regdate">
<option value="">---</option>
<option value="2012">at 2012</option>
<option value="2011">at 2011</option>
<option value="2010">at 2010</option>
</select>
</div>
now i got a problem after submit the form...
if "at 2011" is selected after submit i get just "2011", the value, but i want the text.
after refresh the select boxes change the text to "at 2011"
in source i can see the problem, but i don't know how to solve it.
after submit the form and i land on the same page the source of the selectbox above is altered like this:
<div class="registrationdate">
<select class="filter registrationdate" name="rdat" id="regdate">
<option value="2012">2012</option>
<option value="">---</option>
<option value="2012">at 2012</option>
<option value="2011">at 2011</option>
<option value="2010">at 2010</option>
</select>
</div>
so can here some help me? i hope i expressed me understandable.
thanx for help!
EDIT
OK, i think i have found the Problem in the code of my coworker... but i encounter another problem, the same what he wanted to solve.
This is the actual code:
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' name='filterform' method='get' class="filterform">
<div class='registrationdate'>
<strong><?php echo $GLOBALS['Lng']['filter_Erstzulassung']; ?>:</strong>
<select id="regdate" name="rdat" class="filter registrationdate">
<?php if (isset($_GET['rdat'])&&$_GET['rdat']!='') echo '<option value="'.$_GET['rdat'].'">'.$_GET['rdat'].'</option>'; ?>
<option value="">---</option>
<?php for ($i = date(Y); $i >= (date(Y)-7); $i-- ) {
echo '<option value="'.$i.'">ab '.$i.'</option>';
}?>
</select>
</div>
<input type="submit" value="<?php echo $GLOBALS['Lng']['apply']; ?>" name="submit" id="submit" class="field">
</form>
The Problem is that he want the entered option visible again after submit the form. In normal case if you send a form u got the default values. he tried to overwrite this with the php part "isset then echo".
AFAIK PHP you can't get the Text part of an select field, or?
What could he else to solve this?
If you wish to get "at 2011" you should set value of the option to
<option value="at 2011">at 2011</option>
As far as I understood, you want to keep value attribute, but get the text inside option tag.
$("#regdate option[value='2011']").text() will return "at 2011"
http://jsfiddle.net/BtNNT/5/

Simple PHP select option programming

I have a simple problem in php select option.
The page has 5 select option & submit button. When I select a option then it should goes on specific web page. For Example: http://onlinetools.org/tricks/using_multiple_select.php
Then I select a option & press Send then It show Which option I select. But I need go specific webpage.
I have tried with this code but I have failed...
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test[]" multiple="multiple">
<option value="http://google.com">one</option>
<option value="http://yahoo.com">two</option>
<option value="http://facebook.com">three</option>
<option value="http://who.is">four</option>
<option value="http://myspace.com">five</option>
</select>
<input type="submit" value="Send" />
</form>
<?php
$test=$_POST['test'];
echo "
<script type=\"text/javascript\">
window.location = \"$test\"
</script>
";
?>
Anybody Can help me?
This should be a simple select not a multiple one since you want to redirect to only one site. here is the code :
<?php
$test=$_POST['test'];
if(isset($test)){
header("Location: $test");
}
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test">
<option value="http://google.com">one</option>
<option value="http://yahoo.com">two</option>
<option value="http://facebook.com">three</option>
<option value="http://who.is">four</option>
<option value="http://myspace.com">five</option>
</select>
<input type="submit" value="Send" />
</form>
As you've declared:
<select name="test[]" multiple="multiple">
test is an array, thus you have to take the first object of the array or remove the unused []. By the way, you can do an HTTP redirection, which is faster and works more often than javascript ones.

Categories