Salut
In mod obisnuit, datele din pagina web sunt trimise la php cu elemnte de formular si buton Submit.
Ceea ce vrei se poate cu Ajax. Se preia datele din codul html cu javascript si cu o functie Ajax se trimit la fisierul php.
Daca nu stii cum se lucreaza cu ajax, poti sa inveti din cursul de pe site:
https://marplo.net/ajax/ajax_post_php.html
- Personal, pentru ce am lucrat am facut functia ajaxSend() din acest exemplu:
Cod: Selectaţi tot
<script>
/* Ajax Function
Send "data" to "php", using the method added to "via", and pass response to "callback" function
data - json object with data to send, name:value; ex.: {"name1":"val1", "name2":"2"}
php - address of the php file where data is send
via - request method, a string: 'post', or 'get'
callback - function called to proccess the server response
*/
function ajaxSend(url, data, via, callback){
var obajx = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); // XMLHttpRequest object
//put data from "data" into a string to be send to "php"
var str_data ='';
for(var k in data){
k = k.toString(); //convert numeric key to string
//build the string with data to be sent
str_data += k +'='+ data[k].toString().replace(/\?/g, '%3F').replace(/=/g, '%3D').replace(/&/g, '%26').replace(/[ ]+/g, '%20').replace(/[\+]/g, '%2B') +'&';
}
str_data = str_data.replace(/&$/, ''); //delete ending &
//send data to php
obajx.open(via, url, true);
if(via =='post') obajx.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
obajx.send(str_data);
//check the state request, if completed, pass the response to callback function
obajx.onreadystatechange = function(){
if(obajx.readyState == 4){
/// alert(obajx.responseText); //for debug
callback(obajx.responseText);
}
}
}
//Example Usage
var sendata = {'id1':'val 1', 'id2':'val 2'}; //object with data to send
ajaxSend(sendata, 'test.php', 'post', function(resp){
alert(resp);
});
</script>