4 Replies - 6172 Views - Last Post: 21 September 2010 - 01:28 PM Rate Topic: -----

#1 eksquall  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-April 09

PHP PDO/SQLITE - Reading selected Dropdown value

Posted 21 September 2010 - 06:29 AM

Hi,

I have populated a dropdown menu using php pdo from sqlite database. How would i go about reading what the user has selected in the dropdown menu. so i can do something like:

$userSelected = [whatever user selected in the dropdown menu];


my code is as follows (part of it):

<form name="placementStudents" method="post"><br />
  
<select name = 'studentrow'>    

<?php 

$db = new PDO('sqlite:mydb.sqlite');


$getStudents= $db ->query("SELECT this part works");

$rowarray2 = $getStudents->fetchAll();
$studentrow = 0;

foreach($rowarray2 as $row)
{
echo "<option value = $studentrow>$row[studentID] $row[firstName] $row[lastName]</option>";
$studentrow++;
}
?>

<input type="submit" value="Select Student">
</select>
</form>


(removed the sql select statement as it was quite long, but it does work)


Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: PHP PDO/SQLITE - Reading selected Dropdown value

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2927
  • View blog
  • Posts: 10,114
  • Joined: 08-August 08

Re: PHP PDO/SQLITE - Reading selected Dropdown value

Posted 21 September 2010 - 07:12 AM

Use $_POST['select name here'] or $_GET['select name here'] depending on how you've set up your form.
Was This Post Helpful? 1
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3520
  • View blog
  • Posts: 10,159
  • Joined: 08-June 10

Re: PHP PDO/SQLITE - Reading selected Dropdown value

Posted 21 September 2010 - 07:13 AM

you get the user selection like youíd do from any other input element. give the <select> a name and thatís the key of the value in the $_GET/$_POST array.

tip: you donít need to use ->fetchAll() at all, you can directly loop over $getStudents
$db = new PDO('sqlite:mydb.sqlite');


$getStudents= $db->query("SELECT this part works");
# do not fetch results as numerical and associative indexed (only the latter)
# $getStudents->setFetchMode(PDO::FETCH_ASSOC);
$studentrow = 0;

foreach($getStudents as $row)
{
    echo '<option value="', $studentrow, '">', $row['studentID'], $row['firstName'], $row['lastName'], '</option>';
    $studentrow++;
}


Was This Post Helpful? 1
  • +
  • -

#4 eksquall  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-April 09

Re: PHP PDO/SQLITE - Reading selected Dropdown value

Posted 21 September 2010 - 07:37 AM

View PostDormilich, on 21 September 2010 - 06:13 AM, said:

you get the user selection like youíd do from any other input element. give the <select> a name and thatís the key of the value in the $_GET/$_POST array.

tip: you donít need to use ->fetchAll() at all, you can directly loop over $getStudents
$db = new PDO('sqlite:mydb.sqlite');


$getStudents= $db->query("SELECT this part works");
# do not fetch results as numerical and associative indexed (only the latter)
# $getStudents->setFetchMode(PDO::FETCH_ASSOC);
$studentrow = 0;

foreach($getStudents as $row)
{
    echo '<option value="', $studentrow, '">', $row['studentID'], $row['firstName'], $row['lastName'], '</option>';
    $studentrow++;
}



Thanks,the looping part works to populate the dropdown menu, however i cant figure out how to read the selected value. my post goes to selectedStudent.php which contains:

 <?php
    
  if( isset($_POST) && !empty($_POST))
  
      {
  
      $userSelected = $_POST['studentrow'];
  
      }     
      echo $userSelected;
      
      ?>


Which only displays a number based on which option was selected from the dropdown menu (ie.0 for first option, 1 for second option). You mentioned an array but i have no idea on how to implement that.

Thanks
Was This Post Helpful? 0
  • +
  • -

#5 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: PHP PDO/SQLITE - Reading selected Dropdown value

Posted 21 September 2010 - 01:28 PM

Hi,

Your only getting the number because thats what you set as the "value" attribute of your option. In this case you are setting it to whatever $studentrow is :

echo '<option value="' . $studentrow . '">'



Whatever is in the value attribute of the option is what will get posted. :)

This post has been edited by JackOfAllTrades: 21 September 2010 - 01:40 PM
Reason for edit:: Changed commas to periods for string concatenation

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1