PHP If statement in html form - php

I have the following form but would like to add a condition so that only managers can see 'clear' option.
<form action="vision_ref.php" method="post">
With selected:
<select name='multi'>
<option value='now'>Set next action time to now</option>
<option value='clear'>Remove the ticket</option>
<option value='unassign'>Unassign Specialist</option>
<option value='priority'>Toggle Priority</option>
</select>
<input type="submit" value="Go!">
</form>
Something like: if(havePriv('grp_mgr'))

<?php if(havePriv('grp_mgr')) : ?>
<option value='clear'>Remove the ticket</option>
<?php endif; ?>
Does the trick

You can do this:
<form action="vision_ref.php" method="post">
With selected:
<select name='multi'>
<option value='now'>Set next action time to now</option>
<?php if(havePriv('grp_mgr')){ ?>
<option value='clear'>Remove the ticket</option>
<?php } ?>
<option value='unassign'>Unassign Specialist</option>
<option value='priority'>Toggle Priority</option>
</select>
<input type="submit" value="Go!">
</form>
So "Remove the ticket" will only displayed if the condition is true.

You can say :
if(havePriv('grp_mgr'))
{
echo "<option value='clear'>Remove the ticket</option>";
}

Related

How do I get value of <select> when a user select a item

I need help.
I have a select box with items and I want, if the user select a car name and click on "search" I need to have the name of the car he selected on a "echo".
I tried couple of things but it displays nothing (blank page).
below is my code.
Thanks in advance !
<select name="category" >
<option value="nocategory">No Category</option>
<option value="toyota">Toyota</option>
<option value="nissan">Nissan</option>
<option value="mazda">Mazda</option>
<option value="volvo">Volvo</option>
</select>
<input name="submit" value="Search" type="submit">
</form>
<?php
if (isset($_POST['submit'])){
$category = $_POST['category'];
?>
Set form action to $_SERVER['PHP_SELF'] like below: $_SERVER['PHP_SELF'] is a PHP super global variable which returns the filename of the currently executing script.
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="category" >
<option value="nocategory">No Category</option>
<option value="toyota">Toyota</option>
<option value="nissan">Nissan</option>
<option value="mazda">Mazda</option>
<option value="volvo">Volvo</option>
</select>
<input name="submit" value="Search" type="submit">
</form>
<?php
if (isset($_POST['submit'])){
$category = $_POST['category'];
echo $category;
}
?>

How do i get the select tag value with codeigniter?

how do i get the select tag value, i am using the syntax below but not working.
Controller
'role' => $this->input->post('rol'));
View
these are the options in my select tag
<select name="rol">
<option value="Employee">Employee</option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
$role = $this->input->post("rol");
Just try it to post like below:
<form action="your_controller_action" method="POST">
<select name="rol" id="pos_select" class="form_input">
<option value="Employee">Employee </option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
<input type="submit" name="submit" value="Post it">
</form>
Then in your controller:
public function youControllerAction() {
if( $this->input->post) {
echo $this->input->post("rol");
}
}
You will get the selected option value.
how did you write your selection dropdown list!did you take it as i posted below?
<select name="rol" id="pos_select" class="form_input">
<option value="Employee">Employee </option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
$role = $this->input->post("rol");
put this code in view. Only you have to call controller function in action. In this example I am getting option values from database
<form method="post" action="<?php echo base_url()."main/project_filter_skills"; ?>">
<select class="demo-default " id="skilltags" name="skilltags[]" data-placeholder="Choose skills">
<option value="">Choose skills</option>
<?php foreach ($all_skill_tags as $skilltags) { ?>
<option value="<?php echo $skilltags->secondCat_id;?>">
<?php echo $skilltags->secondCat_title;?>
</option>
<?php } ?>
</select>
</form>
now put this in modal or controller to get the vlaue
$skilltags= $this->input->post('skilltags');

how to connect drop down list with button and php file with if statement

I'm new in php, and I need help. I have created drop down list, than I should hit submit button and it should run one of my php files that are in drop down list. (using if statement) I tried in this way:
<p>
What Genre you want?
<select name="Ganre">
<option value="">Select...</option>
<option value="FPS">FPS</option>
<option value="JRPG">JRPG</option>
<option value="RPG">RPG</option>
<option value="Sports">Sports</option>
</select>
<?php if (Genre == "FPS"): ?>
<FORM METHOD="LINK" ACTION="FPS.php">
<INPUT TYPE="submit" VALUE="Submit">
</FORM>;
</p>
Use your code like this:
<?php if (Genre == "FPS") { ?>
<form method="get" action="FPS.php">
<select name="Genre">
<option value="">Select...</option>
<option value="FPS">FPS</option>
<option value="JRPG">JRPG</option>
<option value="RPG">RPG</option>
<option value="Sports">Sports</option>
</select>
<input type="submit" value="Submit" />
</form>
<?php } ?>
You can get values in PHP by using PHP superglobal (POST/GET):
<?php
if(isset($_GET['Ganre'])){
echo $_GET['Ganre'];
// use your stuff that you want in IF condition.
}
?>
And if you need values in URL as sub string than you need to replace form method with $_GET:
Change:
<FORM METHOD="LINK" ACTION="FPS.php">
With:
<form method="get" action="yourfilename.php">

simplest php dropdown box

I have a simple dropdow box, I want to get the value of the selected item with php. If anyone can help me with it please.
<form id="s" method="post">
<select name="size">
<option value="small">Small</option>
<option value="medium">Medium</option>
<option value="large">Large</option>
</select>
<input type="submit" name="Submit" value="Send">
</form>
<?php
---
echo "selected size".$selected;
?>
Provided you put everything in one file:
<form id="s" method="post">
<select name="size">
<option value="small">Small</option>
<option value="medium">Medium</option>
<option value="large">Large</option>
</select>
<input type="submit" name="Submit" value="Send">
</form>
<?php
if(isset($_POST['size'])) {
echo "selected size: ".htmlspecialchars($_POST['size']);
}
?>
<select name="rider_select">
<option value="">Select Rider</option>
<?php
foreach ($riderlist as $rows) {
$rname = $rows['name'];
$rnum = $rows['number'];
$rider_full = $rname.'-'.$rnum;
?>
<option value="<?php echo $rname.'-'.$rnum; ?>"
<?= (($rider_fromdb == $rider_full) ? 'selected="selected"' : ''); ?>>
<?php echo $rname.'-'.$rnum; ?></option>
<?php } ?>
</select>

How to get multiple selected values of select box in php?

I have a html form which has a select list box from which you can select multiple values because its multiple property is set to multiple. Consider form method is 'GET'. The html code for the form is as follows:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="get" action="display.php">
<table width="300" border="1">
<tr>
<td><label>Multiple Selection </label> </td>
<td><select name="select2" size="3" multiple="multiple" tabindex="1">
<option value="11">eleven</option>
<option value="12">twelve</option>
<option value="13">thirette</option>
<option value="14">fourteen</option>
<option value="15">fifteen</option>
<option value="16">sixteen</option>
<option value="17">seventeen</option>
<option value="18">eighteen</option>
<option value="19">nineteen</option>
<option value="20">twenty</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" tabindex="2" /></td>
</tr>
</table>
</form>
</body>
</html>
I want to display the selected values in select list box on display.php page. So how are the selected values accessed on display.php page using $_GET[] array.
If you want PHP to treat $_GET['select2'] as an array of options just add square brackets to the name of the select element like this: <select name="select2[]" multiple …
Then you can acces the array in your PHP script
<?php
header("Content-Type: text/plain");
foreach ($_GET['select2'] as $selectedOption)
echo $selectedOption."\n";
$_GET may be substituted by $_POST depending on the <form method="…" value.
Change:
<select name="select2" ...
To:
<select name="select2[]" ...
You can use this code to retrieve values from multiple select combo box
HTML:
<form action="c3.php" method="post">
<select name="ary[]" multiple="multiple">
<option value="Option 1" >Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option>
<option value="Option 5">Option 5</option>
</select>
<input type="submit">
</form>
PHP:
<?php
$values = $_POST['ary'];
foreach ($values as $a){
echo $a;
}
?>
Use the following program for select the multiple values from select box.
multi.php
<?php
print <<<_HTML_
<html>
<body>
<form method="post" action="value.php">
<select name="flower[ ]" multiple>
<option value="flower">FLOWER</option>
<option value="rose">ROSE</option>
<option value="lilly">LILLY</option>
<option value="jasmine">JASMINE</option>
<option value="lotus">LOTUS</option>
<option value="tulips">TULIPS</option>
</select>
<input type="submit" name="submit" value=Submit>
</form>
</body>
</html>
_HTML_
?>
value.php
<?php
foreach ($_POST['flower'] as $names)
{
print "You are selected $names<br/>";
}
?>
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="get" action="display.php">
<table width="300" border="1">
<tr>
<td><label>Multiple Selection </label> </td>
<td><select name="select2[]" size="3" multiple="multiple" tabindex="1">
<option value="11">eleven</option>
<option value="12">twelve</option>
<option value="13">thirette</option>
<option value="14">fourteen</option>
<option value="15">fifteen</option>
<option value="16">sixteen</option>
<option value="17">seventeen</option>
<option value="18">eighteen</option>
<option value="19">nineteen</option>
<option value="20">twenty</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" tabindex="2" /></td>
</tr>
</table>
</form>
</body>
</html>
You can iterate it directly like this
foreach ($_GET['select2'] as $value)
echo $value."\n";
or you can do it like this
$selectvalue=$_GET['select2'];
foreach ($selectvalue as $value)
echo $value."\n";
This will display the selected values:
<?php
if ($_POST) {
foreach($_POST['select2'] as $selected) {
echo $selected."<br>";
}
}
?>
// CHANGE name="select2" TO name="select2[]" THEN
<?php
$mySelection = $_GET['select2'];
$nSelection = count($MySelection);
for($i=0; $i < $nSelection; $i++)
{
$numberVal = $MySelection[$i];
if ($numberVal == "11"){
echo("Eleven");
}
else if ($numberVal == "12"){
echo("Twelve");
}
...
...
}
?>
You could do like this too. It worked out for me.
<form action="ResultsDulith.php" id="intermediate" name="inputMachine[]" multiple="multiple" method="post">
<select id="selectDuration" name="selectDuration[]" multiple="multiple">
<option value="1 WEEK" >Last 1 Week</option>
<option value="2 WEEK" >Last 2 Week </option>
<option value="3 WEEK" >Last 3 Week</option>
<option value="4 WEEK" >Last 4 Week</option>
<option value="5 WEEK" >Last 5 Week</option>
<option value="6 WEEK" >Last 6 Week</option>
</select>
<input type="submit"/>
</form>
Then take the multiple selection from following PHP code below. It print the selected multiple values accordingly.
$shift=$_POST['selectDuration'];
print_r($shift);
I fix my problem with javascript + HTML. First i check selected options and save its in a hidden field of my form:
for(i=0; i < form.select.options.length; i++)
if (form.select.options[i].selected)
form.hidden.value += form.select.options[i].value;
Next, i get by post that field and get all the string ;-)
I hope it'll be work for somebody more. Thanks to all.
foreach ($_POST["select2"] as $selectedOption)
{
echo $selectedOption."\n";
}
form submit with enctype="multipart/form-data"

Categories