0

I try to assign a string type javascript variable to a php variable and retrieve a table from database, I wrote all the code in a file.

I read date from input:

<input type="date" id="myDate" >

and by a listener I run myDate function, it produce sql1 that is a string, it should pass sql1 to $sql, I tried that users explain in other Q's but I don't get the right answer :

<script>
var dateBut = document.getElementById("myDate")
function myDate() {
    var x = document.getElementById("myDate").value;
    document.getElementById("demo3").innerHTML = x;

    // sql1 string
    var sql1 = "SELECT * FROM TSE(" + document.getElementById("demo3").innerHTML +")";
    document.getElementById("demo2").innerHTML = sql1;

    var tbl = document.getElementById("demo");
    tbl.innerHTML = "";

    var ch = document.getElementById("checkerWork");


    <?php
    // sql1 should pass to $sql;
    $sql = ?>"SELECT * FROM TSE(" + document.getElementById("demo3").innerHTML + ")"<?php
    mysqli_set_charset($conn, "utf8");
    $result = $conn->query($sql);

    ?>

    }
dateBut.addEventListener("change", myDate, false );

all of the code is in a php file. thanks

mohsen
  • 23
  • 1

1 Answers1

-1

I would send the contents of the variable to a PHP page via Ajax with jquery, then I would assign the data sent via get or post to the PHP variable, execute the query and return the result to the myDate () function. I'm not a native English speaker so if the answer is not clear tell me and I'll give you an example.

V.Cozzatella
  • 707
  • 2
  • 9
  • 15
  • thanks for your respond. I know it could be done by ajax, but I don't have any idea how I should do it at all. if it possible plz give a example. – mohsen Jul 29 '18 at 07:03