ianhaney Posted October 31, 2017 Share Posted October 31, 2017 (edited) Hi I need bit of help regarding dropdown values when editing a record using php. I edit a record using a html form and php to process the form and it saves all ok in the database but when I edit the record, it shows the first select dropdown value within the form instead of the value when the form saves to the database Can anyone help please, I have added the coding below <?php ini_set('display_startup_errors',1); ini_set('display_errors',1); error_reporting(-1); ?> <?php /* Allows the user to both create new records and edit existing records */ // connect to the database include("connect-db.php"); // creates the new/edit record form // since this form is used multiple times in this file, I have made it a function that is easily reusable function renderForm($customer_name = '', $customer_email = '', $customer_phone = '', $computer_make = '', $computer_model = '', $technician = '', $status = '', $exrdate = '', $exrtime = '', $exstdate = '', $exstime = '', $deltype = '', $comments = '', $job_cost = '', $part_cost = '', $profit = '', $error = '', $id = '', $send_sms = '') { ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title> <?php if ($id != '') { echo "Edit Repair Tracking"; } else { echo "New Repair Tracking"; } ?> </title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <link rel="stylesheet"href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/blitzer/jquery-ui.css"/> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script> <link rel="stylesheet" type="text/css" media="screen" href="css/styles.css" /> <script src="js/jquery.ui.timepicker.js"></script> <link rel="stylesheet" type="text/css" media="screen" href="css/jquery.ui.timepicker.css" /> <script> $(function() { $("#exrdate").datepicker({ showButtonPanel: true, dateFormat: "yy/mm/dd", showOn:"both" }); }); $(function() { $("#exstdate").datepicker({ showButtonPanel: true, dateFormat: "yy/mm/dd", showOn:"both" }); }); </script> <script> $(document).ready(function() { $('#exrtime').timepicker({ defaultTime: '12:00', showLeadingZero: true, showNowButton: true, showCloseButton: true, showDeselectButton: true, showOn: 'both', }); $('#exstime').timepicker({ defaultTime: '12:00', showLeadingZero: true, showNowButton: true, showCloseButton: true, showDeselectButton: true, showOn: 'both', }); }); </script> <!--<script src="//cdn.ckeditor.com/4.5.5/full/ckeditor.js"></script>--> <script type="text/javascript"> function doCalc() { var job_cost = parseFloat(document.getElementById('job_cost').value); var part_cost = parseFloat(document.getElementById('part_cost').value); var profit = job_cost - part_cost; profit = profit.toFixed(2); document.getElementById('profit').value = profit; } </script> </head> <body> <div id="logo"> <img src="https://www.it-doneright.co.uk/admin/repairs-tracking/images/logo/it-done-right.jpg" alt="" title=""> </div> <? session_start(); if($_SESSION['user']==''){ header("Location:../index.php"); }else{ include("../config.php"); $sql=$dbh->prepare("SELECT * FROM users WHERE id=?"); $sql->execute(array($_SESSION['user'])); while($r=$sql->fetch()){ echo "<div class='home-content'>"; echo "<center><h2>Hello, ".$r['username']."</h2>"; echo "<a href='../logout.php'>Log Out</a> <br><br> <a href='../index.php'>Home</a></center>"; echo "</div>"; echo "<br>"; } } ?> <?php include("nav-menu.php"); ?> <h1><?php if ($id != '') { echo "Edit Repair"; } else { echo "New Repair"; } ?></h1> <?php if ($error != '') { echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error . "</div>"; } ?> <div style="float: left;"> <u>NOTES</u> <br><br> <strong> ENTER MOBILE WITH 44 IN FRONT AND <br> NO SPACES (E.G. 447538503276) </strong> </div> <form action="" method="post" class="basic-grey"> <div> <?php if ($id != '') { ?> <input type="hidden" name="id" value="<?php echo $id; ?>" /> <p>Repair ID: <?php echo $id; ?></p> <?php } ?> <br> <strong>Customer Name:</strong> <input type="text" name="customer_name" value="<?php echo $customer_name; ?>"/> <br/> <strong>Customer Email:</strong> <input type="text" name="customer_email" value="<?php echo $customer_email; ?>"/> <br> <strong>Customer Phone:</strong> <input type="text" name="customer_phone" value="<?php echo $customer_phone; ?>"/> <?php if($send_sms == 1):?> <br /><strong style='color:red;'>SMS ALREADY SENT</strong><br /><br /> <?php else:?> <br> <strong>Send SMS Confirmation:</strong> <input type="checkbox" name="send_sms" value="yes"/> <br /><br /> <?php endif;?> <strong>Computer Make:</strong> <input type="text" name="computer_make" value="<?php echo $computer_make; ?>"/> <br> <strong>Computer Model:</strong> <input type="text" name="computer_model" value="<?php echo $computer_model; ?>"/> <br> <strong>Assigned to Technician:</strong> <select name="technician"> <option value="Ian Haney">Ian Haney</option> </select> <br> <strong>Repair Status:</strong> <select name="status"> <option value="In Queue">In Queue</option> <option value="Working on">Working on</option> <option value="Awaiting Parts">Awaiting Parts</option> <option value="Ready for Collection/Delivery">Ready for Collection/Delivery</option> <option value="Complete">Complete</option> <option value="Unable To Repair">Unable To Repair</option> </select> <br> <strong>Expected Start Date:</strong> <input type="text" name="exstdate" value="<?php echo $exstdate; ?>" id="exstdate" /> <br><br> <strong>Expected Start Time:</strong> <input type="time" name="exstime" value="<?php echo $exstime; ?>" id="exstime"/> <br><br> <strong>Expected Repair Date:</strong> <input type="text" name="exrdate" value="<?php echo $exrdate; ?>" id="exrdate"/> <br><br> <strong>Expected Repair Time:</strong> <input type="time" name="exrtime" value="<?php echo $exrtime; ?>" id="exrtime"/> <br><br> <strong>Delivery Type:</strong> <select name="deltype"> <option value="Customer Pickup">Customer Pickup</option> <option value="Delivery">Delivery</option> </select> <br> <strong>Comments:</strong> <br> <textarea name="comments"><?php echo $comments; ?></textarea> <br> <strong>Job Repair Cost:</strong> <input type="text" name="job_cost" id="job_cost" value="<?php echo $job_cost; ?>" onBlur="doCalc(this.form)" /> <br> <strong>Parts Cost:</strong> <input type="text" name="part_cost" id="part_cost" value="<?php echo $part_cost; ?>" onblur="doCalc(this.form)" /> <br> <strong>Profit:</strong> <input type="text" name="profit" id="profit" value="<?php echo $profit; ?>" /> <br> <input type="submit" name="submit" value="Add/Update Repair Tracking" /> </div> </form> </body> </html> <?php } /* EDIT RECORD */ // if the 'id' variable is set in the URL, we know that we need to edit a record if (isset($_GET['id'])) { // if the form's submit button is clicked, we need to process the form if (isset($_POST['submit'])) { // make sure the 'id' in the URL is valid if (is_numeric($_POST['id'])) { // get variables from the URL/form $id = $_POST['id']; $customer_name = htmlentities($_POST['customer_name'], ENT_QUOTES); $customer_email = htmlentities($_POST['customer_email'], ENT_QUOTES); $customer_phone = htmlentities($_POST['customer_phone'], ENT_QUOTES); $computer_make = htmlentities($_POST['computer_make'], ENT_QUOTES); $computer_model = htmlentities($_POST['computer_model'], ENT_QUOTES); $technician = htmlentities($_POST['technician'], ENT_QUOTES); $status = htmlentities($_POST['status'], ENT_QUOTES); $exrdate = htmlentities($_POST['exrdate'], ENT_QUOTES); $exrtime = htmlentities($_POST['exrtime'], ENT_QUOTES); $exstdate = htmlentities($_POST['exstdate'], ENT_QUOTES); $exstime = htmlentities($_POST['exstime'], ENT_QUOTES); $deltype = htmlentities($_POST['deltype'], ENT_QUOTES); $comments = htmlentities($_POST['comments'], ENT_QUOTES); $job_cost = htmlentities($_POST['job_cost'], ENT_QUOTES); $part_cost = htmlentities($_POST['part_cost'], ENT_QUOTES); $profit = htmlentities($_POST['profit'], ENT_QUOTES); // check that firstname and lastname are both not empty if ($customer_name == '' || $customer_phone == '' || $computer_make == '' || $computer_model == '' || $comments == '') { // if they are empty, show an error message and display the form $error = 'ERROR: Please fill in all required fields!'; renderForm($customer_name, $customer_phone, $computer_make, $computer_model, $comments, $error, $id); } else { // if everything is fine, update the record in the database if ($stmt = $mysqli->prepare("UPDATE repairs SET customer_name = ?, customer_email = ?, customer_phone = ?, computer_make = ?, computer_model = ?, technician = ?, status = ?, exrdate = ?, exrtime = ?, exstdate = ?, exstime = ?, deltype = ?, comments = ?, job_cost = ?, part_cost = ?, profit = ? WHERE id=?")) { $stmt->bind_param("ssssssssssssssssi", $customer_name, $customer_email, $customer_phone, $computer_make, $computer_model, $technician, $status, $exrdate, $exrtime, $exstdate, $exstime, $deltype, $comments, $job_cost, $part_cost, $profit, $id); $stmt->execute(); $stmt->close(); } // show an error message if the query has an error else { echo "ERROR: could not prepare SQL statement."; } // redirect the user once the form is updated header("Location: view-repairs-tracking.php"); } } // if the 'id' variable is not valid, show an error message else { echo "Error!"; } } // if the form hasn't been submitted yet, get the info from the database and show the form else { // make sure the 'id' value is valid if (is_numeric($_GET['id']) && $_GET['id'] > 0) { // get 'id' from URL $id = $_GET['id']; // get the recod from the database if($stmt = $mysqli->prepare("SELECT id, customer_name, customer_email, customer_phone, computer_make, computer_model, technician, status, exrdate, exrtime, exstdate, exstime, deltype, comments, job_cost, part_cost, profit, send_sms FROM repairs WHERE id=?")) { $stmt->bind_param("i", $id); $stmt->execute(); $stmt->bind_result($id, $customer_name, $customer_email, $customer_phone, $computer_make, $computer_model, $technician, $status, $exrdate, $exrtime, $exstdate, $exstime, $deltype, $comments, $job_cost, $part_cost, $profit, $send_sms); $stmt->fetch(); // show the form renderForm($customer_name, $customer_email, $customer_phone, $computer_make, $computer_model, $technician, $status, $exrdate, $exrtime, $exstdate, $exstime, $deltype, $comments, $job_cost, $part_cost, $profit, NULL, $id, $send_sms); $stmt->close(); } // show an error if the query has an error else { echo "Error: could not prepare SQL statement"; } } // if the 'id' value is not valid, redirect the user back to the view.php page else { header("Location: view-repairs-tracking.php"); } } } /* NEW RECORD */ // if the 'id' variable is not set in the URL, we must be creating a new record else { // if the form's submit button is clicked, we need to process the form if (isset($_POST['submit'])) { $country_code = '44'; // get the form data $customer_name = htmlentities($_POST['customer_name'], ENT_QUOTES); $customer_email = htmlentities($_POST['customer_email'], ENT_QUOTES); $customer_phone = htmlentities($_POST['customer_phone'], ENT_QUOTES); $computer_make = htmlentities($_POST['computer_make'], ENT_QUOTES); $computer_model = htmlentities($_POST['computer_model'], ENT_QUOTES); $technician = htmlentities($_POST['technician'], ENT_QUOTES); $status = htmlentities($_POST['status'], ENT_QUOTES); $exrdate = htmlentities($_POST['exrdate'], ENT_QUOTES); $exrtime = htmlentities($_POST['exrtime'], ENT_QUOTES); $exstdate = htmlentities($_POST['exstdate'], ENT_QUOTES); $exstime = htmlentities($_POST['exstime'], ENT_QUOTES); $deltype = htmlentities($_POST['deltype'], ENT_QUOTES); $comments = htmlentities($_POST['comments'], ENT_QUOTES); $job_cost = htmlentities($_POST['job_cost'], ENT_QUOTES); $part_cost = htmlentities($_POST['part_cost'], ENT_QUOTES); $profit = htmlentities($_POST['profit'], ENT_QUOTES); // check that firstname and lastname are both not empty if ($customer_name == '' || $computer_make == '' || $computer_model == '' || $comments == '') { // if they are empty, show an error message and display the form $error = 'ERROR: Please fill in all required fields!'; renderForm($customer_name, $computer_make, $computer_model, $comments, $error); } else { // insert the new record into the database if ($stmt = $mysqli->prepare("INSERT repairs (customer_name, customer_email, customer_phone, computer_make, computer_model, technician, status, exrdate, exrtime, exstdate, exstime, deltype, comments, job_cost, part_cost, profit) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) { $stmt->bind_param("ssssssssssssssss", $customer_name, $customer_email, $customer_phone, $computer_make, $computer_model, $technician, $status, $exrdate, $exrtime, $exstdate, $exstime, $deltype, $comments, $job_cost, $part_cost, $profit); $stmt->execute(); $stmt->close(); if(isset($_POST['send_sms'])): $repair_id = $mysqli->insert_id; $SMS = "Hello ".$customer_name.". Your repair has been booked in with IT Done Right. Your Repair ID is ".$repair_id.". To track your repair, please visit https://www.it-doneright.co.uk/track-my-repair FROM IT Done Right"; function sendSMS($username, $password, $customer_phone, $message, $originator) { $URL = 'https://api.textmarketer.co.uk/gateway/'."?username=$username&password=$password&option=xml"; $URL .= "&to=$customer_phone&message=".urlencode($message).'&orig='.urlencode($originator); $fp = fopen($URL, 'r'); return fread($fp, 1024); } $from = ''; $response = sendSMS('', '', $customer_phone, $SMS, $from); if ($stmt = $mysqli->prepare("UPDATE repairs SET send_sms = 1 WHERE id=$repair_id")) { $stmt->execute(); $stmt->close(); } // show an error message if the query has an error else { echo "ERROR: could not prepare SQL statement."; } endif; } // show an error if the query has an error else { echo "ERROR: Could not prepare SQL statement."; } $to = "$customer_email"; $subject = "Booked Repair Information"; $message = " <html> Hello <strong>$customer_name</strong> <h2>Below is your repair details to track the repair status</h2> <p>Your Name: $customer_name</p> <p>Your Email: $customer_email</p> <p>Your Phone Number: $customer_phone</p> <p>PC/Laptop Make: $computer_make</p> <p>PC/Laptop Make: $computer_model</p> <p>Assigned to Technician: $technician</p> <p>Current Repair Status: $status</p> <p>Expected Start Date: " . date('d/m/Y', strtotime($exstdate)) . "</p> <p>Expected Start Time: $exstime</p> <p>Expected Repair Date: " . date('d/m/Y', strtotime($exrdate)) . "</p> <p>Expected Repair Time: $exrtime</p> <p>Dropoff or Pickup: $deltype</p> <p>Comments: $comments</p> <p>Job Repair Cost: " . '£' . $job_cost . "</p> <p>Repair Job ID: " . mysqli_insert_id($mysqli) . " <strong>(Enter this ID number into link below)</strong></p> <p>https://www.it-doneright.co.uk/track-my-repair.php</p> </html> "; // Always set content-type when sending HTML email $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; $headers .= 'From: <enquiries@it-doneright.co.uk>' . "\r\n"; mail($to,$subject,$message,$headers); // redirec the user header("Location: view-repairs-tracking.php"); } } // if the form hasn't been submitted yet, show the form else { renderForm(); } } // close the mysqli connection $mysqli->close(); ?> Thank you in advance UPDATE: Would something like the following work on each option value? <option value="In Queue" <?php if ($row['status'] == 'In Queue') 'selected = "selected"'; ?>>In Queue</option> Edited October 31, 2017 by ianhaney Quote Link to comment https://forums.phpfreaks.com/topic/305509-select-dropdown-value-keeps-changing-on-edit/ Share on other sites More sharing options...
Sepodati Posted October 31, 2017 Share Posted October 31, 2017 <strong>Repair Status:</strong> <select name="status"> <option value="In Queue">In Queue</option> <option value="Working on">Working on</option> <option value="Awaiting Parts">Awaiting Parts</option> <option value="Ready for Collection/Delivery">Ready for Collection/Delivery</option> <option value="Complete">Complete</option> <option value="Unable To Repair">Unable To Repair</option> </select>Where exactly are you applying any PHP logic to set the option value? Quote Link to comment https://forums.phpfreaks.com/topic/305509-select-dropdown-value-keeps-changing-on-edit/#findComment-1553262 Share on other sites More sharing options...
ianhaney Posted October 31, 2017 Author Share Posted October 31, 2017 Is that in the following I think EDIT RECORD */ // if the 'id' variable is set in the URL, we know that we need to edit a record if (isset($_GET['id'])) { // if the form's submit button is clicked, we need to process the form if (isset($_POST['submit'])) { // make sure the 'id' in the URL is valid if (is_numeric($_POST['id'])) { // get variables from the URL/form $id = $_POST['id']; $customer_name = htmlentities($_POST['customer_name'], ENT_QUOTES); $customer_email = htmlentities($_POST['customer_email'], ENT_QUOTES); $customer_phone = htmlentities($_POST['customer_phone'], ENT_QUOTES); $computer_make = htmlentities($_POST['computer_make'], ENT_QUOTES); $computer_model = htmlentities($_POST['computer_model'], ENT_QUOTES); $technician = htmlentities($_POST['technician'], ENT_QUOTES); $status = htmlentities($_POST['status'], ENT_QUOTES); $exrdate = htmlentities($_POST['exrdate'], ENT_QUOTES); $exrtime = htmlentities($_POST['exrtime'], ENT_QUOTES); $exstdate = htmlentities($_POST['exstdate'], ENT_QUOTES); $exstime = htmlentities($_POST['exstime'], ENT_QUOTES); $deltype = htmlentities($_POST['deltype'], ENT_QUOTES); $comments = htmlentities($_POST['comments'], ENT_QUOTES); $job_cost = htmlentities($_POST['job_cost'], ENT_QUOTES); $part_cost = htmlentities($_POST['part_cost'], ENT_QUOTES); $profit = htmlentities($_POST['profit'], ENT_QUOTES); // check that firstname and lastname are both not empty if ($customer_name == '' || $customer_phone == '' || $computer_make == '' || $computer_model == '' || $comments == '') { // if they are empty, show an error message and display the form $error = 'ERROR: Please fill in all required fields!'; renderForm($customer_name, $customer_phone, $computer_make, $computer_model, $comments, $error, $id); } else { // if everything is fine, update the record in the database if ($stmt = $mysqli->prepare("UPDATE repairs SET customer_name = ?, customer_email = ?, customer_phone = ?, computer_make = ?, computer_model = ?, technician = ?, status = ?, exrdate = ?, exrtime = ?, exstdate = ?, exstime = ?, deltype = ?, comments = ?, job_cost = ?, part_cost = ?, profit = ? WHERE id=?")) { $stmt->bind_param("ssssssssssssssssi", $customer_name, $customer_email, $customer_phone, $computer_make, $computer_model, $technician, $status, $exrdate, $exrtime, $exstdate, $exstime, $deltype, $comments, $job_cost, $part_cost, $profit, $id); $stmt->execute(); $stmt->close(); } // show an error message if the query has an error else { echo "ERROR: could not prepare SQL statement."; } // redirect the user once the form is updated header("Location: view-repairs-tracking.php"); } } // if the 'id' variable is not valid, show an error message else { echo "Error!"; } } // if the form hasn't been submitted yet, get the info from the database and show the form else { // make sure the 'id' value is valid if (is_numeric($_GET['id']) && $_GET['id'] > 0) { // get 'id' from URL $id = $_GET['id']; // get the recod from the database if($stmt = $mysqli->prepare("SELECT id, customer_name, customer_email, customer_phone, computer_make, computer_model, technician, status, exrdate, exrtime, exstdate, exstime, deltype, comments, job_cost, part_cost, profit, send_sms FROM repairs WHERE id=?")) { $stmt->bind_param("i", $id); $stmt->execute(); $stmt->bind_result($id, $customer_name, $customer_email, $customer_phone, $computer_make, $computer_model, $technician, $status, $exrdate, $exrtime, $exstdate, $exstime, $deltype, $comments, $job_cost, $part_cost, $profit, $send_sms); $stmt->fetch(); // show the form renderForm($customer_name, $customer_email, $customer_phone, $computer_make, $computer_model, $technician, $status, $exrdate, $exrtime, $exstdate, $exstime, $deltype, $comments, $job_cost, $part_cost, $profit, NULL, $id, $send_sms); $stmt->close(); } // show an error if the query has an error else { echo "Error: could not prepare SQL statement"; } } // if the 'id' value is not valid, redirect the user back to the view.php page else { header("Location: view-repairs-tracking.php"); } } } Quote Link to comment https://forums.phpfreaks.com/topic/305509-select-dropdown-value-keeps-changing-on-edit/#findComment-1553264 Share on other sites More sharing options...
mac_gyver Posted October 31, 2017 Share Posted October 31, 2017 UPDATE: Would something like the following work on each option value? yes, something like that logic would work. however, what you should be doing is dynamically building the list of options, by making an array of the choices or even better, storing the choices in a database table, then looping over the choices to build the output, so that you don't need to repeat logic for each choice. by using a loop, you would only have that particular line of logic once in your code for each select menu. Quote Link to comment https://forums.phpfreaks.com/topic/305509-select-dropdown-value-keeps-changing-on-edit/#findComment-1553265 Share on other sites More sharing options...
ianhaney Posted October 31, 2017 Author Share Posted October 31, 2017 Oh right ok, sorry not 100% on PHP, how would it look in regards to storing the choices in a database table and then looping over the choices to build the output? Quote Link to comment https://forums.phpfreaks.com/topic/305509-select-dropdown-value-keeps-changing-on-edit/#findComment-1553267 Share on other sites More sharing options...
ianhaney Posted October 31, 2017 Author Share Posted October 31, 2017 I tried the coding below <select name="status"> <option value="In Queue" <?php if ($row['status'] == 'In Queue') 'selected = "selected"'; ?>>In Queue</option> <option value="Working on" <?php if ($row['status'] == 'Working On') 'selected = "selected"'; ?>>Working on</option> <option value="Awaiting Parts" <?php if ($row['status'] == 'Awaiting Parts') 'selected = "selected"'; ?>>Awaiting Parts</option> <option value="Ready for Collection/Delivery" <?php if ($row['status'] == 'Ready for Collection/Delivery') 'selected = "selected"'; ?>>Ready for Collection/Delivery</option> <option value="Complete" <?php if ($row['status'] == 'Complete') 'selected = "selected"'; ?>>Complete</option> <option value="Unable To Repair" <?php if ($row['status'] == 'Unable To Repair') 'selected = "selected"'; ?>>Unable To Repair</option> </select> buy gives the following error Notice: Undefined Variable: row I am not sure what it should be, I changed it to $status instead of $row but got a illegal string error Quote Link to comment https://forums.phpfreaks.com/topic/305509-select-dropdown-value-keeps-changing-on-edit/#findComment-1553268 Share on other sites More sharing options...
ianhaney Posted October 31, 2017 Author Share Posted October 31, 2017 UPDATE: I have solved it and also managed to do the array of choices, it seems to work when I tested it, I now have the following coding <?php $selection=array('In Queue','Working on','Awaiting Parts','Ready for Collection/Delivery','Complete','Unable To Repair'); echo ' <strong>Repair Status:</strong> <select name="status"> <option value="0">Please Select Option</option>'; foreach($selection as $selection){ $selected=($status == $selection)? "selected" : ""; echo '<option '.$selected.' value="'.$selection.'">'.$selection.'</option>'; } echo '</select>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/305509-select-dropdown-value-keeps-changing-on-edit/#findComment-1553270 Share on other sites More sharing options...
taquitosensei Posted November 1, 2017 Share Posted November 1, 2017 Just a note here on your original code. <option value="In Queue" <?php if ($row['status'] == 'In Queue') 'selected = "selected"'; ?>>In Queue</option> you need to echo the selected part. <option value="In Queue" <?php if ($row['status'] == 'In Queue') echo 'selected = "selected"'; ?>>In Queue</option> Quote Link to comment https://forums.phpfreaks.com/topic/305509-select-dropdown-value-keeps-changing-on-edit/#findComment-1553284 Share on other sites More sharing options...
Psycho Posted November 1, 2017 Share Posted November 1, 2017 (edited) Your code can't work because of this foreach($selection as $selection){ You are doing a foreach on $selection and immediately redefining $selection. I am assuming you changed the variable names from what was working for you. But, you are on the right track by using code to create the options. But, I would recommend you create a function to create your select options. You can use a generic function to create the options for ANY select list. Such a function could look like this function createOptions($optionsAry, $useLabelAsValue = false, $selectedValue = false) { $optionsHtml = ''; foreach($optionsAry as $value => $label) { if($useLabelAsValue) { $value = $label; } $selected = ($value===$selectedValue) ? " selected='selected'" : ''; $optionsHtml .= "<option value='{$value}'{$selected}>{$label}</option>\n"; } return $optionsHtml; } The function above has two "modes" based on how you need the lists created. In many cases the label for the option is not the value. For example, if you are pulling a list of records from a database, the value will likely need to be the database id of those records. But, for usage, you want the label and the value to be the same, so you would set the 2nd parameter to false when calling the function. EDIT: Fixed typos. Edited November 2, 2017 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/305509-select-dropdown-value-keeps-changing-on-edit/#findComment-1553287 Share on other sites More sharing options...
benanamen Posted November 1, 2017 Share Posted November 1, 2017 (edited) You have typos here. $optionsHtml .= "<option value='$value{}'{$selected}>{$label}</option>\n"; Edited November 1, 2017 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/305509-select-dropdown-value-keeps-changing-on-edit/#findComment-1553290 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.