我有一个调查显示一张快乐和悲伤的脸
当用户点击单选按钮时,它应该执行 onclick 事件
然后 PHP 应该将它提交到数据库但是当单击单选按钮时它没有运行 php,我正在考虑如何提交它我是否需要从该页面内运行 php 而没有它作为一个表单 Action 。
<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>
<body>
<div id="wrapper">
<img src="Wincanton.png" alt="wincantonLogo" class="wincantonLogo" />
<img src="Screwfix.jpg" alt="screwfixLogo" class="screwfixLogo" />
<h1 class="Survey_Title">Heath and Wellbeing</h1><br />
<h2 class="Survey_Question">Did you find the most recent Wellbeing campaign useful?</h2><br/>
<div class="cc-selector">
<form class="cc-selector" id="form-id" action="tester.php" method="POST">
<label><input id="happy" type="radio" name="radioAnswer" onclick="document.getElementById('cc-selector').click();" /></label>
<label class="drinkcard-cc happy" for="happy"></label>
<label><input id="sad" type="radio" name="radioAnswer" onclick="document.getElementById('cc-selector').click();" /></label>
<label class="drinkcard-cc sad"for="sad"></label>
</form>
</div>
<script type="text/javascript">
$(".cc-selector").html(
$(".cc-selector").children().sort(function(a, b) {
return Math.round(Math.random()) - 0.8;
})
);
</script>
</div>
</body>
</html>
<?php if(array_key_exists('radioAnswer', $_POST)) {
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('test');
$radioAnswer = $_POST['radioAnswer'];
...
}
?>
最佳答案
我认为有几件事会希望将单选按钮命名为相同的。所以:
<form class="cc-selector" id="form-id" method="POST">
<label><input id="happy" type="radio" name="radioAnswer" value="happy"/></label>
<label><input id="sad" type="radio" name="radioAnswer" value="sad"/></label>
<input type="submit" name="submit" value="submit" />
</form>
你的 php 也需要改变:
<?php if(array_key_exists('radioAnswer', $_POST)) {
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('test');
$radioAnswer = $_POST['radioAnswer'];
...
}
如果你想让它发生在onclick上,你可以使用javascript onclick提交表单,这里是一个例子:
<input id="happy" type="radio" name="radioAnswer" onclick="document.getElementById('form-id').submit();" />
关于javascript - 单击将单选按钮值插入 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51176329/