Mal_235
Mal_235

Reputation: 53

How to pass value of the table row into the variable in modal

I have a table from which I am successfully passing values into the textfields in modal. However, I need to create the select dropdown within modal, which will be based on value passed from the row.

Table

edit | ref. number |

.edit_record button | AAA01 |

js

        $(document).ready(function () {
          $('.edit_record').on('click', function() {
           $('#edit_record_modal').modal('show');
           $tr = $(this).closest('tr');
           var data = $tr.children("td").map(function() {
             return $(this).text();
           }).get();
           console.log(data);
           $('#ref').val(data[1]);
          });
        });

modal

        <div class="form-group">
          <input id="ref" type="text" name="ref" class="form-control">
        </div>
        <div class="form-group">
          <select name="selectDate" id="selectDate" onchange="">
            <?php 
              $ref_list_result= mysqli_query($db_conn,"SELECT ref_no FROM record WHERE 
              caller_number= /*the value of row*/");
              while ($row = mysqli_fetch_array($ref_list_result)) {
                $ref_no = $row['ref_no']; 
            ?>
            <option value=<?php echo $ref_no ?>><?php  echo $ref_no ?></option>
            <?php } ?>
          </select>
        </div>

Is it possible to pass that value from #ref textfield into the php variable, so I can compare it against condition in SQL's WHERE clause? Perhaps, maybe there is a different way to do this?

Upvotes: 1

Views: 74

Answers (1)

Zirc
Zirc

Reputation: 470

Short Answer: You cant pass variables in PHP and JavaScript unless you are using an API to transfer data

Long Answer: If you only know how PHP works, it is what is called a "Pre-templater" which means that in the server, or somewhere it is deployed, the server translates PHP codes into HTML and JavaScript depending on your echo. This means that by the moment the code you've written gets sent to the browser, it is all HTML, JS and CSS.

Alternative Approach: You could try building an API using PHP, and use AJAX to modify the dropdown. Search the following in google "PHP API" and "AJAX Call On PHP".

Upvotes: 2

Related Questions