单击将单选按钮值插入mysql

时间:2018-07-04 14:48:45

标签: javascript php html css

我进行的一项调查显示出高兴和悲伤的表情

当用户单击单选按钮时,它应该执行onclick事件

然后,PHP应该将其提交到数据库,但是当单击单选按钮时,它没有运行php,我试图考虑如何提交它,我需要从该页面中运行php,而不必作为形式动作。

 <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'];


   ...
}
?>

3 个答案:

答案 0 :(得分:1)

您应该为单选按钮命名相同。

<div class="cc-selector">
        <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" />
      </div>

现在要获取您可以使用的回复

$response = $_REQUEST['radioAnswer'];

这将向您显示用户是否选择了快乐或悲伤的广播。使用mysqli_ *或PDO函数查询数据库,不建议使用mysql_ *函数。您可能需要使用this link作为参考。

答案 1 :(得分:1)

我认为有几件事想将单选按钮命名为相同的名称。所以:

=100*((Q$180/Q$168)-1)

您的php也将需要更改:

<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>

如果希望onclick发生,可以使用javascript onclick提交表单,以下是示例:

<?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'];


   ...
}

答案 2 :(得分:0)

 <form name="form" action="sample2.php" class="cc-selector" method="post" >

       <label class="label">
        <input type="image" name="Opinion" value="Positive" src="happy.png" onclick="document.getElementById('form').submit();"/>

       </label>

       <label class="label">
        <input type="image" name="Opinion" value="Negative" src="sad.png" onclick="document.getElementById('form').submit();"/>
       </label>
      </form>