Copy link to clipboard
Copied
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>
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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>
Find more inspiration, events, and resources on the new Adobe Community
Explore Now