How to make a sort script with PHP - php

This is my HTML Code
<form action="sort.php" method="post" >
<select name="option">
<option value="1">Select a category!</option>
<option value="2">Category 1<option>
<option value="3">Category 2</option>
</select>
<select name="option">
<option value="a">Select a 2nd category!(Optional)</option>
<option value="b">Category 1</option>
<option value="c">Category 2</option>
</select>
This is my PHP Code:
<?php
if (isset($_POST['option']))
{
if ($_POST['option'] == '1') { header('Location: /website'); }
elseif ($_POST['option'] == '2') { header('Location: https://example.com/2'); }
elseif ($_POST['option'] == '2') { header('Location: https://example.com/2'); }
elseif ($_POST['option'] == '2') { header('Location: https://example.com/2'); }
elseif ($_POST['option'] == '3') { header('Location: https://example.com/3'); }
elseif ($_POST['option'] == '4') { header('Location: https://example.com/4'); }
elseif ($_POST['option'] == '5') { header('Location: https://example.com/5'); }
elseif ($_POST['option'] == '6') { header('Location: https://example.com/6'); }
}
?>
I want to make it so if someone select value "1" & value "a".Then X Link will open. And I want to make the 2nd button optional. If someone leaves it empty, it will not have any effect. How can I do so?

No need mutiple elseif
$arr =array(2,3,4,5,6);
if ($_POST['option'] == '1')
{
header('Location: /website');
}
elseif($_POST['option'] != '' && in_array($_POST['option'],$arr) )
{
header('Location: example.com/'.$_POST['option']);
}
else
{
echo "may be empty";
}
Note: change your input name and set required to first one
<select name="option" required>
<option value="">Select a category!</option>
<option value="1">Category 1<option>
<option value="2">Category 2</option>
<option value="3">Category 3</option>
<option value="4">Category 4</option>
<option value="5">Category 5</option>
<option value="6">Category 6</option>
</select>
<select name="option1">
<option values="">Select a 2nd category!(Optional)</option>
<option value="1">Category 1<option>
<option value="2">Category 2</option>
<option value="3">Category 3</option>
<option value="4">Category 4</option>
<option value="5">Category 5</option>
<option value="6">Category 6</option>
</select>

Related

PHP/HTML Showing selected option in dropdown from sql query

I wasn't sure how to title my question, I'm basically asking if anyone knows a better way to approach this. I'm using this function below to show which option is pulled from the DB, I'm wondering if there is a more compact way or better way to do this other than elseif each ID
function access_option($access)
{
if ($access == 0)
{
echo '<option value="0" selected>Member</option>
<option value="1">Streamer</option>
<option value="2">Moderator</option>
<option value="3">Manager</option>
<option value="4">Administrator</option>
<option value="5">Senior Administrator</option>';
}
elseif ($access == 1)
{
echo '<option value="0">Member</option>
<option value="1" selected>Streamer</option>
<option value="2">Moderator</option>
<option value="3">Manager</option>
<option value="4">Administrator</option>
<option value="5">Senior Administrator</option>';
}
elseif ($access == 2)
{
echo '<option value="0">Member</option>
<option value="1">Streamer</option>
<option value="2" selected>Moderator</option>
<option value="3">Manager</option>
<option value="4">Administrator</option>
<option value="5">Senior Administrator</option>';
}
elseif ($access == 3)
{
echo '<option value="0">Member</option>
<option value="1">Streamer</option>
<option value="2">Moderator</option>
<option value="3" selected>Manager</option>
<option value="4">Administrator</option>
<option value="5">Senior Administrator</option>';
}
elseif ($access == 4)
{
echo '<option value="0">Member</option>
<option value="1">Streamer</option>
<option value="2">Moderator</option>
<option value="3">Manager</option>
<option value="4" selected>Administrator</option>
<option value="5">Senior Administrator</option>';
}
elseif ($access == 5)
{
echo '<option value="0">Member</option>
<option value="1">Streamer</option>
<option value="2">Moderator</option>
<option value="3">Manager</option>
<option value="4">Administrator</option>
<option value="5" selected>Senior Administrator</option>';
}
}
Is this the best way to do it?
You can do it with less repetition using ternary operators:
function access_option($access)
{
echo '<option value="0"'.($access == 0 ? ' selected' : '').'>Member</option>';
echo '<option value="1"'.($access == 1 ? ' selected' : '').'>Streamer</option>';
echo '<option value="2"'.($access == 2 ? ' selected' : '').'>Moderator</option>';
echo '<option value="3"'.($access == 3 ? ' selected' : '').'>Manager</option>';
echo '<option value="4"'.($access == 4 ? ' selected' : '').'>Administrator</option>';
echo '<option value="5"'.($access == 5 ? ' selected' : '').'>Senior Administrator</option>';
}
You can loop over the values, which are held in an array, and set the selected value dynamically. This method makes it easy to add/remove options in the future by adjusting the array -
function access_option($access)
{
$options = ["Member", "Streamer", "Moderator", "Manager", "Administrator", "Senior Administrator"];
foreach($options as $key=>$value)
{
echo '<option value="'.$key.'"'.($key==$access ? ' selected' : '').'>'.$value.'</option>';
}
}
Ternary operators will reduce the size of it as mentioned as another answer. You can also use the switch statement. Similar to if and elseif but easier and will reduce your code.
Example:
<?php
function access_option($access)
{
switch ($access) {
case 0: echo "access equals 0";
case 1: echo "access equals 1";
case 2: echo "access equals 2";
}
}
?>

how to edit state and country list and update it using php

Hi i am storing countries and states in database i need to edit and update it in database but when i click on edit option iam not able to fetch the record from database.Here is my code.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://lab.iamrohit.in/js/location.js"></script>
Country:<select name="country" class="countries" id="country" value="<?php echo $row['country'];?>">
<option >Select Country</option>
</select><br/>
State:<select name="state" class="states" id="state" value="<?php echo $row['state'];?>">
<option >Select State</option>
</select><br/>
If i click on console the value is printing but iam not able to display in frontend.Can anyone help me regarding this.
please use this code.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://lab.iamrohit.in/js/location.js"></script>
Country:<select name="country" class="countries" id="country">
<option value="<?php echo $row['country'];?>"><?php echo $row['country'];?></option>
<option >Select Country</option>
</select><br/>
State:<select name="state" class="states" id="state">
<option value="<?php echo $row['state'];?>"><?php echo $row['state'];?></option>
<option >Select State</option>
</select><br/>
There were few things which were wrong.
Option should be inside select box
select value should be based on database condition
Thanks
Amit
Select box don't contain value attribute. You are going by wrong way.
You can use select box by this way:-
<select name="country">
<option value="country1" <?php if ($row['country'] == 'country1') { echo ' selected="selected"'; } ?>>country1</option>
<option value="country2" <?php if ($row['country'] == 'country2') { echo ' selected="selected"'; } ?>>country2</option>
<option value="country3" <?php if ($row['country'] == 'country3') { echo ' selected="selected"'; } ?>>country3</option>
</select>
<select name="state">
<option value="state1" <?php if ($row['state'] == 'state1') { echo ' selected="selected"'; } ?>>state1</option>
<option value="state2" <?php if ($row['state'] == 'state2') { echo ' selected="selected"'; } ?>>state2</option>
<option value="state3" <?php if ($row['state'] == 'state3') { echo ' selected="selected"'; } ?>>state3</option>
</select>
Hope it will help you :)
You can't assign value attribute to Select Box.So you can use Like this:
<select name="country">
<option value="country" <?php if ($row['country'] == 'country') { echo ' selected="selected"'; } ?>>country</option>
<select name="state">
<option value="state12" <?php if ($row['state'] == 'state12') { echo ' selected="selected"'; } ?>>state12</option>

Limit of number of checks in a PHP If statement

I'm new to PHP and HTML I have several other IF statements with two or three OR in it and they seem to work just fine.
What I'm checking for is 'NULL', 'blank', 'space' or text 'None'. If I find any of these I want to do the select statement. If no matches I would like to use the $AT_Med_1_1 data which has been pull out of a database.
IF statement as shown below which does not see me to work. It always drops into the SELECT statement even when $AT_Med_1_1 is something else.
if ($AT_Med_1_1 == '' or $AT_Med_1_1 == NULL or $AT_Med_1_1 == ' ' or $AT_Med_1_1 = 'None') {
<select name="AT_Med_1_2">
<option value="None">None</option>
<option value="Oral_Zofran">Oral Zofran</option>
<option value="IV_Zofran">IV_Zofran</option>
<option value="Oxygen">Oxygen</option>
<option value="Ibuprofen">Ibuprofen</option>
<option value="Aetaminothen">Aetaminothen</option>
<option value="Upinephrne">Upinephrne</option>
<option value="Xopenex">Xopenex</option>
<option value="Albuterol">Albuterol</option>
<option value="Valium">Valium</option>
<option value="Magnesiunsulphate">Magnesiunsulphate</option>
<option value="Diphenhyamine">Diphenhyamine</option>
<option value="Ketorolac">Ketorolac</option>
<option value="Promethazion">Promethazion</option>
<option value="Oral_Fluids_Soup">Oral Fluids Soup</option>
<option value="Oral_Fluids_Electrolyte">Oral Fluids Electrolyte</option>
<option value="Oral_Fluids_Water">Oral Fluids Water</option>
</select>
<?php
} else {
// echo 'Showing $AT_';
echo $AT_Med_1_2;
echo nl2br("\n");
}
?>
If I change the IF statement to several elseif it seem to work correctly
if ($AT_Med_1_1 == '')
{
goto test1;
} else if ($AT_Med_1_1 == NULL)
{
goto test1;
} else if ($AT_Med_1_1 == ' ')
{
goto test1;
} else if ($AT_Med_1_1 == 'None')
{
goto test1;
}
goto test;
test1:
?>
<select name="AT_Med_1_1">
<option value="None">None</option>
<option value="Oral_Zofran">Oral Zofran</option>
<option value="IV_Zofran">IV_Zofran</option>
<option value="Oxygen">Oxygen</option>
<option value="Ibuprofen">Ibuprofen</option>
<option value="Aetaminothen">Aetaminothen</option>
<option value="Upinephrne">Upinephrne</option>
<option value="Xopenex">Xopenex</option>
<option value="Albuterol">Albuterol</option>
<option value="Valium">Valium</option>
<option value="Magnesiunsulphate">Magnesiunsulphate</option>
<option value="Diphenhyamine">Diphenhyamine</option>
<option value="Ketorolac">Ketorolac</option>
<option value="Promethazion">Promethazion</option>
<option value="Oral_Fluids_Soup">Oral Fluids Soup</option>
<option value="Oral_Fluids_Electrolyte">Oral Fluids Electrolyte</option>
<option value="Oral_Fluids_Water">Oral Fluids Water</option>
</select>
<?php
goto endtest;
test:
echo $AT_Med_1_1;
echo nl2br("\n");
endtest:
if (... or $AT_Med_1_1 = 'None') {
For the last condition you accidentally wrote = instead of ==.

validate options[] with isset

Hey i am having some trouble getting an options[] array to work, if anyone can help that will be great
Form
<form method="post" action="array2.php">
<select name="options[]">
<option value=""></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>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<input name="submit" type="submit" value="submit">
</form>
The array2.php
<?php
session_start();
if(isset($_POST['submit'])){
if($_POST['options[]'] == "" ){
header("Location: error.html");
exit;
}else{
$checked = $_POST['options'];
$_SESSION['checked'] = $checked;
}
}
?>
Any help would be great, also what happens is even if it's an empty feild, the thing progress's to the rest of the script
rest of script
<?php
for($i=0; $i < count($checked); $i++){
echo "You have selected to recive " . $checked[$i] . " tickets<br/>";
}
for($i=0; $i < count($checked2); $i++){
echo "And you have selected to recive " . $checked2[$i] . " for accommodation are you sure? <br/>";
}
?>
Sorry I am unable to reply to people for now soon as I posted it a class came to the empty room so need to wait an hour :/
You need the change your options[] to option as it mean you are submitting multiple select with the same name
<form method="post" action="array2.php">
<select name="options">
<option value=""></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>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<input name="submit" type="submit" value="submit">
</form>
in your array2.php file
<?php
session_start();
if(isset($_POST['submit'])){
if($_POST['options'] == "" ){
header("Location: error.html");
exit;
}else{
$checked = $_POST['options'];
$_SESSION['checked'] = $checked;
}
}
?>
if you really need to send options[]
<?php
session_start();
if(isset($_POST['submit'])){
if(is_array($_POST['options']){
if($_POST['options'][0] == "" ){
header("Location: error.html");
exit;
}else{
$checked = $_POST['options'][0];
$_SESSION['checked'] = $checked;
}
}else{
if($_POST['options'] == "" ){
header("Location: error.html");
exit;
}else{
$checked = $_POST['options'];
$_SESSION['checked'] = $checked;
}
}
}
?>
you can clean this if else as you like
You can use this example code
<?php
if($_POST) {
if(isset($_POST['state'])) {
if($_POST['state'] == 'NULL') {
echo '<p>Please select an option from the select box.</p>';
}
else {
echo '<p>You have selected: <strong>', $_POST['state'], '</strong>.</p>';
}
}
}
?>
and your html code
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<legend>Please select a state</legend>
<select name="state">
<option value="NULL">-- Please select a state --</option>
<option value="AK">AK - Alaska</option>
<option value="AL">AL - Alabama</option>
<option value="WY">WY - Wyoming</option>
</select>
<input type="submit" name="submit">
</fieldset>
</form>
in your case options[] can be used for sending multiple fields as array, the index starts with 0, for example:
<input name="test[]"> : index 0
<input name="test[]"> : index 1
then, you can get these values in $_POST['test'] like this:
$input_one = $_POST['test'][0];
$input_one = $_POST['test'][1];
if you look at this inside $_POST it will be like this:
$_POST = array (
...,
'test' => array(0=> ..., 1 => ...)
)
for your form, if you only had one options[], then the value is the $_POST will be
if(isset($_POST['options'][0])){
}
So let's clean it up
<select name="options">
You do not need options[].
And then the result of $_POST['options'] will be the value of option. And add there <option value="0">Select</option> and then check whether the POSTed data is higher then 0
in html
<select name="options">
<option value="0"></option>
<option value="1">1</option>
.
.
in php
if(isset($_POST['options']) && !empty($_POST['options'])) {
$checked = $_POST['options'];
}

JQuery/PHP: Replace a select box with another select box on a different page

On my main page, I have a simple dropdown box:
<select name="miles" id="miles">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="75">75</option>
<option value="100">100</option>
</select>
<button type="button" name="button" id="button">Search</button>
When this button is clicked, it performs a JQuery $.load:
$("#miles").load("main_data.php #miles_data");
And here the page button is calling, main_data.php:
<?php
... code to calculate $b
?>
<span id="miles_data">
<option value="5" <?php if ($b == 5) { echo "selected"; } ?>>5</option>
<option value="10" <?php if ($b == 10) { echo "selected"; } ?>>10</option>
<option value="15" <?php if ($b == 15) { echo "selected"; } ?>>15</option>
<option value="25" <?php if ($b == 25) { echo "selected"; } ?>>25</option>
<option value="50" <?php if ($b == 50) { echo "selected"; } ?>>50</option>
<option value="75" <?php if ($b == 75) { echo "selected"; } ?>>75</option>
<option value="100" <?php if ($b == 100) { echo "selected"; } ?>>100</option>
</span>
My goals seem simple: Execute a $.load to another page, calculate an integer and set it to a variable, select the matching dropdown item and return it to the main page.
With my code above, the dropdown box on the main page becomes blank. Does anyone notice a solution?
One possible issue is that the format for a selected 'option' is:
<select>
<option>Volvo</option>
<option selected="selected">Saab</option>
<option>Mercedes</option>
<option>Audi</option>
</select>
Blatantly ripped from here
So you want to have
<option value="5" <?php if ($b == 5) { echo 'selected="selected"'; } ?>>5</option>

Categories