title: function is executed without being called
body: <p>My code is supposed to store the info that the user inserts and store it in a Database.</p>
  
  <p>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.</p>
  
  <p>here is my code</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;!--
  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.
  --&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta charset="UTF-8"&gt;
          &lt;title&gt;&lt;/title&gt;
          &lt;script src="//code.jquery.com/jquery-2.1.1.min.js"&gt;&lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;?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 -&gt; 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-&gt;query($_write_command) == TRUE) {
      echo "Order was placed,Thanks!\nyour order was:$cart";
      } else {
      echo "Server error!";
      }  
              }
          }
  
  
          ?&gt;
  
          &lt;center&gt;
              &lt;form method="POST"&gt;
              &lt;input name="name" class="field" placeholder="Full name"&gt;&lt;/input&gt;
              &lt;br&gt;
              &lt;br&gt;
              &lt;input name="address" class="field" placeholder="Address"&gt;&lt;/input&gt;
              &lt;/br&gt;
              &lt;/br&gt;
              &lt;input name="telephone" class="field" placeholder="Telephone/Mobile number"&gt;&lt;/input&gt;
              &lt;br&gt;
              &lt;br&gt;
              &lt;br&gt;
              &lt;br&gt;
              &lt;button id="submit"&gt;Submit order&lt;/button&gt;
              &lt;/br&gt;
              &lt;/br&gt;
              &lt;/br&gt;
              &lt;/br&gt;
          &lt;/form&gt;
  &lt;script&gt;
  
      $("#submit").click(function(){
  
      alert('&lt;?php
     $name = $_POST['name'];
     $address = $_POST['address'];
     $telephone = $_POST['telephone'];
     $cart = $_GET['cart'];
     write_command($name,$address,$telephone,$cart);
  
      ?&gt;');
  
      });
  &lt;/script&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>