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.
I have a javascript function that when a radio button is clicked it should execute the php.
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
<html>
<head>
<title>survey</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="test.css" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" type="text/javascript"></script>
</head>
<div class="cc-selector">
<form class="cc-selector" id="form-id" method="POST">
<label><input id="happy" type="radio" name="radioAnswer" onclick="doSomething();"></label>
<label class="drinkcard-cc happy" for="happy"></label>
<label><input id="sad" type="radio" name="radioAnswer" onclick="doSomething();"></label>
<label class="drinkcard-cc sad"for="sad"></label>
</form>
</div>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function doSomething() {
$.get("sample2.php");
return false;
}
</script>
</body>
</html>
And my php sample2.php
<?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')");
}
?>
$.get(). In your php, you have$_POST. See the problem? Protip: you always post forms.