Page 1 of 1

Passing variables

Posted: Fri Nov 21, 2008 7:44 am
by Lonestarjack
This code never gets to box_data for me to see the contents of the passed parameter.
If I click the M button -- I get the error "M" is not defined.



<?php
echo '<table border="1">';
echo '<tr>';
$letters = array("All","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
foreach ($letters as $letter){
echo "<td><a class='ovalbutton' onclick='box_data($letter)'><span>$letter</span></a></td>";
}
echo '</tr></table>';
?>

Here is the ajax link. The alert function is never executed.
<script language="JavaScript" type="text/javascript">
function box_data(id) {
alert(id);
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null) {
alert ("Your browser does not support AJAX!");
return;
}
var url="ajaxcontactbox.php?nd='+new Date().getTime()";
url=url+"&id="+id;
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete") {
document.getElementById('box_data').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
</script>

Re: Passing variables

Posted: Fri Nov 21, 2008 8:46 am
by Mark Baker
Javascript does like string values to be wrapped in quotes

Code: Select all

echo "<td><a class='ovalbutton' onclick='box_data(\"$letter\")'><span>$letter</span></a></td>";

Re: Passing variables

Posted: Fri Nov 21, 2008 10:37 am
by Lonestarjack
Thanks
I had tried adding the quotes, but without the escape.
I am able to use the non-quoted non-escaped when I use <option>.
Confusing to say the least.
Works great by copying you code.
Thanks again

Re: Passing variables

Posted: Fri Nov 21, 2008 11:31 am
by mmj

Code: Select all

<?php
echo '<table border="1">';
echo '<tr>';
for ($i = 64; $i < 91; $i++) {
    $c = $i == 64 ? 'All' : chr($i);
    echo '<td><a class="ovalbutton" onclick="box_data('.$c.')"><span>'.$c.'</span></a></td>';
}
echo '</tr></table>';
?>

Re: Passing variables

Posted: Fri Nov 21, 2008 3:16 pm
by Lonestarjack
This makes the same alphabet selector, but has the same "X is undefined" upon pressing the X button. I tried escaping the quotes but no go.
I like the loop concept though.

Re: Passing variables

Posted: Fri Nov 21, 2008 4:21 pm
by Mark Baker
Lonestarjack wrote:This makes the same alphabet selector, but has the same "X is undefined" upon pressing the X button. I tried escaping the quotes but no go.
I like the loop concept though.
You need to do an initial retrieval of all the data, grouping by initial letter and returning a count.

Code: Select all

 
select SUBSTR(textField,1,1) as Letter,
       count(*) as quantity
  from dataTable
 group by SUBSTR(textField,1,1)
 
Then loop through the result and disable the link in your list for any letters that don't have an entry in the result

Re: Passing variables

Posted: Sat Nov 22, 2008 3:22 am
by mmj
Lonestarjack wrote:This makes the same alphabet selector, but has the same "X is undefined" upon pressing the X button. I tried escaping the quotes but no go.
I like the loop concept though.
Oops.
<?php
echo '<table border="1">';
echo '<tr>';
for ($i = 64; $i < 91; $i++) {
    $c = $i == 64 ? 'All' : chr($i);
    echo '<td><a class="ovalbutton" onclick="box_data(\''.$c.'\')"><span>'.$c.'</span></a></td>';
}
echo '</tr></table>';
?>

Re: Passing variables

Posted: Sat Nov 22, 2008 5:53 am
by Lonestarjack
Thanks
Works great. I have added it to my snippet collection.

Re: Passing variables

Posted: Sat Nov 22, 2008 10:34 am
by Mark Baker
Quirkily, this also works for A..Z

Code: Select all

 
<?php
 
echo '<table border="1">';
echo '<tr>';
for ($i = 'A'; $i != 'AA'; ++$i) {
    echo '<td><a class="ovalbutton" onclick="box_data(\''.$i.'\')"><span>'.$i.'</span></a></td>';
}
echo '</tr></table>';
 
?>
 

Re: Passing variables

Posted: Sat Nov 22, 2008 10:44 am
by mmj
Mark Baker wrote:Quirkily, this also works for A..Z
Wow, that is really interesting.

*goes off to php.net to find some documentation*

edit:

interesting, comes from perl but something similar is in C.
http://www.php.net/operators.increment wrote:PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's. For example, in Perl 'Z'+1 turns into 'AA', while in C 'Z'+1 turns into '[' ( ord('Z') == 90, ord('[') == 91 ). Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.

Re: Passing variables

Posted: Sat Nov 22, 2008 12:45 pm
by Mark Baker
mmj wrote:Wow, that is really interesting.
Extremely useful when you're working with Excel, because it also matches Excel's column naming.

What you do have to watch out for is your comparisons, particularly in for loops: equality/inequality is predictable when working with characters, but >, >=, <, <= aren't intuitive.
Hence my use of $i != 'AA'. $i <= 'Z' (which might have seemed logical to use) has a somewhat different effect.