John Davidson

html - Comboboxes return null value in PHP

0 comments
Message:


Hi I have five (05) comboboxes in the form that fetch values from database but while editing form all comboboxes return no value. I have to select values in the combobox all the time to update form.
Here is my code:


if(isset($_POST['submit']))
{
$fname=validate($_POST['fname']);
$mname=validate($_POST['mname']);
$lname=validate($_POST['lname']);
$gender=validate($_POST['gender']);
$contact=validate($_POST['contact']);
$proid=validate($_POST['proid']);
$distid=validate($_POST['districtid']);
$degid=validate($_POST['degreeid']);
$batchid=validate($_POST['batchid']);
$majorid=validate($_POST['majorid']);
$desg=validate($_POST['designation']);
$inst=validate($_POST['institute']);
$add=validate($_POST['address']);
$userid=$_SESSION['id'];
$img=validate($_POST['featured_img']);
$oldimg=validate($_POST['old']);
$tmp_name = $_FILES['featured_img']['tmp_name'];
$dest = "alumni_images/";

if($img=='') {
$img=$oldimg;
$sql = "UPDATE alumnidata SET firstname='$fname',middlename='$mname',
lastname='$lname',gender='$gender',
address='$add',province_id='$proid',
district_id='$distid',contact='$contact',
degree_id='$degid',batch_id='$batchid',
major_id='$majorid',job_title='$desg',
connected_to='$inst',avatar='$img'
WHERE user_id='$userid'";
if (mysqli_query($con, $sql)) {
move_uploaded_file($tmp_name,$dest.$img);
$msg="Data has been updated successfully";

} else {
$error= "<b>in updating record:</b><br/>" . mysqli_error($con);
}
} else {
$sql = "UPDATE alumnidata SET firstname='$fname',middlename='$mname',
lastname='$lname',gender='$gender',
address='$add',province_id='$proid',
district_id='$distid',contact='$contact',
degree_id='$degid',batch_id='$batchid',
major_id='$majorid',job_title='$desg',
connected_to='$inst',avatar='$img'
WHERE user_id='$userid'";
if (mysqli_query($con, $sql)) {
move_uploaded_file($tmp_name,$dest.$img);
$msg="Data has been updated successfully";
} else {
$error= "<b>in updating record:</b><br/>" . mysqli_error($con);
}
}

Here is my HTML code of one combobox and similar code is for other 04 comboboxes. I have to choose value every time for updating form because if i don't do this, comboboxes return empty value:


<select name="proid" class="form-control" id="provinceid" required="required">
<option value="<?php echo $row['province_id'];?>" disabled selected><?php echo $row['province_name'];?></option>

<?php
$query1="Select * from tblprovinces";
$result1 = mysqli_query($con,$query1);
if(mysqli_num_rows($result1)>0) {
while($row1= mysqli_fetch_assoc($result1)) {
?>
<option value="<?php echo $row1['province_id']; ?>"><?php echo $row1['province_name']; ?></option>
<?php
}
}
?>
</select>


My Answer:
It seems like the issue might be related to how you are populating the comboboxes with values from the database.

In your HTML code, you are setting the default selected value of the combobox to `$row['province_id']`, which might be causing the issue when you try to update the form without selecting a new value.

To fix this issue, you can modify your HTML code to check if the value from the database matches the value from the combobox options and set the `selected` attribute accordingly. Here's an example:

html



By adding the `$selected` variable and checking if the value from the database matches the value from the combobox options, you can ensure that the correct value is selected by default when editing the form.

Make sure to apply this modification to all of your comboboxes in the form to ensure that the selected values are retained when updating the form.

Rate this post

3 of 5 based on 3224 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us