Array change input like if A change to B for example - php

Question: so i have a form with arrays that sends data to excel but i need the time that they put in the form to be : and not . is there a way i can check the array like
if $time = 00.00 change to 00:00 like that, can I do that ?
if not is there a way to lock the format of the input form so it only accepts 00:00 and if they put 00.00 they will get something that says its wrong ?
Edit:
i have a list but if they doesent use it
<input list="start" name="start[]" value="" class="listbox" />
<datalist id="start">
<option value="08:00"/>
<option value="08:30"/>
</datalist>

You can use an HTML5 pattern to confirm the input meets your requirements.
pattern="^\d+:\d+$"
This requires the field has numbers to start with, a colon, and then ends with numbers. You also should have a validation server side incase someone bypasses the check, or is using an older browser.
if(!preg_match('/^\d+:\d+$/', $_POST['field'])) {
echo 'Invalid input';
}
Regex demo: https://regex101.com/r/xp8IzI/1
Another client side option would be using a select in place of the datalist and input, that won't allow them to enter altering times though.
<select name="start[]" class="listbox">
<option value="08:00">08:00</option>
<option value="08:30">08:30</option>
</select>

Related

HTML Dropdown Default value not setting

I have a Dropdown that consists of two options. When I select an option the page reloads and using GET method I find out which value is selected. Then I try to make sure that the dropdown has the previously selected value and this is where things go wrong.
<?php echo $Status; ?> <!-- the value passed in the URL(stored in the variable Status) gets correctly printed here -->
<select class="btn btn-primary" required name="Status" style="float:right" onchange="location = this.value">
<option value="">Select Status</option>
<option value="?pageno=1&Status='Active'" <?php if($Status=='Active'){echo "selected";}?>>Active</option>
<option value="?pageno=1&Status='Not Active'" <?php if($Status=='Not Active'){echo "selected";}?>>Not Active</option>
</select>
The value gets printed correctly in the first line but for some reason, it isn't working in the options part. What am I doing wrong here? Can anyone clarify
The value of your URL parameter Status is either 'Active' or 'Not Active' - including those single quotes, they are part of the value that you are sending.
But what you are comparing it to, is just Active resp. Not Active:
if($Status=='Active')
The single quotes here are not part of the value that you are comparing $Status to - they are part of the PHP syntax. You would have to write something like
if($Status=="'Active'")
or
if($Status=='\'Active\'')
here, to properly compare the your variable with what it actually contains.
But that makes rather little sense to begin with - you should rather remove the single quotes from the parameter value you are sending.
<option value="?pageno=1&Status=Active"
<option value="?pageno=1&Status=Not+Active"
Note that I replaced the space with + here, to make this a properly URL encoded value.

Trying to Echo a value from a select drop down in PHP

I am in school for web development, so I clearly don't know a lot yet. I am trying to grab a value from a select, and output it in a paragraph. I know it's about the Browns, but everything else works, but I cannot seem to find anything on StackOverflow, or Google on how to grab the value that works. I get the following error:
Notice: Undefined variable: draft in C:\wamp\www\lab 5\process.php on line 42
This error is in my paragraph, which makes me think I am grabbing the value, but it isn't outputting correctly? I am using a variable to show where I want to output that value in the paragraph.
This is my HTML:
<li><select id="uDraft"
<select>
<option value="Draft Offense">Draft Offense</value>
<option value="Draft Defense">Draft Defense</value>
<option value="Trade them, we can't pick good anyways">Trade them, we can't pick good anyways</value>
</select></li>
This is my process.php code:
if(isset($_GET["uDraft"])){
$draft= $_GET["uDraft"];
}
echo "I want to output **$draft var** as the value they choose so it shows the choice in a paragraph that is pre-written";
All of my other text boxes work, I just cannot seem to get the value from the select, so that I can show what it says in the text, into the paragraph. I also had a select for wins, but gave up when I couldn't figure out how to grab the value. I know I can use radio buttons, but I am trying to learn how to grab the value from the drop down. Any help would be greatly appreciated.
You need to give your dropdown a name:
HTML:
<li>
<select name="uDraft">
<option value="Draft Offense">Draft Offense</option>
<option value="Draft Defense">Draft Defense</option>
<option value="Trade them, we can't pick good anyways">Trade them, we can't pick good anyways</option>
</select>
</li>
PHP:
if(isset($_GET["uDraft"])){
$draft= $_GET["uDraft"];
echo $draft;
}
1st you have simple html errors, closing the select tag early, no name on select and closing the options tags incorrectly. It should be:
<select id="uDraft" name="uDraft">
<option value="Draft Offense">Draft Offense</option>
<option value="Draft Defense">Draft Defense</option>
<option value="Trade them, we can't pick good anyways">Trade them, we can't pick good anyways</option>
</select>
Then in php, you need to handle the case when the page loads, so there is no get data:
if(isset($_GET["uDraft"])){
$draft= $_GET["uDraft"];
}else{
$draft = 'DEFAULT VALUE GOES HERE';
}
echo "I want to output **$draft var** as the value they choose so it shows";
This could be handled with a ternary if as well:
$draft = isset($_GET['uDraft'])? $_GET['uDraft'] : 'DEFAULT';

PHP - Store multiple select option into array and us the ID as array key

I have selectbox option list with multiple selection.
<select multiple="MULTIPLE" size="20" name="facilities[]">
<option id="idOne" value="Value One">Value One</option>
<option id="idTwo" value="Value Two">Value Two</option>
<option id="idThree" value="Value Three">Value Three</option>
</select>
I have done already the php code that can store the value into an Array on my database, but.. what I want now, is that possible to use the ID from the option as a key of the array, so when you storing the array will look like sample below
array('idOne'=>'Value One', 'idTwo'=>Value Two, 'idThree'=>'Value Three')
Instead of array([0]=>'Value One', [1]=>Value Two, [2]=>'Value Three') (that what I get now).
Any kind help will much appreciate :)
Sorry my English bad.
By definition, only the value of your option tags will be passed in your POST. There's a couple of options here
First, you would need to know the values on the other end and marry them up. This is more work potentially but you would always have a list of the values selected.
$vals = array('idOne'=>'Value One', 'idTwo'=>'Value Two', 'idThree'=>'Value Three');
foreach($vals as $key => $row) {
if(array_search($row, $_POST['facilities']) === false) unset($vals[$key]);
}
Second would be to use JavaScript and build a list of the text values that are selected and POST that (either via hidden field or using AJAX). This is riskier since the data would be in the client's hands but still doable.

Delete duplicates in dropdown list

I have hard coded and added items to dropdownlist ie teamsize as 1,2,3 like that.
When i load this dropdownlist for edit/update i get duplicate values like this
1
1
2
3
4... How do i eliminate this duplicate values?
please find the code below
<select name="anesthesia" id="selectAnesthesiaVal" style="width:25%;" class="required safe" AppendDataBoundItems = "false">
<option value="<?php echo isset($event)?$event->proc_anesthesia_type:"" ;?>"><?php echo isset($event)?$event->proc_anesthesia_type:"" ;?></option><option value="General">General</option>
<option value="Mac">Mac</option>
<option value="Spinal/Epidural">Spinal/Epidural</option>
<option value="Regional">Regional</option>
<option value="Local">Local</option>
<option value="Other">Other</option>
</select>
If you do not want to use jQuery for this then I would advise placing all of the possible values into an array and looping through them in PHP. Then if the value exists, only place it once.
In addition, if you would like to use jQuery and the PHP is not possible in your circumstances then let me know and I will post up some jQuery.
UPDATE
This will do the trick. I have clearly laid out comments to explain what is going on step by step. Hope this helps.
Please note that it would be much more efficient to do this in PHP
// Set the present object
var present = {};
$('#selectAnesthesiaVal option').each(function(){
// Get the text of the current option
var text = $(this).text();
// Test if the text is already present in the object
if(present[text]){
// If it is then remove it
$(this).remove();
}else{
// Otherwise, place it in the object
present[text] = true;
}
});
I suspect that line
<option value="<?php echo isset($event)?$event->proc_anesthesia_type:"" ;?>"><?php echo isset($event)?$event->proc_anesthesia_type:"" ;?></option>
add option which correspond to option choosen from select control, i e on first load you see coded list as this line returns empty option, but when you choose actual option this first one gets populated with choosen value
in this case you need to do 2 things
1 remove this line
2 add conditions to each line of hardcoded option and set it to select depending on the value of $event->proc_anesthesia_type
and because of 2nd tank you will end up with 6 almost identical conditional statements putting selected='selected' to each option
so in order to make the overall code looks pretty i recomend instead of hardcodding options add values to list or even better dictionary and check this condition in a loop

Grabbing all options from HTML select list

I need to create a php array from a very large HTML select list. I searched for "dropdown to array" generators and programs but can't find anything. Can someone please suggest a php code that I can use to grab all options from a HTML drop-down list and output an array of them?
update: php or js. Anything that will output an array which I can copy and paste in my script.
Include brackets in the select name; PHP will accept your selection as an array:
<?php print_r($_GET['cars']);?>
<form>
<select name="cars[]" multiple="multiple" size="5">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<input type="submit" />
</form>
Upon postback the print_r outputs (depending on selection):
Array ( [0] => saab [1] => mercedes )
If it's javascript:
document.getElementById(<!--the_id_of_the_select-->). Options and just treat it as an array that will create and append (document.createElement('input'), document.forms[0].appendChild(previous_item)) during the onsubmit stage of the forms process.
Alternatively in php:
When you do the loop for the option tags created in the select area, just do a second foreach and create a bunch of hidden elements somewhere else on the form.
Alternatively for both, instead of lots of hidden inputs (with a certain prefix), you could just create a single one that lists all the options in a csv format, or something similar

Categories