-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>
5
  • 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 Commented Oct 13, 2014 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. Commented Oct 13, 2014 at 21:06
  • possible duplicate of How can I use a JavaScript variable as a PHP variable? Commented Oct 13, 2014 at 21:07
  • Any Suggestion in Coding Please? Commented Oct 13, 2014 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. Commented Oct 13, 2014 at 21:14

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.