Passing variables
Moderator: General Moderators
- Lonestarjack
- Forum Commoner
- Posts: 31
- Joined: Tue Nov 11, 2008 7:13 am
- Location: Texas
Passing variables
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>
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
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>";- Lonestarjack
- Forum Commoner
- Posts: 31
- Joined: Tue Nov 11, 2008 7:13 am
- Location: Texas
Re: Passing variables
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
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
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>';
?>- Lonestarjack
- Forum Commoner
- Posts: 31
- Joined: Tue Nov 11, 2008 7:13 am
- Location: Texas
Re: Passing variables
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.
I like the loop concept though.
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: Passing variables
You need to do an initial retrieval of all the data, grouping by initial letter and returning a count.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.
Code: Select all
select SUBSTR(textField,1,1) as Letter,
count(*) as quantity
from dataTable
group by SUBSTR(textField,1,1)
Re: Passing variables
Oops.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.
<?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>';
?>
- Lonestarjack
- Forum Commoner
- Posts: 31
- Joined: Tue Nov 11, 2008 7:13 am
- Location: Texas
Re: Passing variables
Thanks
Works great. I have added it to my snippet collection.
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
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
Wow, that is really interesting.Mark Baker wrote:Quirkily, this also works for A..Z
*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
Extremely useful when you're working with Excel, because it also matches Excel's column naming.mmj wrote:Wow, that is really interesting.
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.