Solved

Setting SELECTED value of a drop down list that is populated via a database lookup when posting a form back to itself

Posted on 2014-02-04
2
360 Views
Last Modified: 2014-02-04
Hello,

I am fairly new to PHP having come from a .NET background and one thing I am missing is the POSTBACK function of the forms and what that offered.

In essence what I am trying to do is have a user select two values from two drop down lists then use these two values and query the database to return the correct value.

If possible I would prefer to just have the one page and then retain the values that are selected on the page in the DDLs when the final value is displayed.

I know it is possible just to have the form action redirect to another page where I could show the values that have been selected but I would much prefer to keep the user not he one page and just keep the selected values int he DDL like I would have done in .NET.

After a search I have found options on setting the selected value but unless I am missing something with the syntax then these don't seem to be working wight eh way I am populating the menu.

I have attached the code I am using for the population of the DDLs below.

$conn = dbconn();

		$query = "SELECT width, mm, inch FROM tbl_width ORDER by mm ASC" or die("Error in the consult.." . mysqli_error($conn));

		$result = $conn->query($query);

		$return_height = "";

		while ($row = mysqli_fetch_array($result)){
			$return_height .= "<option value=\"" . $row['width'] . "\">" . $row['mm'] . " mm ( " . $row['inch'] . " inches )</option>";
		}

		return $return_height;

Open in new window


Many thanks.
0
Comment
Question by:Lee Redhead
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 

Accepted Solution

by:
Lee Redhead earned 0 total points
ID: 39833191
I have managed to find a solution to this by the following.

if (isset($sel_width)) {
          $selected = ($row['width'] == $sel_width ? 'selected' : '');
          }
		
			$return_height .= "<option value=\"" . $row['width'] . "\"" . $selected . ">" . $row['mm'] . " mm ( " . $row['inch'] . " inches )</option>";

Open in new window


This works by running the check outside of the complete line which I think was what I was doing wrong before.
0
 

Author Closing Comment

by:Lee Redhead
ID: 39833196
Applying some thought to what was going wrong setting the value as a variable outside of the return worked.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question