I can display an alpha index of the table contents using a form as follows:

<?php                                            
if (isset($_POST['name'])) $name = ($_POST['name']);
else $name = "no name entered";
?>  

<form method="post" name="search" action="directory.php"/>
        <font family = "arial,verdana,sans-serif" color="#3F7cef"size="4"/>Select Letter</font>
        <input type="text" name="name" size="5" maxlength ="1"  value="<?php if(isset($_POST['name'])){echo htmlentities($_POST['name']);}?>" />
        <input type="submit" name="submit" value="submit" alt="search"/> 
    </form>  

<?php
if($name== "")
{
echo "<p><font color='red' size='6px'>You have not entered a name</font></p>";
exit;                                           
}
else 
{
echo"<br />";
echo "<font color='#3F7cef'size='4'/>You searched for:</font>";
echo "<p>";
echo "<font color='#3F7cef'size='6'>";
require_once "../php/fix_it.php";
echo fix_it($name);                              //function to return name input
echo"</font>";
}
require_once '../php/dbconnect.php';                                
$query = "(SELECT name1 FROM allnames WHERE name1 LIKE '$name%')UNION(SELECT name2 FROM allnames WHERE name2 LIKE '$name%')UNION(SELECT name3 FROM allnames WHERE name3 LIKE '$name%').....UNION(SELECT name17 FROM allnames WHERE name17 LIKE '$name%')ORDER BY 1";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
$num_rows=mysql_num_rows($result);
if(mysql_num_rows($result)==0)                              
{
                       /* some code omitted  */

while($row = mysql_fetch_assoc($result)){
$array= $row;
foreach($array as $var){
if(!empty($var)){                                   
$str=($var); 
$str=strtoupper($str);
?>       

I would really like to replace the FORM with a line 'A' 'B' 'C' 'D' etc then onclick on a letter would initiate the SELECT query and print the index.
I am told this can only be done with JavaScript or Ajax neither of which I know. I this the only way ? If so could someone describe what it involves and give some pointers?

Member Avatar for LastMitch

I am told this can only be done with JavaScript or Ajax neither of which I know. I this the only way ? If so could someone describe what it involves and give some pointers?

You can just used PHP but if you want to used Ajax/Javascript then you do this:

http://www.jotorres.com/2011/11/display-database-information-with-ajax-jquery-and-php/

You can add checkbox or radio button to that code (optional).

Thanks. I have looked at Ajax/Javascript but think I will stay with PHP. However in neither have I been able to work out how to assign variable $name to the letter clicked (A,B C etc) when no form input is used.
At the moment I am only able to display ALL records at once !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.