Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

question of select data from database to be a opition

New Here ,
Feb 20, 2017 Feb 20, 2017

firstly , under the ajax method, i have this and it is available and working

xmlhttp.open("GET","code_add_test2.php?q="+str,true);

<form>

<select name="users" onchange="showUser(this.value)">

  <option value="">Select a person:</option>

  <option value="30">viki</option>

  <option value="25">simon</option>

  <option value="24">Paul</option>

  <option value="28">Angie</option>

  <option value="46">ANNIE</option>

  </select>

</form>

however i want to select the row in my database to become the option

and i have this , how to combine these two into one??? thank you

        <select name="ch_staff" id="ch_staff">

         <?php

   require_once('Connections/XXXXXXX.php');

mysql_select_db('staff');

     $query="select staffid From staffdata";

     $result=mysql_query($query);

   

$num_result=mysql_num_rows($result);

for($i=0;$i<$num_result; $i++)

{

    $row=mysql_fetch_array($result);

    $now = htmlspecialchars(stripslashes($row['staffid']));

    echo "<option value='$now'>$now</option>\n";

}

   ?>

              </select>

404
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Feb 21, 2017 Feb 21, 2017

There are several things about your example I would comment on, but first, why are you using AJAX? Maybe you have a good reason to use AJAX, but it isn't apparent from the example.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 21, 2017 Feb 21, 2017

we need to display the data by selecting the name from the option just like the option list under the asp

i need to link up with the option from my staffid database.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Feb 21, 2017 Feb 21, 2017
LATEST

None of what you have said indicates a need to use AJAX, so don't use it.

1. Don't use AJAX unless you need to.
2. No one should be using the old MySQL connection any longer. Use PDO or MySQLi.
3. You really need to use stripslashes and htmlspecialchars on the staff id field? Isn't it just an integer?
4. In the non-database example you use an id and name, but in your database example you pull just the id.
5. The for loop is not the efficient way to do this. Use a while loop


Here is a complete PDO example.

<?php


// PDO connection - it goes in a separate file, as you did for your mySQL connection

// Note that you need to replace the variables with your own


$dsn ='mysql:dbname=xxx;host=localhost;port=3306';
$user='xxx';
$password='xxx';
try {
$dbh = new PDO($dsn, $user, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
//$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // change to SILENT for production

}
catch (PDOException $e) {
     die('Connection failed: ');
}

//  end of connection script to put in separate file

if ($_POST){

echo "<pre>";

print_r($_POST);

echo "</pre>";

}

// now we get the data -- correct the variable names first or it will now work

$sql = $dbh->prepare("
SELECT        staffid, staff_lastname
FROM             staff
ORDER BY staff_lastname");
try {
$sql->execute()or $response = "<h3>Request  Failed.</h3>";
}catch(PDOException $e)
{    echo $e;}  
if ($response){
echo $response;
}
while($result = $sql->fetch(PDO::FETCH_ASSOC)){
$staff_optionset.= "<option value='".$result['staffid']."'>".$result['staff_lastname']."</option>";
}


// so now you have the options in one chuck which you can place within the select tags using PHP

?>

<form action='' method='post'>

<select name="users" >

<?php echo $staff_optionset ; ?>

  </select>

<input type='submit' value='submit'/>

</form>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines