Why is it that the following 2 examples work, but the last doesn’t?
Example 1: An onchange event from a drop down list is passing the correct string to the function’s selectedText argument so the value of text1 field is being set correctly on the onchange event.
<SCRIPT LANGUAGE=’JavaScript’ type=’text/javascript’>
<!– //
function getMembers(selectedText) {
members = new Array();
<?php
$membersName = selectedText;
print(“members[0] = $membersName;n”);
?>
Form1.text1.value = members[0];
// –>
}
</SCRIPT>
Example 2: Here, I’m ignoring the selectedText argument and hard coding the PHP variable to the text that would be passed to the function. Query works fine, and the correct value is displayed in text1 on the onchange event of the dropdown list. (Not shown here)
<SCRIPT LANGUAGE=’JavaScript’ type=’text/javascript’>
<!– //
function getMembers(selectedText) {
members = new Array();
<?php
$membersName = “Pine Ridge Fellowship Church”;
$query2 = “SELECT groups_id FROM groups WHERE groups.groups_name = “” . $membersName . “””;
$result2 = mysql_query($query2);
while ($row2 = mysql_fetch_array($result2)) {
$members[] = $row2[‘groups_id’];
}
print(“members[0] = $membersName;n”);
?>
Form1.text1.value = members[0];
// –>
}
</SCRIPT>
Example 3. This is what doesn’t make sence. Combine the 2 above to get this and it does not set text1 value properly.
<SCRIPT LANGUAGE=’JavaScript’ type=’text/javascript’>
<!– //
function getMembers(selectedText) {
members = new Array();
<?php
$membersName = selectedText;
$query2 = “SELECT groups_id FROM groups WHERE groups.groups_name = “” . $membersName . “””;
$result2 = mysql_query($query2);
while ($row2 = mysql_fetch_array($result2)) {
$members[] = $row2[‘groups_id’];
}
print(“members[0] = $row2[0];n”);
?>
Form1.text1.value = members[0];
// –>
}
</SCRIPT>
The 1st example proves that I can pass a JS function argument’s value to a PHP variable.
The 2nd example proves I can use a PHP variable in a SQL query. (of course)
The last example says I can’t use a PHP variable that received it’s value from the JS function argument in a SQL query although combining the first 2 examples would logically say I could. Does this make sense to anyone? So what am I doing wrong here?!