Yuav Ua Li Cas Kom Dhau Tus Kuj Sib Txawv Thiab

Cov txheej txheem:

Yuav Ua Li Cas Kom Dhau Tus Kuj Sib Txawv Thiab
Yuav Ua Li Cas Kom Dhau Tus Kuj Sib Txawv Thiab

Video: Yuav Ua Li Cas Kom Dhau Tus Kuj Sib Txawv Thiab

Video: Yuav Ua Li Cas Kom Dhau Tus Kuj Sib Txawv Thiab
Video: Kab tes Muaj Nyiaj thiab kab tes Txom Nyem sib txawv li cas? 2024, Hlis ntuj nqeg
Anonim

Los npaj kev sib tham sib txuas lus ntawm tus qhua thiab lub vev xaib (lossis theej, browser nrog lub web server), tus programmer yuav tsum muab cov xwm txheej rau kev hloov cov ntaub ntawv ntawm lawv. Cia peb xav txog ntau txoj kev xaiv yooj yim rau kev npaj cov kev hloov ntawm kev hloov pauv los ntawm cov neeg siv JavaScrip rau tsab ntawv server PHP tsab ntawv thiab hloov ua lwm yam.

Kev xa cov ntaub ntawv ntawm PHP rau JavaScript thiab hloov ua lwm yam
Kev xa cov ntaub ntawv ntawm PHP rau JavaScript thiab hloov ua lwm yam

Nws yog qhov tsim nyog

Yooj yim kev paub lus PHP, JavaScript thiab HTML

Cov Lus Qhia

Kauj ruam 1

Nyob rau theem ntawm nplooj ntawv tsim, nws tsis yog qhov nyuaj hloov hloov ib qho sib txawv nrog rau nws cov nqi los ntawm php tsab ntawv mus rau JavaScript tsab ntawv. Daim ntawv PHP nws tus kheej tsim cov HTML code ntawm nplooj ntawv thov, suav nrog cov ntawv sau nws muaj. Qhov no txhais tau tias nws tuaj yeem sau cov ntawv hloov mus rau hauv JavaScript code uas yuav tsum tau dhau nrog lawv cov nqi. Piv txwv li, php tsab ntawv no yuav dhau mus rau tus neeg siv ntawv lub npe sib txawv hu ua "serverTime" uas muaj lub sijhawm server tam sim no hauv hom HOUR: MINUTE:

<? php

$ JSvarName = 'serverTime';

$ JSvarValue = hnub ('H: i');

$ JScode = $ JSvarName. '= "'. $ JSvarValue. '";';

luam ''. $ JScode.'alert ("Thiab nyob rau ntawm lub server tam sim no" + '. $ JSvarName.'); '

?>

Dhau qhov sib txawv thiab nws tus nqi ntawm PHP rau Javascript
Dhau qhov sib txawv thiab nws tus nqi ntawm PHP rau Javascript

Kauj ruam 2

Txoj kev yooj yim tshaj plaws ntawm kev xa cov npe thiab cov nqi ntawm cov kev hloov pauv nyob rau hauv cov lus qhia rov qab (los ntawm JS tsab ntawv hauv tus neeg siv lub browser mus rau PHP tsab ntawv ntawm lub web server) tuaj yeem saib zoo li qhov no nyob hauv HTML code ntawm nplooj:

var tam sim no = tshiab hnub ();

var varName = 'clientTime';

var varValue = now.getHours () + ":" + now.getMinutes ();

window.location.href = 'https://sa/test2.php?' + varName + '=' varValue;

Tsab ntawv no yuav xa mus rau tsab ntawv test2.php lub npe "clientTime" kuj sib txawv thiab nws cov nqi muaj lub sijhawm siv computer tam sim no nyob rau tib hom HOUR: MINUTE Txoj kev no ntawm kev hloov cov ntaub ntawv hu ua "synchronous" - nws yuav ua rau rov qab nplooj ntawv nrawm. Ntau dua precisely, hloov ntawm nplooj ntawv tam sim no, qhov tshwm sim ntawm test2.php tsab ntawv yuav muab ntim rau hauv qhov browser. Tus lej rau tsab ntawv no php tej zaum yuav zoo li no:

<? php

yog ($ _ GET) ncha 'Tau txais kev hloov pauv'.key ($ _ GET). '='. $ _ GET [key ($ _ GET)];

?>

Koj tuaj yeem ua ke tag nrho peb qhov kev txiav txim siab ntawm tus lej rau kev hloov pauv ntawm lub server mus rau browser thiab rov qab mus rau hauv ib qho php ntaub ntawv zoo li no:

<? php

yog ($ _ GET) ncha 'Tau txais kev hloov pauv'.key ($ _ GET). '='. $ _ GET [key ($ _ GET)];

$ JSvarName = 'serverTime';

$ JSvarValue = hnub ('H: i');

$ JScode = $ JSvarName. '= "'. $ JSvarValue. '";';

luam ''. $ JScode.'alert ("Thiab nyob rau ntawm lub server tam sim no" + '. $ JSvarName.'); '

?>

muaj nuj nqi xaData () {

var tam sim no = tshiab hnub ();

var varName = 'clientTime';

var varValue = now.getHours () + ":" + now.getMinutes ();

window.location.href = "https://sa/test2.php?" + varName + "=" + varValue;

rov qab cuav;

}

Xa cov ntaub ntawv rau server Hauv no sau ua ke (PHP + JavaScript) tsab ntawv, php code yuav tsim cov cai JavaScript los ntawm "dhau" tus kuj sib txawv npe hu ua "serverTime" nrog tus nqi uas muaj lub sijhawm server tam sim no. Thaum nplooj ntawv tau ntim rau hauv qhov browser, sau JavaScript JavaScript yuav tso tawm cov lus nrog lub sijhawm no. Tom qab ntawd tus neeg siv nyem rau ntawm "Xa cov ntaub ntawv mus rau server" link yuav tso qhov kev xa sendData (), uas yuav xa GET thov rau tus neeg rau zaub mov, dhau cov npe hu ua sib txawv ("clientTime") thiab nws tus nqi (lub sijhawm tus neeg siv) mus rau php tsab ntawv. Ib tsab ntawv php, uas tau nyeem lub npe thiab tus nqi ntawm tus nqi sib txawv ntawm $ _GET superglobal array, yuav luam nws thiab pib tag nrho cov ntawv sau piav dua.

Kev pauv hloov pauv thiab lawv cov txiaj ntsig ntawm PHP thiab JavaScript
Kev pauv hloov pauv thiab lawv cov txiaj ntsig ntawm PHP thiab JavaScript

Kauj ruam 3

Txhua yam piav qhia saum toj no piv txwv qhov tshwm sim ntawm "synchronous" hloov ntaub ntawv. Kev siv ntawm "asynchronous" txoj kev ntawm kev sib pauv cov ntaub ntawv ntawm cov neeg thiab cov server scripts muaj nws lub npe AJAX (Asynchronous Javascript thiab XML). Cov ncauj lus no tsim nyog cais lub ntsiab lus.

Pom zoo: