下拉Onchange并将JavaScript值传递给PHP

时间:2018-05-01 07:00:11

标签: javascript php

您好我应该如何向PHP发送JavaScript值。

这是我的表格:

enter image description here

数据库中的下拉问题:

enter image description here

fiddle code

<form action="action/survey">
          <div class="form-group">
            <label class="control-label col-sm-2" for="email">Select Question</label>
            <div class="col-sm-10">
              <select class="form-control" id="mySelect" onchange="option()">
                <?php
                $sql = mysqli_query($con,"SELECT *,(SELECT GROUP_CONCAT(answer) AS `option`FROM `survey_anweroptions` WHERE survey_qID = sq.survey_qID) `option` FROM `survey_questionnaire`sq WHERE sq.survey_ID = $id"); 

                while ($q = mysqli_fetch_array($sql)) {
                 ?>
                  <option value="<?php echo $q[0]?>"><?php echo $q[2]?></option>
                 <?php
                }
                ?>
              </select>
            </div>

          </div>
          <div class="form-group">
            <label class="control-label " for=""></label>
          </div>
          <?php 
          $sql = mysqli_query($con,"SELECT *,(SELECT GROUP_CONCAT(answer) AS `option`FROM `survey_anweroptions` WHERE survey_qID = sq.survey_qID) `option` FROM `survey_questionnaire`sq WHERE sq.survey_ID = 1 AND survey_qID = 1");
          $d1= mysqli_fetch_array($sql);
          ?>
          <div class="form-group">
            <label class="control-label col-sm-2" for="">Question</label>
            <div class="col-sm-10">
              <input type="text" class="form-control" id="" placeholder="" value="<?php echo $d1[2]?>">
            </div>
          </div>
          <?php 
          $variable = $d1[3];
          $z = 1;
          $piece = explode(",", $variable);
          foreach ($piece as $key => $value) {

            ?>

            <div class="form-group">
            <label class="control-label col-sm-2" for="">Option <?php echo $z?> </label>
            <div class="col-sm-10">
              <input type="text" class="form-control" id="" placeholder="" value="<?php echo $value?>">
              </div>
            </div>
            <?php
            $z++;
          }
          ?>
          <div class="text-center">
          <button type="submit" class="btn btn-default" name="">Update</button>
          </div>
        </form>

JavaScript的:

function option(){
    var x = document.getElementById("mySelect").value; 
    document.getElementById("demo").innerHTML = "You selected: " + x;
}

我想要的是如果我在下拉列表中选择1个值,则必须将option的值发送到此查询中的PHP变量:

SELECT *, (
    SELECT GROUP_CONCAT(answer) AS `option`
    FROM `survey_anweroptions` 
    WHERE survey_qID = sq.survey_qID
) `option` 
FROM `survey_questionnaire`sq
WHERE sq.survey_ID = 1 
    AND survey_qID = (**the value of option will go here**)

2 个答案:

答案 0 :(得分:1)

您应该使用name而不是id。并执行print_r($ _ POST)来检查天气数据是否到来。你也不需要javascript直到你使用ajax来调用你的php脚本

答案 1 :(得分:0)

就像@Coder说的那样输入发送到服务器的名称属性不是 id 属性

你应该将select元素更改为

<select class="form-control" id="mySelect" name="my_select" onchange="option()">

并使用

检索其值
$_POST['my_select'] // if you use POST method on form submition
$_GET['my_select'] // if you use GET method on form submition
$_REQUEST['my_select'] // if you accept any method