简体   繁体   English

PHP中的Javascript无法正常工作

[英]Javascript inside php not working

I've create a code that check if a variable is empty or not. 我创建了一个代码来检查变量是否为空。
If the variable is empty I execute a javascript alert, in particular: 如果变量为空,则执行javascript警报,尤其是:

if($verbo_name == NULL)
{
    echo "
    <script>
        alert('no record available in the database');
    </script>";
    exit();
}   

If I insert a message inside the echo , the message appears correctly, but I want show the alert in javascript. 如果我在echo插入一条消息,则该消息会正确显示,但是我想用javascript显示警报。 What's the error? 怎么了 Thanks.. 谢谢..

UPDATE more details: 更新更多详细信息:

$results = $con->query("SELECT verbo, descrizione FROM verbo WHERE verbo = '$verbo'");

$verbo_name  = NULL; 

while($row = $results->fetch_array()) 
{
    $verbo_name = $row['verbo'];
}   

Check below codes are working fine for me. 检查以下代码对我来说工作正常。

<?php
    $verbo_name = NULL;
    if(is_null($verbo_name))
    {
        echo "
        <script>
            alert('no record available in the database');
        </script>";
        exit();
    }   
?>

Check this one 检查这个

if(empty($verbo_name))
{
    echo "
    <script>
        alert('no record available in the database');
    </script>";
    exit();
}

This should trigger an alert: 这应该触发警报:

   <html>
   <body>
   <?php
   if($verbo_name == NULL)
   {

    $msg = enter code here'no record available in the database';

   }  else {
   $msg = 'not set';
   }

   echo "
   <script>
   alert('". $msg ."');
   </script>";

   ?>
   </body>
   </html>

If the message is not set the problem is just the variable. 如果not set该消息,则问题只是变量。

This is Pravat Kumar Sahoo's answer, This must work fine. 这是Pravat Kumar Sahoo的答案,这必须工作正常。

If you are testing on google chrome, you might have prevented the alert dialog message mistakenly. 如果您在google chrome上进行测试,则可能是错误地阻止了警告对话框消息。 Please try clearing the cache/cookie or test on any other browser. 请尝试清除缓存/ cookie或在任何其他浏览器上进行测试。

<?php
$verbo_name = NULL;
if(is_null($verbo_name))
{
    echo "<script>alert('no record available in the database');</script>";
    exit();
}   
?>

Update: Instead of alerting the message inside PHP, echo a message, like "NoData". 更新:而不是在PHP中警告消息,而是回显一条消息,例如“ NoData”。 And in the ajax calling Javascript, receive response "success". 并在调用Javascript的ajax中,接收响应“成功”。

echo "NoData"; // In PHP file.

In your Javascript, get the response, 在您的Javascript中,获取响应,

 success:function(response)
 {
    if(response == "NoData");
    {
       alert("No data in the Database");
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM