Mysql
 SQL >> Database >  >> RDS >> Mysql

Funzione JavaScript che invia dati e richiama uno script PHP

MODIFICA 2018

Sì, sono ancora vivo. Puoi usare l'API fetch al posto di jQuery. È ampiamente supportata tranne (indovina chi?...) IE 11 e precedenti, ma esiste un polyfill per questo. Buona programmazione moderna.

Supporto dell'API fetch

Vecchia risposta

Dovrai usare AJAX. Da solo, JavaScript non può raggiungere uno script PHP. Devi effettuare una richiesta, passare la variabile a PHP, elaborarla e restituire un risultato. Se usi jQuery, inviare una richiesta ajax è piuttosto semplice:

$.ajax({ data: 'orderid=' + your_order_id, url: 'url_where_php_is_located.php', method: 'POST', // or GET success: function(msg) { alert(msg); } }); 

e il tuo script PHP dovrebbe recuperare l'id dell'ordine così:

echo $_POST['orderid']; 

L'output verrà restituito come stringa alla funzione success.

MODIFICA

Puoi anche usare le funzioni abbreviate:

$.get('target_url', { key: 'value1', key2: 'value2' }).done(function(data) { alert(data); }); // or eventually $.post instead of $.get