为什么这段代码不起作用?

时间:2022-11-22 21:28:49

Can anyone tell me why this code won't work?

任何人都可以告诉我为什么这段代码不起作用?

<?php $err=1; ?>

<img src="334234234234234.gif" onError="<?php $err=0; ?>" />

<?php echo "<br />" . $err; ?>

Even when the image exists, the onerror is still executed. Why?

即使图像存在,仍然会执行错误。为什么?

4 个解决方案

#1


HTML cannot set PHP variables. Those can only be set while on the server. The onError method of the IMG tag has no access to PHP variables.

HTML无法设置PHP变量。这些只能在服务器上设置。 IMG标记的onError方法无法访问PHP变量。

The page life-cycle:

页面生命周期:

  1. (SERVER) The server builds your page (handling variables, etc)
  2. (SERVER)服务器构建您的页面(处理变量等)

  3. (SERVER -> CLIENT) The server sends out the page to the client computer
  4. (服务器 - >客户端)服务器将页面发送到客户端计算机

  5. (CLIENT) The html is loaded, javascript is ran, and any errors are raised.
  6. (客户端)加载html,运行javascript,并引发任何错误。

Note, you're attempting to combine item 3 with item 1, which cannot be done semantically.

注意,您正在尝试将项目3与项目1组合在一起,这在语义上无法完成。

The only way to do what you're attempting would be to attach a javascript method to that event, and communicate to the server when and if the onError method is ran. But this is likely going to be a bit more complicated than you're use to.

执行您尝试的操作的唯一方法是将javascript方法附加到该事件,并在运行onError方法时与服务器进行通信。但这可能比你习惯的要复杂一些。

#2


Since php is server side code, the <?php $err=0; ?> will get executed whether or not onError actually happens. You'll want to use someother method, or Javascript (or another client side code) to do that.

由于php是服务器端代码,

If all you are wanting to do is print out a variable to the screen if the image doesn't load, you can use onError="alert('ERROR: Image did not load');" to create a popup box (or you can use javascript to modify the html on the page), but if you want to talk to the server, you'll have to either submit a form with javascript or use an AJAX method.

如果您想要做的只是在图像未加载时将变量打印到屏幕上,则可以使用onError =“alert('ERROR:Image not load');”创建一个弹出框(或者你可以使用javascript修改页面上的html),但是如果你想与服务器通信,你必须使用javascript提交表单或使用AJAX方法。

#3


Your code will always produce this output:

您的代码将始终生成此输出:

<img src="334234234234234.gif" onError="" />
<br />0

The onError JavaScript handler is empty because the php code inside it produces no output.

onError JavaScript处理程序为空,因为其中的php代码不产生输出。

Is the code you posted actual code or simplified code? It's not clear what you're trying to accomplish.

您发布的代码是实际代码还是简化代码?目前尚不清楚你想要完成什么。

#4


If you are checking for the existence of a file PHP offers a nice function

如果要检查文件的存在,PHP提供了一个很好的功能

http://us.php.net/file_exists

Modified example from page.

来自页面的修改示例。

<?php
$filename = '/path/to/334234234234234.gif';

if (file_exists($filename)) {
    echo "<img src='334234234234234.gif'/>";
} else {
    echo "The file $filename does not exist";
}
?>

To answer your question.

回答你的问题。

Even when the image exists, the onerror is still executed. Why?

即使图像存在,仍然会执行错误。为什么?

The onError is never executed. It appears to execute because you explicitly output the $err variable at the end of the script. If you use the above code you can achieve what I believe was the intended result without relying on a JavaScript event.

永远不会执行onError。它似乎执行,因为您在脚本的末尾显式输出$ err变量。如果您使用上面的代码,您可以在不依赖JavaScript事件的情况下实现我认为的预期结果。

#1


HTML cannot set PHP variables. Those can only be set while on the server. The onError method of the IMG tag has no access to PHP variables.

HTML无法设置PHP变量。这些只能在服务器上设置。 IMG标记的onError方法无法访问PHP变量。

The page life-cycle:

页面生命周期:

  1. (SERVER) The server builds your page (handling variables, etc)
  2. (SERVER)服务器构建您的页面(处理变量等)

  3. (SERVER -> CLIENT) The server sends out the page to the client computer
  4. (服务器 - >客户端)服务器将页面发送到客户端计算机

  5. (CLIENT) The html is loaded, javascript is ran, and any errors are raised.
  6. (客户端)加载html,运行javascript,并引发任何错误。

Note, you're attempting to combine item 3 with item 1, which cannot be done semantically.

注意,您正在尝试将项目3与项目1组合在一起,这在语义上无法完成。

The only way to do what you're attempting would be to attach a javascript method to that event, and communicate to the server when and if the onError method is ran. But this is likely going to be a bit more complicated than you're use to.

执行您尝试的操作的唯一方法是将javascript方法附加到该事件,并在运行onError方法时与服务器进行通信。但这可能比你习惯的要复杂一些。

#2


Since php is server side code, the <?php $err=0; ?> will get executed whether or not onError actually happens. You'll want to use someother method, or Javascript (or another client side code) to do that.

由于php是服务器端代码,

If all you are wanting to do is print out a variable to the screen if the image doesn't load, you can use onError="alert('ERROR: Image did not load');" to create a popup box (or you can use javascript to modify the html on the page), but if you want to talk to the server, you'll have to either submit a form with javascript or use an AJAX method.

如果您想要做的只是在图像未加载时将变量打印到屏幕上,则可以使用onError =“alert('ERROR:Image not load');”创建一个弹出框(或者你可以使用javascript修改页面上的html),但是如果你想与服务器通信,你必须使用javascript提交表单或使用AJAX方法。

#3


Your code will always produce this output:

您的代码将始终生成此输出:

<img src="334234234234234.gif" onError="" />
<br />0

The onError JavaScript handler is empty because the php code inside it produces no output.

onError JavaScript处理程序为空,因为其中的php代码不产生输出。

Is the code you posted actual code or simplified code? It's not clear what you're trying to accomplish.

您发布的代码是实际代码还是简化代码?目前尚不清楚你想要完成什么。

#4


If you are checking for the existence of a file PHP offers a nice function

如果要检查文件的存在,PHP提供了一个很好的功能

http://us.php.net/file_exists

Modified example from page.

来自页面的修改示例。

<?php
$filename = '/path/to/334234234234234.gif';

if (file_exists($filename)) {
    echo "<img src='334234234234234.gif'/>";
} else {
    echo "The file $filename does not exist";
}
?>

To answer your question.

回答你的问题。

Even when the image exists, the onerror is still executed. Why?

即使图像存在,仍然会执行错误。为什么?

The onError is never executed. It appears to execute because you explicitly output the $err variable at the end of the script. If you use the above code you can achieve what I believe was the intended result without relying on a JavaScript event.

永远不会执行onError。它似乎执行,因为您在脚本的末尾显式输出$ err变量。如果您使用上面的代码,您可以在不依赖JavaScript事件的情况下实现我认为的预期结果。