-4

I have a select dropdown that is being populated and displayed correctly from a mysql query:

  <select name="title">
  <option></option>
  <?php
    while($row = mysql_fetch_assoc($title_data)) {
      echo '<option value=' . $row["title"];
      echo '>' . $row["title"];
      echo '</option>';
    } 
  ?>
  </select>

When I add code to check which row was selected, I get an undefined index error for each row in the query results:

  <select name="title">
  <option></option>
  <?php
    while($row = mysql_fetch_assoc($title_data)) {
      echo '<option value=' . $row["title"];
      if($_POST["title"] == $row["title"]) {
        echo ' selected';
      }
      echo '>' . $row["title"];
      echo '</option>';
    } 
  ?>
  </select>

I'm realtively new to this and may be missing something obvious. I had checked the posts and did not see this particular example where I am using a while loop from a query and trying to echo the selected attribute. The actual error text is inserted into the dropdown box:

Notice: Undefined index: title in C:\xampp\htdocs\indexdb.php on line 215 >Analyst I" and appears for each row in the dropdown box.

I apologize if this has been covered as I stated, this is my first php code and is a protype only. This is the only hurdle I have not been able to overcome.

chris85
  • 23,846
  • 7
  • 34
  • 51
sawmkw
  • 15
  • 6
  • 2
    Please include your code in the question. You should also consider switching from `mysql_*` to [`mysqli`](http://php.net/manual/en/book.mysqli.php) or [`PDO`](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers) as `mysql` is [officially](http://php.net/manual/en/migration55.deprecated.php) deprecated. You are not sanitizing your user input which could allow people to use [XSS](https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)) attacks to deface your site. Look in too [`htmlspecialchars`](http://php.net/manual/en/function.htmlspecialchars.php). – Script47 Jan 04 '16 at 19:06
  • @sawmkw It is best to include the code in the post or pastebin rather than an image. But code in post = A MUST pastebin = easier formatting so we can look at. – RepeaterCreeper Jan 04 '16 at 19:07
  • Check if `POST` is populated first. `!empty(`. I'd provide an example but I can't copy/paste part of an image. – chris85 Jan 04 '16 at 19:08
  • @RepeaterCreeper pastebin is not suggested either. The question is the best place for the code. – Script47 Jan 04 '16 at 19:08
  • @Script47 I was saying it could be possible to include it in pastebin. I've seen many post that includes the code but it looks like a pile of garbage and it's very hard to look at. Pastebin you can just copy and paste and it'll auto format. – RepeaterCreeper Jan 04 '16 at 19:10
  • The error should indicate what is undefined? – Rajdeep Paul Jan 04 '16 at 19:10
  • I realized that each of my drop down values has a space, such as "Clinical Nurse". I'm not picking up the entire value in the $_POST which in this example contains only "Clinical". I apologize for missing this. Thanks @chris85 for pointing me in that right direction. – sawmkw Jan 05 '16 at 14:02
  • @sawmkw what's the status here, does the answer below help? – chris85 Jan 07 '16 at 16:53

1 Answers1

0

You need to encapsulate the value attribute's data.

echo '<option value="' . $row["title"] . '" ';

Then to resolve the undefined index first check that the field is populated:

if(!empty($_POST["title"]) && $_POST["title"] == $row["title"]) {
chris85
  • 23,846
  • 7
  • 34
  • 51