title: Execute php script from onclick insert to database
body: <p>I have created a working example that inserts the value of a radio button to a database using a submit button but now i'm looking at ways to insert without the use of a submit button.</p>
  
  <p>I have a javascript function that when a radio button is clicked it should execute the php.</p>
  
  <p>I have looked at why i can't get it to function but i'm unsure if its even possible or if there's a better way to do this. Below is my code</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;survey&lt;/title&gt;
    &lt;meta charset="utf-8" /&gt;
    &lt;link rel="stylesheet" type="text/css" href="test.css" /&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1" /&gt;
  
    &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" type="text/javascript"&gt;&lt;/script&gt;
  
  &lt;/head&gt;
  
  &lt;div class="cc-selector"&gt;
        &lt;form class="cc-selector" id="form-id"  method="POST"&gt;
      &lt;label&gt;&lt;input id="happy" type="radio" name="radioAnswer" onclick="doSomething();"&gt;&lt;/label&gt;
      &lt;label class="drinkcard-cc happy" for="happy"&gt;&lt;/label&gt;
      &lt;label&gt;&lt;input id="sad" type="radio" name="radioAnswer" onclick="doSomething();"&gt;&lt;/label&gt;
      &lt;label class="drinkcard-cc sad"for="sad"&gt;&lt;/label&gt;
    &lt;/form&gt;
          &lt;/div&gt;
          &lt;script type="text/javascript" src="jquery.min.js"&gt;&lt;/script&gt; 
  
  &lt;script type="text/javascript"&gt; 
  function doSomething() { 
      $.get("sample2.php"); 
      return false; 
  } 
  &lt;/script&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And my php sample2.php</p>
  
  <pre><code>&lt;?php
  $con = mysqli_connect("localhost","root","","test");
  
  // Check connection
  if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
  
    if (isset($_POST['radioAnswer'])){
          $radioAnswer = $_POST['radioAnswer'];
          mysqli_query($con,"INSERT INTO survey (radioAnswer) VALUES ('$radioAnswer')");
      }
  ?&gt;
  </code></pre>