Rooter
Rooter

Reputation: 383

javascript function not work in php properly

This is my php code. after insert data, I am used alert message. Message appeared properly. but it not close when I click x cross sign.

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));
    }

}

Alert message part:

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

Upvotes: 0

Views: 270

Answers (1)

Bing
Bing

Reputation: 3171

This line has too many nested " marks:

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

If you escape the inner ones properly (\") it will work.

Fiddle example by using raw HTML: https://jsfiddle.net/myingling/3vv7jLd4/

Better yet, I'd just remove the echo statements, so it looks like this:

if($stmt){
 ?>

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

Upvotes: 2

Related Questions