Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how update record in database using php code help javascript

    Hello i have a question
    how update record in database using php code help javascript

    for example

    click link then javascript timer start 10 to 1 then 0 then
    tell me that read first or not

    Code:
    <div id="bip">Wait Please... </div>
    
    <script>
    var counter = 10;
    var intervalId = null;
    function finish() {
    clearInterval(intervalId);
    <?php
    $read = '1';
    if ($read == '1') {
    ?>
    document.getElementById("bip").innerHTML = "already read!";
    <?php
    }else{
    $sql = "Select * from message where securtycode='r5tr43r5t67y6'";
    $result = mysqli_query($conn, $sql); 
    $myrow = mysqli_fetch_array($result);
    $read = $myrow["read"];
    
    
    $sql = "UPDATE message Set read = '$read'+1 where securtycode='r5tr43r5t67y6'";
    $result = mysqli_query($conn, $sql);
    
    document.getElementById("bip").innerHTML = "Read Message";
    }
    ?>
    }
    
    function bip() {
    if(counter == 0) finish();
    else {
    if(document.hasFocus()) {
    document.getElementById("bip").innerHTML = counter + " seconds remaining";
    counter--;	
    }else{
    }
    }
    }
    function start() {  intervalId = setInterval(bip, 1000);
    }	
    </script>
    how can this
    Last edited by vinyl-junkie; Jun 11th, 2018 at 07:36 AM. Reason: added code tags

  2. #2
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,651
    Thanks
    24
    Thanked 580 Times in 573 Posts
    You can't include PHP code in JS. you have to send a request to PHP (=> AJAX) to do the DB update.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •