-1

I have a Javascript variable as "name" I want to add that to my php code. The php code uses the JS variable for a select statement. This is what I am Trying, but I am getting a syntax error;

<script type="text/javascript">
    function fillform(name){

        <?php $name = "SELECT * FROM table_name WHERE name = ". ?> name <?php ." "; ?>
            console.log(<?php echo $name; ?>);
        }   
</script>
Ilia Choly
  • 18,070
  • 14
  • 92
  • 160
user2529058
  • 83
  • 3
  • 12
  • 5
    php is a server-side language. js is a client-side language. you cannot combine it like this. You can send the variable via ajax to your server-side php code. – steven Oct 28 '13 at 17:08
  • 3
    **Danger**: Concatenating user input into SQL makes you **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make easy to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Oct 28 '13 at 17:11
  • And your PHP syntax is not good. It's more like ` name ` – Fabien Sa Oct 28 '13 at 17:11
  • @Quentin Well, no, because what he's doing doesn't work... ;) – deceze Oct 28 '13 at 17:12
  • @FabSa: It doesn't matter. It won't work even if the statement is syntactically correct. See Quentin's comment above. – Amal Murali Oct 28 '13 at 17:13
  • @AmalMurali It's not the solution of course but for his information :) – Fabien Sa Oct 28 '13 at 17:14

1 Answers1

1

PHP code is executed by the server. Then the server sends the output of that code to the client in html form.. Javascript is executed by the client so you cannot combine them.

A solution could be the use of AJAX which allows javascript to call a php file and get the output of the executed code..

Bobby Shark
  • 1,074
  • 2
  • 17
  • 33