Trying to capture name= and value= US or NONE (if selected="selected" not included). Given the following (part of a larger $page):
$page = '
<select name="country" ><option value="NONE">Select One</option>
<option value="AE">UNITED ARAB EMIRATES</option>
<option value="GB">UNITED KINGDOM</option>
<option value="US" selected="selected">UNITED STATES</option>
<option value="UY">URUGUAY</option>
<option value="UZ">UZBEKISTAN</option>
<option value="ZW">ZIMBABWE</option></select>';
Edit 1 This pattern won't pick up NONE if none of the options are selected.
$pattern = '/select name="([a-zA-Z]*)"\s?>[\w\W]*value="([A-Z]{2,4})"(selected="selected">)?/'
So, I'm looking for a pattern that will.
The syntax for the if, then, else regex in php is:
(if 'selected=' found) then find pattern | else find this pattern
(if condition) then regex | else regex
(?(?=.*selected=)value="([A-Z]+)"\s+selected=|value="(NONE)">)
The .* is necessary for the condition to be true.
Does it have to be one regex ?
I would recommend splitting it up this way:
match the element
tryMatch with the "selected" attribute
proceed according to results - I'm not sure what the logic is as don't quite undestand from your description when you want which value. But either way it should be easy to get either value or name attributes.
The benefit of this approach is that you are working on a part of the html code and it's far less error prone.
EDIT:
BTW: how do you know it never evaluates to true ? It seems to work as intended (i.e. matching 'select' or 'select name="country") here http://www.pagecolumn.com/tool/pregtest.htm
(you need to escape the quotes (?(?=selected=\"selected\")select|select name=\"([a-zA-Z]*)\") to use it there)
EDIT2
I still won't be able to help you with if then else, but based on your explanation I can suggest this:
<select name="([^"]*)".*?(?!</select>)<option value="[^"]*" selected="selected"|<select name="([^"]*)"\s*>\s*<option value="([^"]*)">
so, tryMatch selected, if it fails match just the first option
Related
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.
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>
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';
I have this issue. I have HTML code stored in the database. I read it, and I display it in my pages using echo $page_content;
I want to add somewhere in the middle of this HTML code a php include. Something like this:
Inside this file I have also html code which is basically the country options for a select:
<option selected="selected" value="">Choose...</option>
<option value="AFGHANISTAN">AFGHANISTAN</option>
<option value="ALBANIA">ALBANIA</option>
<option value="ALGERIA">ALGERIA</option>
<option value="AMERICAN SAMOA">AMERICAN SAMOA</option>
<option value="ANDORRA">ANDORRA</option>
<option value="ANGOLA">ANGOLA</option>
etc etc
Until now I have hard-coded all the countries in many places, which I don't like.
But this is not displayed at all when i do my
echo $page_content;
One of the easiest ways is to use tokens that get replaced with the content.
This can avoid the use of eval - which is not recommended ane would get me down voted by the herd.
For example:
HTML CODE
Hello, _TOKEN2_!
You can then use string replace functions to replace the TOKEN2 (the underscores are there but seemed to disappear here) with whatever you want:
str_replace("_TOKEN2_","World",$page_content);
And so on, you get the idea.
I would recommend you look into using something like Smarty to separate you're PHP and layout.
I am processing files that contain HTML markup.
I need to get the selected option from the drop down box. In this case Australia is the selected option ..
<select name="cCountry" id="cCountry" style="width:200" tabindex="5">
<option value="251">Ascension Island</option>
<option selected="selected" value="14">Australia</option>
<option value="13">Austria</option>
Another scenario :
<select name="cBirthYearM" id="cBirthYearM">
<option value="1974">1974</option>
<option value="1975">1975</option>
<option value="1976">1976</option>
<option selected="selected" value="1977">1977</option>
<option value="1978">1978</option>
<option value="1979">1979</option>
In this case '1977" is the value that I need to extract as it's the selected option .
To make it clear I need to get the value from the markup not from user GET/POST input
The value specified in the value part of the option will be in $_GET ['cCountry'] or $_POST ['cCountry'] (depending if you're using GET or POST to submit the form).
It should be simple matter to look up the value from a lookup table or DB query.
EDIT: This question didn't make it clear if you meant how to get the value the user selected when the form is submitted, or how to get the value from the markup. If it's the latter, then you should look at DOMDocument.
If you wish to parse HTML in PHP then the preferred method is the DOM hierarchy of objects, especially DOMDocument. You can use Javascript-like methods of accessing the DOM tree such as getElementById to grab the select control (in this case getElementById ('cCountry')) and then examine its children (the options) to find the selected one and get its attributes.
It can take a little getting used to using the DOM objects but they're far more powerful for parsing and manipulating HTML than regex would be, so they're well worth learning.
http://php.net/manual/en/book.dom.php
http://php.net/manual/en/class.domdocument.php