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.
Related
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 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
I put a first option in my select element just to tell the user what they are selecting, I think it's advisable to put a null first option, but I want to put an error message that the user didnt select anything if he is just selecting the first option which is the null option here's my code
<select class = "strand" name = "strand">
<option >=====Strand=====</option>
<option value ="GAS">GAS</option>
<option value ="ICT">ICT</option>
<option value ="STEM">STEM</option>
<option value ="HUMSS">HUMMSS</option>
<option value ="ABM">ABM</option>
</select>
I have 2 select lists, one naming products and another for quantities.
<select name="fish" id="fish">
<option value="blueFish">Blue Fish</option>
<option value="redFish">Red Fish</option>
<option value="pinkFish">Pink Fish</option>
<option value="greenFish">Green Fish</option>
</select>
<select name="numFish" id="numFish">
<option value="0">0</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>
</select>
I would like it so when a product is chosen, the corresponding quantity from the database will be set accordingly.
So if there was a record that customer Billy had bought 5 Pink Fish, when I select Pink Fish, the quantity select list will change to 5.
This would be for use in a PHP form using a MySQL database.
Is such functionality possible, and if so how would I go about doing it?
You might want to google for ajax requests. What it does is detecting a change (in your case) through javascript, send the value you selected to another php script which should do a sql query to return the quantity. The ajax request will catch the returned value and through javascript again change the value from the select dropdown.
All this would happen in the background and your site wouldn't refresh.
If you are not very used to javascript you can have a look at the jquery framework, which makes this task a bit easier, and with a lot of examples documented.
I didn't paste any code because assume you are not familiar with javascript/jquery/ajax. You might want to read a bit of documentation and play around a bit, and come back when you have a concrete problem, that would be the normal workflow here in Stackoverflow.
Edit: (some code as requested by OP)
Javascript:
$('#fish').change(function(){
var fishType = $('#fish select option:selected');
$.ajax("getQuantity.php", {fish: fishType}, function(data){
data = JSON.parse(data);
if(data.status == "OK"){
var quantity = data.quantity;
$('#numFish option[value='+quantity+']').prop("selected", true);
} else {
alert("error");// or console.log(), whatever you prefer
}
}
});
php (getQuantity.php):
<?php
$fish = $_POST['fish']; //getting the fish type
$sql = "your SQL to select the fish quantity for that type";
$result = mysqli_query($mysqli, $sql);
if(mysqli_num_rows($result)>0){
$row = mysqli_fetch_assoc($result);
$data = array("status" => "OK", "quantity" => $row['quantity']); // you can just output a string with the number if you want, but this way you have more control and better debugging.
echo json_encode($data);
}
?>
It's a basic code, it still would need to catch some errors for the database or return a different status. But basically that's it. I didn't even test that code so use it as a guideline only.
Change <select name="fish" id="fish"> to <select name="fish" id="fish" onchange="getQuantity(this.value);">
Declare the following function in javascript:
function getQuantity( o ) {
// get the quantity from the database using ajax,
// and set the quantity dropdown here.
}
I have a select list like the following inside of a form.
<select name="0[voicemail]" >
<option value="" selected="selected"></option>
<option value="800">800</option>
<option value="801">801</option>
<option value="802">802</option>
<option value="803">803</option>
<option value="805">805</option>
<option value="807">807</option>
<option value="808">808</option>
<option value="809">809</option>
<option value="810">810</option>
<option value="811">811</option>
<option value="820">820</option>
<option value="830">830</option>
<option value="831">831</option>
<option value="9778">9778</option>
<option value="9995">9995</option>
</select>
This was generated by some Kohana PHP code.
$id = 0;
$disabled = '';
foreach ( $line_detail as $line ){
echo '<tr class="d'.($id & 1).'">';
echo '<td>'.$line->did.form::hidden($id."[did]", $line->did).'</td>';
echo '<td>'.form::input($id."[cid_prefix]", $line->cid_prefix).'</td>';
echo '<td>'.$line->type.'</td>';
echo '<td>'.form::input($id."[ivr_context]", $line->ivr_context, "disabled='true'").'</td>';
if ($line->ivr_context != ''){
$disabled = "disabled='true'";
echo '<td>'.form::input(array('name'=>$id."[dial_timeout]", 'size'=>15,
'maxlength'=>3), $line->dial_timeout, $disabled).
form::hidden($id."[dial_timeout]", $line->dial_timeout).'</td>';
echo '<td>'.form::dropdown($id."[voicemail]", $phones, $line->voicemail, $disabled).
form::hidden($id."[voicemail]", $line->voicemail).'</td>';
} else {
echo '<td>'.form::input(array('name'=>$id."[dial_timeout]", 'size'=>15,
'maxlength'=>3), $line->dial_timeout, $disabled).'</td>';
echo '<td>'.form::dropdown($id."[voicemail]", $phones, $line->voicemail, $disabled).'</td>';
}
echo '<td>'.form::input($id."[notes]", $line->notes).'</td>';
echo "</tr>";
$id++;
}
Not everything shown, but basically the options are in the $phones variable.
Now the problem.
When I use a form submit all is fine until I choose the empty value in the submit.
This is inside a method where $detail is equivalent to $_POST
foreach($detail as $key => $val){
$this->db->query("UPDATE dids SET cid_prefix=?, dial_timeout=?, voicemail=?, notes=? WHERE did=?",
array($val['cid_prefix'],
$val['dial_timeout'],
$val['voicemail'],
$val['notes'],
$val['did']));
The problem here is that I set the value for the empty option to be "NULL", but because Kohana adds commas around everything it inserts it'll try to put "NULL" into the database instead of NULL. In this case this will violate a foreign key constraint.
Is there a simple way to deal with NULL in PHP/Kohana so that I don't have to check for blank and rewrite each query that might contain NULL more than one time.
What happens when you could get multiple valid NULLs? Surely there is a way to deal with these types of situations simply?
Placing this code before the database update seems to fix the problem. I.e. set $val['voicemail] to the PHP constant NULL.
if (empty($val['voicemail'])){
$val['voicemail'] = NULL;
}
Then I can continue to use the original SQL statement. Kohana seems to behave itself and NULL is set through in the update statement.
You can use the DB::expr method which will not escape the value in the query. That way you insert a raw NULL
However, I'm amazed you've used a object orientated framework and yet chucked out all the advantages by creating a mess of a form. Job security?