1

Hello I have a problem on how should I send a JavaScript value to PHP.

Here is my form:

enter image description here

Dropdown Question from database:

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

What I want is if I select 1 value on the drop-down, the value of option must be sent to a PHP variable in this query:

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**)
3
  • you can use ajax here. pass the from values through JavaScript and make ajax call. get the variable values in ajax and run your query Commented May 1, 2018 at 7:10
  • 2
    AJAX is your only solution here. JavaScript cannot update a PHP variable Commented May 1, 2018 at 7:29
  • sad to here that @hungrykoala Commented May 1, 2018 at 8:17

2 Answers 2

1

You should use name instead of id. and do print_r($_POST) to check weather post data coming or not. also you dont need javascript for this until you are using ajax to call your php script

Sign up to request clarification or add additional context in comments.

Comments

0

like @Coder said you the inputs send to the server with it's name attribute not id attribute

you should change the select element to

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

and retrieve it's value with

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.