i am trying to write validation that checks too see if the option picked matches the regex. There is 10 options, with only one of the options being wrong.
So far iv tried using if and else statements combined with "preg_match" and "$matches" but i cant seem to get it to work for select options. Here is my code and what iv been trying so far.
My Select options on form:
<h3>Standard</h3>
<label for="Adu">Adults</label>
<span class="error">* <?php echo $staErr;?></span>
<select name="seats[STA]" id="seatsSTA">
<option value="0">Choose one from below</option>
<option value="1">1xTicket</option>
<option value="2">2xTicket</option>
<option value="3">3xTicket</option>
<option value="4">4xTicket</option>
<option value="5">5xTicket</option>
<option value="6">6xTicket</option>
<option value="7">7xTicket</option>
<option value="8">8xTicket</option>
<option value="9">9xTicket</option>
</select>
My attempt at the regex:
$staErr = "";
$seatsSTA = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["seatsSTA"])) {
$staErr = "no match";
} else {
$seatsSTA = test_input($_POST["seatsSTA"]);
// check if name only contains letters and whitespace
if (preg_match("/\b1xTICKET\b/i",$seatsSTA)) {
$staErr = "matches";
}
}
}
It should Echo "matches" if any of the tickets options are selected (1xTicket, 2xTicket,...) and it should echo "no match" if any other option is picked (in this case it will only be "Choose one from below").
First of all your input name is incorrect. You are using seats[STA] in the form and seatsSTA in PHP.
Second Your pattern is matching only for number 1 when it should match all numbers from 1-9 like this /\b[1-9]xTICKET\b/i
Related
In my case I use select and php code, everything works perfectly when I call the function but when printing the results, I get the following value for the echo, please for your help and opinion.
<select name="cars" id="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="without_car">Without a car</option>
</select>
if($cars == "without_car") {
$cars .= "You dont have a car.";
} else {
$cars .= "You have a car.";
}
I get the answer by calling through html:
'.$cars.'
The result if you choose "Without a car":
without_carYou dont have a car.
or
The result if you choose "Volvo":
volvoYou have a car.
How to forbid value to appear and only echo result to be seen?
I need the result to be as follows:
You have a car.
or
You dont have a car.
Instead of concatenation reassign value of the variable:
if($cars == "without_car") {
$cars = "You dont have a car.";
} else {
$cars = "You have a car.";
}
My aim is to get the selected item from the drop down and covert it to type string. After conversion, am comparing the result with another string and then use if() statement to execute some code. But the conversion does not seem to work because the program only execute else() statement. How can I do this? somebody help Please.
Below is my html and php code.
<select name="p_category">
<option selected disabled >-- select --</option>
<option value="computers">Computer</option>
<option value="phones" >Phone</option>
<option value="accessories">Accessory</option>
<option value="general">Display</option>
</select>
<?php
$getCategory = $_POST['p_category'];
$value = strval($getCategory);
$comps = "computer";
if($value == $comps){
echo "<script>alert('Equal')</script>";
}else{
echo "<script>alert('Not equal')</script>";
}
?>
I am currently working on a project where a user chooses an option from a select box and submits a form, the form is then processed by PHP, and the PHP code determines what the select box value is, and does something based on that value.
My select box is called combined_group and has two select values: philharmonic_orchestra and symphony_orchestra.
This is how I am checking the selected value:
if($_POST['combined_group'] == "philharmonic_orchestra"){
$_SESSION['semesterprice'] = "170";
$_SESSION['fullprice'] = "330";
}
if($_POST['combined_group'] == "symphony_orchestra"){
$_SESSION['semesterprice'] = "275";
$_SESSION['fullprice'] = "530";
}
But when PHP runs through this code, neither if statement is chosen. I know that the value of $_POST['combined_group'] is, in fact, either of those two values, just PHP isn't picking it up for some reason.
Anybody care to help?
EDIT: My HTML form code is as follows
<select name="combined_group" class="OBJ-1">
<option value="" selected="">Select One</option>
<option value="philharmonic_orchestra">Philharmonic Orchestra</option>
<option value="symphony_orchestra ">Symphony Orchestra</option>
</select>
Client side
<select name="combined_group">
<option value="">Select an option</option>
<option value="philharmonic_orchestra">Philharmonic Orchestra</option>
<option value="symphony_orchestra">Symphony Orchestra</option>
</select>
Server side
if (! isset($_POST["combined_group"]))
{
exit('not set');
}
if (trim($_POST["combined_group"]) == '')
{
exit('not selected');
}
if (trim($_POST["combined_group"]) == 'philharmonic_orchestra')
{
//business logic for 'philharmonic_orchestra'
}
else
{
//business logic for 'symphony_orchestra'
}
Most likely is a bad HTML syntax. Check if your option item has value attribute:
<option value="...">...</option>
The reason why your conditional statement is failing, is because of a space in your option's value.
<option value="symphony_orchestra ">
^ right there
What you will need to do is remove it:
<option value="symphony_orchestra">
^ deleted space
Technical sidenote:
Had your conditional statement been:
if($_POST['combined_group'] == "symphony_orchestra ")
^ notice the space
with the space before the quote, it would have worked.
Anything between quotes is considered and part of an element's value.
I have a standard select box in my php form for my website containing a list of counties in the UK, an example of which is shown below:
<p class='form_title'>County<br>
<select id="county" name="county">
<optgroup label="England">
<option>Bedfordshire</option>
<option>Berkshire</option>
<option>Bristol</option>
....
<option>Wiltshire</option>
<option>Worcestershire</option>
</optgroup>
<optgroup label="Wales">
<option>Anglesey</option>
...
<option>Radnorshire</option>
</optgroup>
<optgroup label="Scotland">
<option>Aberdeenshire</option>
...
<option>Wigtownshire</option>
</optgroup>
<optgroup label="Northern Ireland">
<option>Antrim</option>
...
<option>Tyrone</option>
</optgroup>
</select>
</p>
Once a user has submitted details, they can then edit them.
I therefore need a way to have selected the option that they previously chose, given that I have the option they chose saved in word form, such as Bedfordshire.
I know I need to add the word selected to one of the options, but I was wondering if there was a better way to do it than a massive case statement.
Thanks
You should set an option value for each option to track the value.
Also you should be generating that from a database and then you can set selected based on a $_GET or $_POST:
for example:
foreach($county as $name){
$selected = '';
if($name == $_GET['name']){
$selected = ' selected="selected"';
}
echo '<option value="'.$name.'"'.$selected.'>'.$name.'</option>';
}
this doesn't include your optgroup but that also should come from database.
Try something like this:
foreach($places as $place) {
$selected = $prevSelectionId == $place['id'] ? " selected='selected' " : "";
echo "<option $selected value='".$place['id']."'>".$place['name']."</option>";
}
</select>
I think i don't get it. I tried to parse some html page :
<select name="sel">
<option value="val0">-please select me-</option>
<option value="val1">some selection</option>
</select>
Im using the Simple_html_dom class :
foreach($html->find('select') as $s) {
if ($html->find('option',1) != false) {
$tempoption = $html->find('option',1)->plaintext;
echo $tempoption; //shows 'some selection'
}
}
but, then if i simply use this line :
$value='';
if ( $tempoption == 'some selection')
$value='79';
echo $value; //doesn't shows anything (empty variable?)
//or this one :
if ( strcmp($tempoption, 'some selection'))
$value='79';
echo $value; //Nope.
I tried out the code just as you had it there and $value DID end up with 79. But I think that you must have more to your HTML than that tiny snippet. The thing in your code that stood out most to me was the fact that you are wrapping the conditionals in a loop that goes through all the selects of the HTML, but you are not making use of the individual select elements that you get from the loop, so the foreach loop seems pointless..
Have a look at this:
foreach($html->find('select option') as $o)
{
$o_label = $o->plaintext;
if($o_label=='some selection') echo 'PERFECT MATCH: '.$o_label."\n";
else if(stripos($o_label, 'some selection')!==false) echo 'LOOSE MATCH: '.$o_label."\n";
else echo 'DOES NOT MATCH: '.$o_label."\n";
}
When given HTML:
<select name="sel">
<option value="val0">-please select me-</option>
<option value="val1">1 some selection</option>
<option value="val1">This does not match</option>
<option value="val1">some selection</option>
<option value="val1">Another non-matching label</option>
<option value="val1">some selection 3</option>
<option value="val1">One more that does not match</option>
<option value="val1">4 SoME sElEcTIon 4</option>
</select>
The output will be:
DOES NOT MATCH: -please select me-
LOOSE MATCH: 1 some selection
DOES NOT MATCH: This does not match
PERFECT MATCH: some selection
DOES NOT MATCH: Another non-matching label
LOOSE MATCH: some selection 3
DOES NOT MATCH: One more that does not match
LOOSE MATCH: 4 SoME sElEcTIon 4