function is executed without being called [closed]

My code is supposed to store the info that the user inserts and store it in a Database.

This function should be called on a button click, but the problem is that the function runs when the page loads and when I press the button.

here is my code

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
    </head>
    <body>
        <?php


        function write_command($name,$address,$telephone,$cart){

            $servername = 'localhost';
            $server_username = 'root';
            $server_password = '';
            $DB_name = 'OnlineGarcon';
            $DB_connect = mysqli_connect($servername,$server_username,$server_password,$DB_name);
            if ($DB_connect -> connect_error){
                echo 'unable to connect to due to server error!';
            }else{
    $_write_command = "INSERT INTO `orders`(`name`, `address`, `telephone`, `cart`) VALUES ('$name','$address','$telephone','$cart')";
    if ($DB_connect->query($_write_command) == TRUE) {
    echo "Order was placed,Thanks!nyour order was:$cart";
    } else {
    echo "Server error!";
    }  
            }
        }


        ?>

        <center>
            <form method="POST">
            <input name="name" class="field" placeholder="Full name"></input>
            <br>
            <br>
            <input name="address" class="field" placeholder="Address"></input>
            </br>
            </br>
            <input name="telephone" class="field" placeholder="Telephone/Mobile number"></input>
            <br>
            <br>
            <br>
            <br>
            <button id="submit">Submit order</button>
            </br>
            </br>
            </br>
            </br>
        </form>
<script>

    $("#submit").click(function(){

    alert('<?php
   $name = $_POST['name'];
   $address = $_POST['address'];
   $telephone = $_POST['telephone'];
   $cart = $_GET['cart'];
   write_command($name,$address,$telephone,$cart);

    ?>');

    });
</script>
    </body>
</html>

Answer

Using an jQuery Ajax call you you can do this quite simply.

form.php

<html>
    <head>
        <script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
    </head>

    <body>
        <input name="name" id="name" placeholder="Full name">
        <input name="address" name="address" placeholder="Address">
        <input name="telephone" id="telephone" placeholder="Telephone/Mobile number">
        <button id="submit">Submit order</button>

        <script type="text/javascript">
            $('#submit').click( function() {
                $.ajax({
                    url: "functions.php",
                    type: "POST",
                    data: {
                        name: $('#name').val();
                        address: $('#address').val();
                        telephone: $('#telephone').val();
                        cart: <?php echo $_GET['cart']; ?>
                    }
                });
            });
        </script>
    </body>
</html>

functions.php

<?php
    function write_command($name, $address, $telephone, $cart) {
        $servername = 'localhost';
        $server_username = 'root';
        $server_password = '';
        $DB_name = 'OnlineGarcon';
        $DB_connect = mysqli_connect($servername,$server_username,$server_password,$DB_name);

        if( $DB_connect->connect_error ){
            echo 'unable to connect to due to server error!';
        } else {
            $_write_command = "INSERT INTO `orders`(`name`, `address`, `telephone`, `cart`) VALUES ('$name','$address','$telephone','$cart')";

            if ($DB_connect->query($_write_command) == TRUE) {
                echo "Order was placed,Thanks!nyour order was:$cart";
            } else {
                echo "Server error!";
            } 
        }
    }

    write_command( $_POST['name'], $_POST['address'], $_POST['telephone'], $_POST['cart'] );

Leave a Reply

Your email address will not be published. Required fields are marked *