-2

I have such a request: I have to do JS script, that works with PHP script. Right now i have such a PHP code:

~~~~~~ PHP ~~~~~~

<?php

    $email = filter_input(INPUT_POST, 'email');
    if (!empty($email)){
        $host = "localhost";
        $dbusername ="root";
        $dbpassword ="";
        $dbname = "email";      
        $conn = new mysqli ($host,$dbusername, $dbpassword , $dbname);
        if (mysqli_connect_error()){
            die('Connection problem('.mysqli_connect_errno().')'.mysqli_connect_error()); 
        }
        else{
            $sql = "INSERT INTO `email` (`email_id`, `email`) VALUES (NULL, '$email');";
            if ($conn->query($sql)){
                echo "<script type='text/javascript'>alert(\"Email has been written to subscribe list!\");</script>";
                echo '<script type="text/javascript">
           window.location = "index.html"
      </script>';
            }
            else{
                echo "<script type='text/javascript'>alert(\"Your email is already in our subscribe list!\");</script>";
                echo '<script type="text/javascript">
           window.location = "index.html"
      </script>';
            }
            $conn->close();   
    }}else{
            echo "<script type='text/javascript'>alert(\"Don't forget to include your email address !\");</script>";
        echo '<script type="text/javascript">
           window.location = "index.html"
      </script>';
        } 

?>

And here is a challenge. Instead of currently js scripts in php (that they activate on clean page without any content) JS script has to work on page, without reload - on the same page, when the request was sent from. I don't know how to do that ( i'm quite new to JS ) and i hope for your hints Thanks for advices

  • 2
    **Warning:** You are wide open to [SQL Injections](http://php.net/manual/en/security.database.sql-injection.php) and should really use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](http://php.net/manual/en/pdo.prepared-statements.php) or by [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Never trust any kind of input, especially that which comes from the client side. Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). – Dharman May 08 '19 at 12:05
  • AJAX: https://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php/5004276 – Adam May 08 '19 at 12:06

1 Answers1

3

If you don't want to reload the rendered page, you will have to work the other way around: not PHP is generating the whole page, but a JS script is pulling information from a PHP script and uses this information to display the according note.

There are multiple ways to fetch information from a server using JavaScript, the most prominent being AJAX to load HTML chunks or JSON responses. Please search for those topics as this is a relatively broad topic and your question is much too broad as it is.

feeela
  • 29,399
  • 7
  • 59
  • 71