当我用HTML调用它时,为什么我的Javascript函数无法运行/工作?

时间:2016-07-06 17:16:14

标签: javascript php html

所以我尝试了很多方法来调用函数本身。我是PHP / Javascript的新手,所以我试着去学习它。

我已经制作了一个功能,可以检查是否选中了某个复选框。我想要的功能是,如果选中复选框,则将页面重定向到另一页面,如果未选中则显示警告框。

我通过javascript函数传递的变量来自我的PHP代码,因此我认为我的问题可能与这两种语言如何相互影响有关。

这是我的代码。我从SQL数据库中获取各种变量并将其存储在PHP变量中。我在PHP代码中的注释行之后调用该函数:



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

<?php
	//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=\"window.open('https://someurl.php?ticketNumber=".$ticketNumber."')\">Print</button> </td>";
		$strHTML .= "</tr>";

		echo $strHTML;

		$recordSet->MoveNext;
	}
?>
&#13;
&#13;
&#13;

根据要求,这是我的$ strHTML:

&#13;
&#13;
<td>Psychology</td>
<td>1000</td>
<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='email@email.ca'/></td>
<td><button id='emailButton' onclick='checkReceived(checked, email@email.ca)'>email@email.ca</button></td>
<td align='center'> <button onclick="window.open('https://someurl.php?ticketNumber=31')">Print</button> </td>
</tr>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

在javascript中,不要写

if (received == checked){

但是

if (received == "checked"){

您想要将变量received与字符串"checked"进行比较。 通过PHP,received变量收到"checked""unchecked"

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

然后通过javascript函数调用将其转换为HTML。必须使用类似

的字符串调用javascript函数

checkReceived("string", "string")

所以在PHP中它变成了

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

并使用PHP变量

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

在您的代码中,它变为:

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

要查看函数checkReceived()是否被调用,请插入console.log()语句并查看浏览器的调试器JavaScript控制台。

checkReceived(received, profEmail){
    console.log(received, profEmail);
}

答案 1 :(得分:1)

问题是从php连接html时。你应该在callin函数(ex onchange=\"checkboxClicked('Received', ".$ticketNumber.")\"

时使用转义序列
$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=\"window.open('https://someurl.php?ticketNumber=".$ticketNumber."')\">Print</button> </td>";
    $strHTML .= "</tr>";

并在javascript函数if(checked == "checked")中加上引号

答案 2 :(得分:1)

我正在通过更好的解释扩大我的评论答案。

首先在JS中,不要将变量与checked进行比较,而是将其与"checked"(即字符串)进行比较,如@PaulH所述。

所以你的JS应该是这样的:

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

现在对于PHP,在行$strHTML .= "<td><button id='emailButton' onclick='checkReceived($received, $email)'>".$email."</button></td>";中,您没有将$received$email变量的输出包装在引号(")中。这是必要的,因为它会打印HTML,将字符串传递给JS函数。因此,您的PHP代码需要如下所示:

<?php
    //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=\"window.open('https://someurl.php?ticketNumber=".$ticketNumber."')\">Print</button> </td>";
        $strHTML .= "</tr>";

        echo $strHTML;

        $recordSet->MoveNext;
    }
?>

让我知道结果如何。