tags:

views:

385

answers:

6

hi, im trying to include javascript variables into php code as php variable but having problems doing so. When a button is clicked, the following function is called:

<script type="text/javascript">

  function addTraining(leve, name, date)
  {
    var level_var = document.getElementById(leve);
    var training_name_var = document.getElementById(name);
    var training_date_var = document.getElementById(date);

    <?php 
        $result = "INSERT INTO training(level, school_name, training_date) VALUES('level_var', 'training_name_var', 'training_date_var')" or die("query not possible");
    ?>

</script>

any help will be appreciated. thanks

+7  A: 

PHP is run server-side. JavaScript is run client-side in the browser of the user requesting the page. By the time the JavaScript is executed, there is no access to PHP on the server whatsoever. Please read this article with details about client-side vs server-side coding.

What happens in a nutshell is this:

  • You click a link in your browser on your computer under your desk
  • The browser creates an HTTP request and sends it to a server on the Internet
  • The server checks if he can handle the request
  • If the request is for a PHP page, the PHP interpreter is started
  • The PHP interpreter will run all PHP code in the page you requested
  • The PHP interpreter will NOT run any JS code, because it has no clue about it
  • The server will send the page assembled by the interpreter back to your browser
  • Your browser will render the page and show it to you
  • JavaScript is executed on your computer

In your case, PHP will write the JS code into the page, so it can be executed when the page is rendered in your browser. By that time, the PHP part in your JS snippet does no longer exist. It was executed on the server already. It created a variable $result that contained a SQL query string. You didn't use it, so when the page is send back to your browser, it's gone. Have a look at the sourcecode when the page is rendered in your browser. You will see that there is nothing at the position you put the PHP code.

The only way to do what you are looking to do is either:

  • do a redirect to a PHP script or
  • do an AJAX call to a PHP script

with the values you want to be insert into the database.

Gordon
thanks, any sugestion?
Selom
@Selom I gave two suggestions in my answer
Gordon
im not too much in ajax, can u give me a simple a quick tutorial?thanks
Selom
@Selom http://www.w3schools.com/Ajax/Default.Asp
Gordon
+3  A: 

You seem to be confusing client-side and server side code. When the button is clicked you need to send (post, get) the variables to the server where the php can be executed. You can either submit the page or use an ajax call to submit just the data. -don

Don Dickinson
Don is right here. Look up a simple ajax script using json (if it's supported by your server) and this should be an easy task.
askon
This is the way to go. You can use jquery or another js framework to make really easy ajax calls.
ign
im not too much in ajax, can u give me a simple a quick tutorial?thanks
Selom
it is easiest to use a framework like dojo or some other ajax wrapper (sajax, etc. there are a bunch of them) to handle the details. do a search on the internet for ajax examples and you'll find a ton of them.
Don Dickinson
+2  A: 

PHP runs on the server. It outputs some text (usually). This is then parsed by the client.

During and after the parsing on the client, JavaScript runs. At this stage it is too late for the PHP script to do anything.

If you want to get anything back to PHP you need to make a new HTTP request and include the data in it (either in the query string (GET data) or message body (POST data).

You can do this by:

  • Setting location (GET only)
  • Submitting a form (with the FormElement.submit() method)
  • Using the XMLHttpRequest object (the technique commonly known as Ajax). Various libraries do some of the heavy lifting for you here, e.g. YUI or jQuery.

Which ever option you choose, the PHP is essentially the same. Read from $_GET or $_POST, run your database code, then return some data to the client.

David Dorward
A: 

You will have to post the result to the server and have a php-script to receive the result; this can be done in most javascript frameworks pretty effortlessly if you want to avoid a reload of the page.

jQuery example; http://api.jquery.com/jQuery.post/

Xipe
A: 

You can do what you want, but not like that. What you need to do is make an AJAX request from JavaScript back to the server where a separate PHP script can do the database operation.

dj_segfault
A: 

I had a same problem few weeks ago like yours; but I invented a brilliant solution for exchanging vars between PHP and JavaScript. it worked for me well:

1-Create a hidden form on a HTML page

2-Create a Textbox or Textarea in that hidden form

3-After all of your codes written in the script,store the final value of your variable in that textbax

4-Use $_REQUEST['textbox name'] line in your PHP to gain access to value of your JS var.

Hope this trick works for you

Sepehr