tirso
tirso

Reputation: 253

javascript variable into php

I have a php function, in this function I have a javascript which get the value of my element from parent window. My problem now is, how can I pass this value into php variables

Any help would greatly appreciated.

Thanks

Tirso

I tried this code but I got value always 1

here is my code

function do_upload()
{   
    $categories = print '<script type="text/javascript">window.top.window.$("#categories").val();</script>';
}    

the value I've got for $categories is always 1;

Upvotes: 1

Views: 342

Answers (3)

meder omuraliev
meder omuraliev

Reputation: 186562

The best you can do is make an ajax call to a server-side page which invokes the do_upload method for you:

$.ajax({ 
    url: '/ajax.php', 
    data: 'category=' + window.top.window.$('#categories').val(), 
    type:'POST', 
    success:function(html){
    } 
});

ajax.php:

if ( isset( $_POST['category'] ) ) {
    do_upload( $_POST['category'] );
}

Don't forget to filter the $_POST data to make sure its in the format you want.

Upvotes: 0

farzad
farzad

Reputation: 8855

PHP code runs on the server (before your code is sent to the client browser), and JavaScript on the client (after the code is sent to the client browser). I'm not sure there is any other way to pass information to the server, other than sending a request to the server. so if you want to send something to your PHP application (server side) you'll have to send a request from your JavaScript code (client side). you may use AJAX techniques, but this causes another PHP process on the server to start. so you'll have to find a way to communication between the base PHP process (who created the JavaScript) and the new process (called by the JavaScript) to pass information. you may use server side data persistence like sessions or files or databases, or inter-process communication solutions like shared memory. but I guess this will get a little more complicated in implementation.

Upvotes: 0

Asaph
Asaph

Reputation: 162771

The PHP print() language construct always returns 1.

PHP executes on the server side. JavaScript executes on the client side. You cannot mix them directly. The best you could do would be to have JavaScript make an AJAX call to a PHP script on the server.

Upvotes: 2

Related Questions