javascript函数在php中无法正常工作

时间:2017-06-15 19:05:12

标签: javascript php alert message

这是我的php代码。插入数据后,我使用了警告信息。消息显示正确。但是当我点击 x 十字标志时,它就不会关闭。

if(isset($_POST['create_post'])){

    $name =  $_POST['name'];
    $email = $_POST['email'];
    $country = $_POST['country'];
    $msg = $_POST['description'];



    $stmt = $connection->prepare("INSERT INTO message(name,email,country,msg,date) VALUES (?, ?, ?, ?,now())");
    $stmt->bind_param('ssss', $name,$email,$country,$msg);
    $stmt->execute(); 
    $stmt->close();

    if($stmt){

        echo '<div class= "alert">';
        echo '  <span class="closebtn" onclick="this.parentElement.style.display="none";">&times;</span> ';
        echo 'Your messaage has been sent ';
        echo '    </div>';

    }else{
        die('Query fialed' . mysqli_error($connection));
    }

}

警报信息部分:

echo '<div class= "alert">';
echo '  <span class="closebtn" onclick="this.parentElement.style.display="none";">&times;</span> ';
echo 'Your messaage has been sent ';
echo '    </div>';

1 个答案:

答案 0 :(得分:2)

这一行有太多嵌套的"标记:

echo ' <span class="closebtn" onclick="this.parentElement.style.display="none";">&times;</span> ';

如果你正确地逃避了内部的那些(\")它将起作用。

使用原始HTML的小提琴示例:https://jsfiddle.net/myingling/3vv7jLd4/

更好的是,我只是删除了echo语句,所以它看起来像这样:

if($stmt){
 ?>

    <div class= "alert">
      <span class="closebtn" onclick="this.parentElement.style.display='none';">&times;</span> 
      Your messaage has been sent 
    </div>
 <?php
 }else{