Domanda

Sto usando PHP e JavaScript. Il mio codice JavaScript contiene una funzione, get_data ():

function get_Data(){
    var name;
    var job;
    .....

    return buffer;
}

Ora ho il codice PHP con il seguente.

<?php
    $i=0;
    $buffer_data;

    /* Here I need to get the value from JavaScript get_data() of buffer;
       and assign to variable $buffer_data. */
?>

Come posso assegnare i dati della funzione JavaScript nella variabile PHP?

È stato utile?

Soluzione

Usa jQuery per inviare una variabile JavaScript al tuo file PHP:

$url = 'path/to/phpFile.php';

$.get($url, {name: get_name(), job: get_job()});

Nel tuo codice PHP, ottieni le tue variabili da $ _GET ['name'] e $ _GET ['job'] in questo modo:

<?php
    $buffer_data['name'] = 

Usa jQuery per inviare una variabile JavaScript al tuo file PHP:

$url = 'path/to/phpFile.php';

$.get($url, {name: get_name(), job: get_job()});

Nel tuo codice PHP, ottieni le tue variabili da $ _GET ['name'] e $ _GET ['job'] in questo modo:

<*>GET['name']; $buffer_data['job'] =

Usa jQuery per inviare una variabile JavaScript al tuo file PHP:

$url = 'path/to/phpFile.php';

$.get($url, {name: get_name(), job: get_job()});

Nel tuo codice PHP, ottieni le tue variabili da $ _GET ['name'] e $ _GET ['job'] in questo modo:

<*>GET['job']; ?>

Altri suggerimenti

Il codice JavaScript viene eseguito sul lato client mentre PHP viene eseguito sul lato server, quindi dovrai inviare i valori JavaScript al server. Questo potrebbe essere nascosto in $ _POST o tramite Ajax .

Se non hai esperienza con Ajax o non hai bisogno, inserisci i tuoi dati in un post / get e invia i dati alla tua pagina.

Dovresti usare Ajax come uno script sul lato client non può essere invocato dal codice lato server con i risultati disponibili sull'ambito lato server. Potrebbe essere necessario effettuare una chiamata Ajax sul lato client che imposterà la variabile PHP.

    <script>
        function get_Data(){
            var name;
            var job;
            .....
            return buffer;
        }

        function getData()
        {
            var agree=confirm("get data?");
            if (agree)
            {
                document.getElementById('javascriptOutPut').value = get_Data();
                return true;
            }
            else
            {
                return false;
            }
        }
    </script>

    <form method="post" action="" onsubmit="return getData()"/>
        <input type="submit" name="save" />
        <input type="hidden" name="javascriptOutPut" id="javascriptOutPut"/>
    </form>

    <?php
        if(isset(<*>POST['save']))
        {
            var_dump(<*>POST['javascriptOutPut']);
        }
    ?>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top