0

Have I got this totally wrong ? It should be simple.
Whatever I try I return the COMPLETE A-Z and never the selected letter only. It seems I am not able to change the variable $name at all.
The relevant code I have made is:

<?php 
$alphasearch='';
$spanSearch = range('A','Z');
foreach($spanSearch as $char)
{
$alphasearch.="<span><a href='javascript:ajaxFunction();' name='alphasearch' value='$char'>$char</a></span>\n";
}
?>
<html>  
<head>
<script language="javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
    var ajaxRequest;  // The variable that makes Ajax possible!
    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            document.getElementById('directory').innerHTML= ajaxRequest.responseText;
        }
    }
    var parameters="char=$name"         
    ajaxRequest.open("POST", "select_Name.php", true); 
    ajaxRequest.send(parameters);
}
//-->
</script>
</head>
<body >
<?php
echo $alphasearch;
?>
<br/>
<br/>
<div id="directory" style="margin-left:60px;height:auto;width:25%;background-color:#ffff66">DATABASE INDEX</div>    
</body>
</html>

Select_Name.php starts so:

.........
mysql_select_db('xxxx')
    or die("Unable to select database: " . mysql_error());
if(isset($_POST['char'])) $name=($_POST['char']);
$query="(SELECT name1 FROM allnames WHERE ..........

Am I anywhere near getting it correct or is the data returned just fortunate ? Could someone explain how I can put it right.

4
Contributors
7
Replies
41
Views
4 Years
Discussion Span
Last Post by furlanut
0

try changing

$alphasearch.="<span><a href='javascript:ajaxFunction();' name='alphasearch' value='$char'>$char</a></span>\n";

to

$alphasearch.="<span><a href='javascript:ajaxFunction();' name='alphasearch' id='alphasearch' value='$char'>$char</a></span>\n";

and

var parameters="char=$name"

to

var parameters= $('#alphasearch');
0

Thank you for reply,but am not getting anything returned (or any error). Select_Name.php on its own returns the full index. I suspect the error is on that page ? (i.e. creating the variable)

0

Try this, then $name will get the character clicked through POST. If I understood correctly, that is what you need?

<?php 
$alphasearch='';
$spanSearch = range('A','Z');
foreach($spanSearch as $char)
{
$alphasearch.="<span><a href='javascript:ajaxFunction(\"$char\");' name='alphasearch' value='$char'>$char</a></span>\n";
}
?>
<html>  
<head>
<script language="javascript">
<!-- 
//Browser Support Code
function ajaxFunction(ch){
    var ajaxRequest;  // The variable that makes Ajax possible!
    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }

    ajaxRequest.open("POST", "select_Name.php", true);
    ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajaxRequest.send("char="+ch);

        // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            document.getElementById('directory').innerHTML= ajaxRequest.responseText;
        }
    }
}
//-->
</script>
</head>
<body >
<?php
echo $alphasearch;
?>
<br/>
<br/>
<div id="directory" style="margin-left:60px;height:auto;width:25%;background-color:#ffff66">DATABASE INDEX</div>    
</body>
</html>
0

on line 39 is $name in that string a php variable?
if so should it not be

var parameters="char=<?php echo $name; ?>"
0

Thank you for helping me out persevering with this,Christos and the Hop.
I have made two changes to my script:

$alphasearch.="<span><a href='javascript:ajaxFunction();' name='alphasearch' value='$char'>$char</a></span>\n";
}
?>
<html>  
<head>
<script language="javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){

nothing was returned with the ajaxFunction($char)
and the parameters ($name was my confusion with $_POST)

    var url="select_Name.php";      
    var parameters="char=<?php echo ($char);?>"              
    ajaxRequest.open("POST", url, true);
    ajaxRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
    ajaxRequest.send(parameters);

Some progress but only items starting with Z are returned no matter what letter is clciked in $alphasearch.
Any ideas why only the last letter in the range is selected ?

0

After

foreach($spanSearch as $char)
{
$alphasearch.="<span><a href='javascript:ajaxFunction();' name='alphasearch' value='$char'>$char</a></span>\n";
}

if you write

echo $char;

you'll see that it is always 'Z', the last item in the foreach loop. So at the later point, where you write

    var parameters="char=<?php echo ($char);?>" 

you are assigning a constant value of 'Z' to the js variable char. So, independent of which item is clicked, the function called as ajaxFunction() will have char='Z'. That is why I suggested using a parameter for the ajaxFunction() function, it would solve this problem.

If you use the code I posted in my previous post and replace the content in select_Name.php with the simple

<?php
    $name=($_POST['char']);
    echo $name;
?>

it will work, I tried it myself. From there on, you can change it to whatever fits your needs.
Good luck!:)

0

Sorry to say I overlooked a piece of code in your post 2 weeks ago.Having made the correction I can agree with the result. Thank you for the explanation in your last post.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.