0

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>
Saagar Elias Jacky
  • 2,684
  • 2
  • 14
  • 28
Eyad Lotfy
  • 67
  • 8
  • you see the alert on page load? – dandavis Apr 12 '15 at 21:25
  • 3
    you cant mix php and js like that. php runs before anything is even sent to the browser –  Apr 12 '15 at 21:25
  • If you want JavaScript code to be run when a button is clicked, you should likely use event listeners: http://www.w3schools.com/jsref/met_element_addeventlistener.asp – idungotnosn Apr 12 '15 at 21:26
  • Yes,but it doesn't always show up – Eyad Lotfy Apr 12 '15 at 21:27
  • but Dagon it is actually working and even you it is sent to the browser and runs before anything it is still a function put inside {} how could it run without being called? – Eyad Lotfy Apr 12 '15 at 21:28
  • 2
    because the php will ignore the js –  Apr 12 '15 at 21:30
  • I guess it makes sense but tell why does the JS calles the function when I hit the button? – Eyad Lotfy Apr 12 '15 at 21:34
  • 1
    because that reloads the page, which runs the php again –  Apr 12 '15 at 21:37
  • You may want to have a look at an ajax call as this will do exactly what you want, this can be done with either just javascript or easier with [jQuery](http://api.jquery.com/jquery.ajax/) – TheSk8rJesus Apr 12 '15 at 22:45
  • possible duplicate of [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Jeremy Apr 13 '15 at 01:06

2 Answers2

0

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'] );
TheSk8rJesus
  • 418
  • 2
  • 12
-1

This will happen , what you need to do is

  • Method 1: <form method="POST" onsubmit="callFuntion()">
  • Method 2: <form action="demo_form.php" method="post">

put all your php code in file demo_form.php and when the submit is clicked it will go that file , which will help you to call the function once not twice .

Shelly
  • 355
  • 1
  • 7
  • onsubmit = "callfunction()" , have u used the preventDefault method inside the function callfunction() , which will stop calling your function on default. – Shelly Apr 12 '15 at 21:40
  • 1
    just wrong, you cant mix js and php like that –  Apr 12 '15 at 21:40
  • @Dagon : i am separating the js and php in my 2nd method solution ., which means that form action will call that php file only when its submit is clicked and page is not loaded. – Shelly Apr 12 '15 at 21:43
  • no, still wrong @Shelly –  Apr 12 '15 at 21:57