Passing variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Lonestarjack
Forum Commoner
Posts: 31
Joined: Tue Nov 11, 2008 7:13 am
Location: Texas

Passing variables

Post 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>
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Passing variables

Post 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>";
User avatar
Lonestarjack
Forum Commoner
Posts: 31
Joined: Tue Nov 11, 2008 7:13 am
Location: Texas

Re: Passing variables

Post 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
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Passing variables

Post 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>';
?>
User avatar
Lonestarjack
Forum Commoner
Posts: 31
Joined: Tue Nov 11, 2008 7:13 am
Location: Texas

Re: Passing variables

Post 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.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Passing variables

Post 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
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Passing variables

Post 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>';
?>
User avatar
Lonestarjack
Forum Commoner
Posts: 31
Joined: Tue Nov 11, 2008 7:13 am
Location: Texas

Re: Passing variables

Post by Lonestarjack »

Thanks
Works great. I have added it to my snippet collection.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Passing variables

Post 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>';
 
?>
 
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Passing variables

Post 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.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Passing variables

Post 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.
Post Reply