samedi 28 novembre 2020

here my setinterval function is not working please help me

enter code here
<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css"
        integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">


<style>
    body {
        width: 50%;
        margin-left: 23%;
    }

    .container {
        border: 2px solid #dedede;
        background-color: #f1f1f1;
        border-radius: 5px;
        padding: 10px;
        margin: 10px 0;
        width: 40%;
        height: 80px;
    }

    /* Darker chat container */
    .darker {
        border-color: #ccc;
        background-color: #ddd;
    }

    /* Clear floats */
    .container::after {
        content: "";
        clear: both;
        display: table;
    }

    /* Style images */
    .container img {
        float: left;
        max-width: 60px;
        width: 100%;
        margin-right: 20px;
        border-radius: 50%;
    }

    /* Style the right image */
    .container img.right {
        float: right;
        margin-left: 20px;
        margin-right: 0;
    }

    /* Style time text */
    .time-right {
        float: right;
        color: #aaa;
    }

    /* Style time text */
    .time-left {
        float: left;
        color: #999;
    }

    .main {
        height: auto;
    }
    </style>
</head>

<body>

<?php

session_start();
$id1 = $_SESSION['id1'];
$id2 = $_SESSION['id2'];


include 'include/connection.php';
echo "id1=" .$id1. "<br>";
echo "id2=" .$id2. "<br>";


$sql="SELECT * FROM `users` WHERE `user_id`='$id1'";
$res = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($res);
$self = $row['user_name'];


$sql="SELECT * FROM `users` WHERE `user_id`='$id2'";
$res = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($res);
$other = $row['user_name'];

echo "<h3> hello $self you are going to talk with $other </h3>";
mysqli_error($conn);
?>


<script>
    //function Fun
function Fun($id1, $id2)
{
<?php 

include 'include/connection.php';
    
$sql = "SELECT * FROM `message` WHERE (`id1`='$id1' AND `id2`='$id2') OR (`id1`='$id2' AND `id2`='$id1');";
$res=mysqli_query($conn,$sql);

while($row=mysqli_fetch_assoc($res)) 
        {
        $msg=$row['message'];
        $send_id=$row['send_id'];
        echo '<div class="main">';
        
        if($send_id==$id1){
            echo '    
            <div class="container">
            <p>'; echo $msg; ' </p>
            <span class="time-right">11:00</span>
            </div>  ';  
        }
        else{
            echo '<div class="container darker">
            <p>'; echo $msg; echo ' </p>
            <span class="time-left">11:01</span>
            </div>';
        }
        echo '</div>';
        
}
mysqli_error($conn);
$message="";

?>
}
// End function

Fun($id1, $id2);
setInterval(Fun($id1, $id2), 1000);
</script>

    <form method="POST" id="myForm">
        <input type="text" placeholder="Add Message" name="message" />
        <button name="send">send</button>

    </form>
<?php
if(isset($_POST['send'])) { 
    include 'include/connection.php';
    $message = $_POST['message'];
    if($message!=""){
    $sql = "INSERT INTO `message` (`id1`, `id2`, `message`, `stime`, `send_id`) VALUES ('$id1', '$id2', '$message', current_timestamp(), '$id1')";
    $res = mysqli_query($conn,$sql);
    }
    $message="";
}

?>

    <!-- Optional JavaScript; choose one of the two! -->

    <!-- Option 1: jQuery and Bootstrap Bundle (includes Popper) -->
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
        integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous">
    </script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous">
    </script>

</body>

</html>

here i am trying to make chat web at top there are message that i fetch from database and than after there are input box when i send message than message inserted into database but not show in top message list.

i use here setinterval function but not working.

is there any other way for refresh Fun function after send message.

is there any idea than ask me

Note:

151 line my setinterval function

112 line my Fun function started and

147 line my Fun function ended.




Aucun commentaire:

Enregistrer un commentaire