0

How to set php mysql from javascript var or input type text ?

This is my php mysql code:

<?PHP
$count = 1;
$query = "SELECT * FROM user WHERE ORDER BY RAND()";
$result = mysql_query($query) or die(mysql_error());
while( ($row = mysql_fetch_array($result)) && ($count <= 36))
{
    $uid = $row['uid'];
    $count++;
}
?>

And this is my javascript for check clientwidth and set value into input type text

<input type="text" id="width"/>
<script>
var width=window.innerWidth || document.documentElement.clientWidth
document.getElementById("width").value = width;
</script>

I want to do like this

if var javascript width > 1599 i want to use this code php my sql

<?PHP
$count = 1;
$query = "SELECT * FROM user WHERE ORDER BY RAND()";
$result = mysql_query($query) or die(mysql_error());
while( ($row = mysql_fetch_array($result)) && ($count <= 40))
{
    $uid = $row['uid'];
    $count++;
}
?>

else width <= 1599

<?PHP
$count = 1;
$query = "SELECT * FROM user WHERE ORDER BY RAND()";
$result = mysql_query($query) or die(mysql_error());
while( ($row = mysql_fetch_array($result)) && ($count <= 36))
{
    $uid = $row['uid'];
    $count++;
}
?>

How can i do that?

thank you

Insane Skull
  • 9,220
  • 9
  • 44
  • 63
mongmong seesee
  • 987
  • 1
  • 13
  • 24
  • 1
    On the surface, this looks like a dupe of http://stackoverflow.com/questions/1917576/how-to-pass-javascript-variables-to-php but I think there are some deeper lessons that could be learned by the OP. @mongmongseesee, why do you want to do this? –  Feb 15 '16 at 04:03
  • u may take use of cookies – Peyman Mohamadpour Feb 15 '16 at 04:17

1 Answers1

0

JavaScript is a client-side script language execute on client web browser but PHP is a server-side script language execute on server.

Therefore, to archive triggering server-side script by client side script, you might need to create a server-side endpoint and trigger the endpoint with AJAX.

You can find an example of PHP AJAX and MYSQL from here.

Solomon Tam
  • 739
  • 3
  • 11