-1

Here is my Code I want to move my Variable of "Hellow"

from JavaScript

to PHP Query at: document.write("<?php $result = mysql_query("SELECT Urdu FROM translate where English= 'Variable'"); ?>");.

Kindly any help?

   <script type='text/javascript'>

   var Variable = "Hellow";


document.write("<?php


$db = mysql_connect("localhost","root","");
mysql_select_db("Dictionary",$db);

?>");

document.write("<?php $result = mysql_query("SELECT Urdu FROM translate where English=     'Variable'"); ?>");

document.write("<?php

 while($row = mysql_fetch_array($result))
  {

echo  $row['Urdu'] ;

 }


mysql_close($db); 

?>");

</script>
user3666197
  • 1
  • 6
  • 50
  • 92
  • 1
    This is not how it works - php has ran and closed by time any javascript is executed. Time to brush up on the fundementals – Steve Oct 13 '14 at 21:05
  • PHP is executed server-side. Javascript is executed client-side. You cannot include PHP code with `document.write`. Looking at your code, I'm not sure the purpose of using javascript here. – showdev Oct 13 '14 at 21:06
  • possible duplicate of [How can I use a JavaScript variable as a PHP variable?](http://stackoverflow.com/questions/2379224/how-can-i-use-a-javascript-variable-as-a-php-variable) – Brian Oct 13 '14 at 21:07
  • Any Suggestion in Coding Please? – user3674520 Oct 13 '14 at 21:07
  • 1
    Just use PHP and eliminate the javascript. If your variable originates from javascript, then you may want to investigate using [AJAX](https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started). – showdev Oct 13 '14 at 21:14

2 Answers2

0

You can't/should not do what you propose. JS is executed client side, while PHP is a server side language, and even if it is possible to send a file to the server and execute it, it would come with a host of security issues.

What you could do however is to make a get/ajax request that sends your variable to a php script, but remember to clean up the variable before using it in any mysql query.

mephisto73
  • 98
  • 7
0

What you are trying to do won't work because the PHP processing happens earlier in the stack than the Javascript. You will need to use ajax to send data back to the server once the page has loaded.

jQuery can make this pretty easy for you. Look up $.ajax methods for jquery.

Ryan Tuosto
  • 1,941
  • 15
  • 23