undefined index error when update button is pressed? - php

i have here a select option and it work fine if i change the value of the option but when i try not to change anything and i pressed on the update button it gives me an error of undefined error. but if i change the value of a option it successfully updated. I really do not know why its giving me this error..
my code
<?php
include_once 'db.php';
$Reason_for_Deduction = isset($_GET['Summary_of_Reason']) ? $_GET['Reason_for_Deduction'] : '';
if(isset($_POST['update']))
{
$ID = $_GET['ID'];
$Reason_for_Deduction = $_POST['Summary_of_Reason'];
if($LTID->update($ID,$Summary_of_Reason))
{
echo "<script type='text/javascript'>alert('Successfully Updated!');</script>";
}
else
{
echo "<script type='text/javascript'>alert('Updating Failed!');</script>";
}
}
if(isset($_GET['ID']))
{
$ID = $_GET['ID'];
extract($LTID->getID($ID));
}
?>
update.php
<select name="Summary_of_Reason" id="yearapproved" class=form-control required/>
<option selected="true" disabled="disabled" <?php echo $Summary_of_Reason; ?>><?php echo $Summary_of_Reason; ?></option>
<option value="18% SLOPE ABOVE AND UNDEVELOPED">18% Slope and Undeveloped</option>
<option value="FIVE (5) HAS. AND BELOW">5 Has. and Below</option>
<option value="CANCELLED TITLE">Cancelled Title</option>
<option value="CANNOT BE LOCATED ON THE GROUND">Cannot Be Located on the Ground</option>
<option value="COMMUNAL FOREST">Communal Forest</option>
<option value="DISTRIBUTED BEFORE CARPER">Distributed Before Carper</option>
<option value="DUPLICATE LH">Duplicate Lh</option>
<option value="ERODED; SILTED/ROCKY NOT SUITABLE TO AGRICULTURAL">Eroded;Silted/Rockt not suitable to Agricultural</option>
<option value="HANDOG TITULO">Handog Titulo</option>
<option value="HOMESTEAD PATENT">Homestead Patent</option>
<option value="IRRIGATION CANAL">Irrigation Canal</option>
<option value="LANDS FOR PUBLIC USE">Lands for Public Use</option>
<option value="LH IS W/N SWAMPY, MANGROVE AREA">Lh is w/n Swampy, Mangrove Area</option>
<option value="LO DIED PRIOR TO CARP">LO Died Prior to CARP</option>
<option value="PASTURE LAND">Pasture Land</option>
<option value="ROAD LOT">Road Lot</option>
<option value="TIMBERLAND">Timberland</option>
<option value="USED FOR INFRASTRUCTURE">Used for Infrastructure</option>
<option value="W/N PROCLAIMED AREA">w/n Proclaimed Area</option>
<option value="W/N DANGER ZONE">w/n Danger Zone</option>
<option value="W/N UNCLASSIFIED PUBLIC FOREST">w/n Unclassified Public Forest</option>
<option value="W/N WATERSHED AREA">w/n Watershed Area</option>
<option value="WORKABLE">Workable</option>
</select>
class.php
public function update($ID,$Summary_of_Reason)
{
try
{
$stmt=$this->db->prepare("UPDATE rlbet SET Summary_of_Reason = :Summary_of_Reason WHERE ID = :ID");
$stmt->bindparam(":Summary_of_Reason",$Summary_of_Reason);
$stmt->bindparam(":ID",$ID);
$stmt->execute();
return true;
}
catch(PDOException $e)
{
echo $e->getMessage();
return false;
}
}

You should change this line:
<option selected="true" disabled="disabled" <?php echo $Summary_of_Reason; ?>><?php echo $Summary_of_Reason; ?></option>
to:
<option selected="selected" disabled="disabled" value="<?php echo $Summary_of_Reason; ?>"><?php echo $Summary_of_Reason; ?></option>
You are echoing the value of $Summary_of_Reason inside the option tag and not as its value and the main problem is selected = "true" needs to be changed to selected="selected".

Firstly you have to remove the / from the end of
Also your select box name is "Summary_of_Reason" then why you are using $_GET["Reason_for_Deduction"] in action page, You should use $_GET["Summary_of_Reason"]. "Reason_for_Deduction" is not available in form, this is the reason it gives you undefined index error.

Related

PHP: How to display select options if no value from database

I am trying to display my dropdown with a value from the database, but if the value is null I want it to show my options.
Currently it keeps showing me the blank select option.
<select class="form-control col-sm-5" id="freqlevels" name="freqlevels" value="<?php if ($customerinfo['freqlevel']) { echo h($customerinfo['freqlevel']);} else { echo "" ; } ?>"">
<option value=""></option>
<option value="Twice Weekly">Twice Weekly</option>
<option value="Weekly">Weekly</option>
<option value="Fortnightly">Fortnightly</option>
<option value="Monthly">Monthly</option>
</select>
Please can you suggest what I should do?
put your condition outside the value
<?php if ($customerinfo['freqlevel']) { echo value="$customerinfo['freqlevel']";}
hope this will resolve your problem
You need to make use of conditional statements.
<select name="something" id="my-select">
<option value="0">Everyone can see me</option>
<?php if (empty($array['some_key'])) : ?>
<option value="1">I'm only if some_key is empty</option>
..etc..
<?php endif; ?>
</select>
Then you can check values against the option value:
<option value="<?php echo $key; ?>"
<?php echo ($key === $_POST['some_key'] ? 'selected' : ''); ?>>
Hello, world
</option>

PHP/JQuery Dynamic Drop Down not sending variable via POST

I have a two step drop down menu. The second select is triggered with jquery. Since I have put this step in, my form does not pass the variable of the selected option. I expect it to just post but it doesn't. I have always had it loaded dynamically, so I didn't think that was the issue. I can't get it to work by passing it through the URL either.
Here is my code:
search.php
while($row = mysqli_fetch_array($result)) { ?>
<option value="<?=$row['id']?>"> <? echo $row['name'] ?></option>
<?php }
<form method = "post" action="index.php?page=users&id=<?php echo $row['id']; ?>">
<p>
<select name="list-select" id="list-select">
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
</select>
<?php
echo '<select name="list-target" id="list-target">';
echo '</select>';
$installation_id = $_POST['list-target'];
mysqli_close($con);
?>
users.php:
$installation_id = $_POST['list-target'];
.js
$(document).ready(function($) {
$("#list-select").change(function() {
$("#list-target").load("index.php?page=search&svalue=" + $("#list-select").val());
});
});
Thanks.

Displaying selected option from database in a select option tag

This is a simple one, i know it is i just cant think of a good logical way to do this.
I have the following code:
<select name="Title[]" class="form-control">
<option value="">Select title...</option>
<option value="Mr">Mr</option>
<option value="Miss">Miss</option>
<option value="Mrs">Mrs</option>
<option value="Ms">Ms</option>
<option value="Prof">Prof</option>
<option value="Doctor">Doctor</option>
</select>
What i want to do is on my edit client page, have their previously selected option displayed. So for example, if Doctor was selected at sign up, it would be selected by default on the edit page. I know i could do this like:
<option value="Doctor" <?php if($client_title == 'Doctor'){ echo 'selected'; } ?>>Doctor</option>
But it seems like a bit of a redundant way to do it. Can this be done easily with a do while statement?
Sorry for the simple request, having a bit of a slow day today! haha
<select name="title[]" class="form-control">
<?php
$titles = array('Mr', 'Miss', 'Mrs', 'Ms', 'Prof', 'Doctor');
foreach ($titles as $title) {
$selected = $client_title == $title ? ' selected="selected"' : null;
?>
<option value="<?php echo $title; ?>"<?php echo $selected; ?>><?php echo $title; ?></option>
<?php
}
?>
</select>

how to retrieve data in multiple selected list

i need to retrive data in mutiple selected list from database
in my database value is
Marathi,Arunachali,Assamese,Awadhi,Marathi,Arunachali,Assamese,Awadhi
i used the below code to retrive
<select id="spokenlanguages" name="known_languages[]" multiple="multiple" size="5">
<option value="" >Select</option>
<?php
$langs = $editdatapersonaltbl[0]['known_languages'] ;
$known_languages = explode(",",$langs);
//print_r($known_languages); exit();
for($i=0;$i < count($known_languages);$i++)
{
?>
<option value="<?php echo $known_languages[$i] ;?>" selected="selected"><?php echo $known_languages[$i] ;?></option>
<?php } ?>
<option value="Marathi" >Marathi</option>
<option value="Arunachali" >Arunachali</option>
<option value="Assamese" >Assamese</option>
<option value="Awadhi" >Awadhi</option>
<option value="Bengali" >Bengali</option>
<option value="Bhojpuri" >Bhojpuri</option>
<option value="Brij" >Brij</option>
<option value="Bihari" >Bihari</option>
</select>
but my problem is if marathi is previously selected than it displayed in selected and
not selected also
not selected because of <option value="Marathi" >Marathi</option> option is there if i will remove this option <option value="Marathi" >Marathi</option> than if marathi is not in database than what if user want to select marathi
please provide me some solutions....
Try this:
<select id="spokenlanguages" name="known_languages[]" multiple="multiple" size="5">
<option value="" >Select</option>
<?php
$langs = $editdatapersonaltbl[0]['known_languages'] ;
$known_languages = explode(",",$langs);
$known_languages = array_filter( $known_languages ); #remove the blank values if any
//print_r($known_languages); exit();
?>
<option value="Marathi" <?php if(in_array('Marathi', $known_languages)){echo 'selected="selected"';}?> >Marathi</option>
<option value="Arunachali" <?php if(in_array('Arunachali', $known_languages)){echo 'selected="selected"';}?>>Arunachali</option>
<option value="Assamese" <?php if(in_array('Assamese', $known_languages)){echo 'selected="selected"';}?>>Assamese</option>
<option value="Awadhi" <?php if(in_array('Awadhi', $known_languages)){echo 'selected="selected"';}?>>Awadhi</option>
<option value="Bengali" <?php if(in_array('Bengali', $known_languages)){echo 'selected="selected"';}?>>Bengali</option>
<option value="Bhojpuri" <?php if(in_array('Bhojpuri', $known_languages)){echo 'selected="selected"';}?>>Bhojpuri</option>
<option value="Brij" <?php if(in_array('Brij', $known_languages)){echo 'selected="selected"';}?>>Brij</option>
<option value="Bihari" <?php if(in_array('Bihari', $known_languages)){echo 'selected="selected"';}?>>Bihari</option>
</select>
first u must put html options in array as html_aarray , then cho database array . after that in another while eho html option if the node of html_aarray not exists in database array.
i wrote it 4 u , use this :http://codepad.org/gjWW9UGg
<?php
//$lang_array is ur language array
$lang_array = array('Marathi','Hindi','Gujrati','Bengali');
//this array ehich u fetching from DB
$known_languages = array('Marathi', 'Gujrati');
?>
<select id="spokenlanguages" name="known_languages[]" multiple="multiple" size="5">
<option value="" >Select</option>
<?php
for($i=0;$i < count($lang_array); $i++)
{
if(in_array($lang_array[$i], $known_languages))
{
$str = 'selected="selected"';
}
else
{
$str ='';
}
echo
'<option value="'.$lang_array[$i].'" '.$str.'> '.$lang_array[$i].'</option>';
}
?>

Set the default value of drop-down list with the last value chosen

I'm using cakephp 1.2, and I have a search form which has also this menu:
Classificazione <select style="margin-top: 5px;" name="classificazione">
<option value="art0"></option>
<option value="C">Articoli</option>
<option value="D">Documentazione</option>
<option value="A">Libri</option>
<option value="G">Materiali</option>
<option value="B">Riviste</option>
<default value="A">
</select><br />
In the next page I want to set the default value of this menu with what the user has chosen before.
I SOLVED like this (for example, with the first option):
In the controller:
$getParams['classificazione'] = isset($params['classificazione']) ? $params['classificazione'] : '';
...
$this->set('getParams', $getParams);
In the view:
<option value="C" <?php if ($getParams['classificazione']=="C") echo "selected"; ?> >Articoli</option>
Save the value in a session variable and use that to echo selected for that option
<?php
function is_selected($selected_option, $list_option_value) {
if($selected_option == $list_option_value) {
return 'selected';
}
}
?>
<select>
<option <?php echo is_selected($_SESSION['selected_option'], '1'); ?>>1</option>
</select>

Categories