-3

I've been trying to find out the response for this because I think it's not difficult to do, but I cannot find it.

I basically want to "recall" a mysql query that works. I have an HTML button and a span so I call a function in javascript which has a php code with the Mysql query and the result that is store in my span, works fine, but... Why I cannot click the button again to get the update results from Mysql table if I changed it ?

The syntax is something like ( in a single php file for test):

<script type="text/javascript">
function test(){
    <?php include 'dbconnect.php';
     $query = 'SELECT * FROM data';
     $results = mysql_query($query);
     while ($row = mysql_fetch_assoc($results)) {
         $data = $data . "row[name]<br>";
     }
     mysql_close();
  ?>
  testspan.innerHTML = <?php echo $data ?>';
}

</script>

<HTML><BODY>
<input type='button' onclick='test();'/><br>
<span id="testspan"></span
</BODY></HTML>

Resuming: when I click the first time the data appears but not the second time onwards so the Mysql query can be call just first time? anything related with the variables ?

dcastro
  • 66,540
  • 21
  • 145
  • 155
Olderdais
  • 21
  • 1
  • 2
    You should check how web servers work before trying to write code. Then you move onto PHP. – planestepper Feb 16 '14 at 14:26
  • You should load results with AJAX. The URL you will use will execute the query. Then the results will be inserted in your desired html tag. See http://stackoverflow.com/a/6570220/1294631 – ggirtsou Feb 16 '14 at 14:27

1 Answers1

1

Php is ServerSide... JS is ClientSide... you can't do code php in code JS you must use AJAX :https://api.jquery.com/jQuery.ajax/

Carbos
  • 117
  • 8
  • *"you can't do code php in code JS you must use AJAX"* - True, but it's not *entirely* true. You can do PHP inside JS, you just can't do what you want inside it; one is limited but not constrained. This works `var player = "";` - You can even do PHP inside CSS also `border: 1px red;` – Funk Forty Niner Feb 16 '14 at 15:16
  • Yes it is true , sorry – Carbos Feb 16 '14 at 15:20
  • Not a problem. However, mixing JS with (pure) PHP is not recommended (and vice-versa), and should only be used sparingly ;-) – Funk Forty Niner Feb 16 '14 at 15:26