Andy Wong Andy Wong - 1 year ago 61
PHP Question

Why does my Javascript function not run/work when I call it in HTML?

So I've tried numerous ways of calling the function itself. I'm rather new to PHP/Javascript, so I'm trying to learn it as I go.

I've made a function that checks if a certain checkbox is checked or not. What I want the function to do is redirect the page to another page if the checkbox IS checked, while displaying an alert box if it is unchecked.

The variables I pass through the javascript function are from my PHP code, so I was thinking my issue might be related to how the two languages interact with each other.

Here is my code. I fetch the various variables from a SQL database and store it in PHP variables. I call the function just after the commented line in the PHP code:

function checkReceived(received, profEmail){
if (received == checked){
window.location.assign('https://someurl.php?email=' + profEmail');
} else{
alert("LOL HOW ABOUT NO");

//This loop populates the ticket display using the records in the database
while($recordSet->EOF == false){
$ticketNumber = $recordSet->Fields['TicketNumber'];
$dateSubmitted = $recordSet->Fields['DateSubmitted'];
$department = $recordSet->Fields['Department'];
$courseNo = $recordSet->Fields['CourseNumber'];
$instructorName = $recordSet->Fields['InstructorName'];
$received = ($recordSet->Fields['Received']) ? "checked" : "unchecked";
$sendReminder = ($recordSet->Fields['SendReminder']) ? "checked" : "unchecked";
$email = $recordSet->Fields['Email'];
$CC = $recordSet->Fields['CC'];

$strHTML = "<tr><td>$dateSubmitted</td>";
$strHTML .= "<td>$department</td>";
$strHTML .= "<td>$courseNo</td>";
$strHTML .= "<td style='word-break: break-all'>$instructorName</td>";
$strHTML .= "<td align='center'> <input type='checkbox' ".$received." class='receivedCheckbox' onchange='checkboxClicked(\"Received\", ".$ticketNumber.")'/> </td>";
$strHTML .= "<td align='center'> <input type='checkbox' ".$sendReminder." class='sendReminderCheckbox' onchange='checkboxClicked(\"SendReminder\", ".$ticketNumber.")' data-email='".$email."'/> </td>";
//$strHTML .= "<td style='word-break: break-all;' align='center'> <a href='javascript:checkReceived($received, $email)'>".$email."</a></td>";
$strHTML .= "<td><button id='emailButton' onclick='checkReceived($received, $email)'>".$email."</button></td>";
$strHTML .= "<td align='center'> <button onclick=\"'https://someurl.php?ticketNumber=".$ticketNumber."')\">Print</button> </td>";
$strHTML .= "</tr>";

echo $strHTML;


As per request, here is my $strHTML:

<td style='word-break: break-all'>Some Name</td>
<td align='center'> <input type='checkbox' checked class='receivedCheckbox' onchange='checkboxClicked("Received", 31)'/> </td>
<td align='center'> <input type='checkbox' unchecked class='sendReminderCheckbox' onchange='checkboxClicked("SendReminder", 31)' data-email=''/></td>
<td><button id='emailButton' onclick='checkReceived(checked,'></button></td>
<td align='center'> <button onclick="'https://someurl.php?ticketNumber=31')">Print</button> </td>

Answer Source

In javascript, don't write

if (received == checked){


if (received == "checked"){

you want to compare the variable received with the string "checked". Through PHP, the received variable received the value "checked" or "unchecked"

$received = ($recordSet->Fields['Received']) ? "checked" : "unchecked";

then it is converted to HTML with a javascript function call. The javascript function must be called with strings like

checkReceived("string", "string"),

so in PHP it becomes

echo "checkReceived(\"string\", \"string\")";

and with PHP variables

echo "checkReceived(\"$var1\", \"$var2\")";

in your code, it becomes:

$strHTML .= "<td><button id='emailButton' onclick='checkReceived(\"$received\", \"$email\")'>$email</button></td>";

To see if function checkReceived() is called, insert a console.log() statement and look in your browser's debugger JavaScript Console.

checkReceived(received, profEmail){
    console.log(received, profEmail);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download