Muaj pes tsawg zaus koj tau ntsib qhov tseeb tias koj nkag mus rau cov ntaub ntawv tsis raug, vim tias nplooj ntawv rov rub tawm thiab tshem tawm tag nrho cov cim nkag hauv cov teb. Txhawm rau txhim kho qhov no, muaj txoj hauv kev nrov nrov los tsim tus neeg siv interface, thiab nws lub npe yog ajax. Nws tshwm sim hauv ntau qhov haujlwm thiab siv ntau txoj hauv kev.
Xa daim ntawv ajax: txuas cov tsev qiv ntawv
suav nrog jquery tsev qiv ntawv hauv index.php.
Muaj lwm txoj hauv kev suav nrog jquery hauv ib daim ntawv. Koj yuav tsum rub tawm lub tsev qiv ntawv los ntawm jquery official site, muab tso rau hauv txoj cai nplaub tshev thiab paste qhov txuas rau nws zoo li no:
Kev sib txuas thiab teeb tsa cov ntaub ntawv
1. Tsim ib daim ntawv.php hauv daim nplaub tshev nrog lub xaib nrog txhua lub npe yooj yim rau koj - qhov no yuav xa daim ntawv ajax php. Nyob rau hauv nws koj muaj peev xwm sau nyob rau hauv dab tsi hom ntawv cov ntawv nrog cov lus yuav tso tawm kom pom. Piv txwv li, form1.php.
2. Hauv koj cov ntaub ntawv javascript folder, tsim ib cov ntaub ntawv.js nrogib qho yooj yim lub npe. Piv txwv li, form.js.
3. Txuas lub nplaub tshev no rau koj daim ntawv.
4. Tsim ib daim ntawv nrog cov kev txwv hauv qab no:
Nyob hauv nws, tsis txhob hnov qab tsim teb rau nkag rau koj cov ntaub ntawv.
5. Mus rau cov ntaub ntawv form1.php hauv cov npe nrog lub xaib, uas sau:
Tam sim no, thaum xa daim foos, tus browser yuav tso tawm cov ntaub ntawv hais txog cov ntaub ntawv.
Nyob hauv tib cov ntaub ntawv, koj tuaj yeem sau qhov tseeb yuav tshwm sim lossis yuav ua li cas. Koj tuaj yeem sau cov voj voog lossis algorithms ntawm no.
Xa ajax jquery form
1. Hauv cov ntaub ntawv tsim form.js, koj yuav tsum tau sau cov cai uas yog lub luag haujlwm ua cov ntaub ntawv ua haujlwm tom qab nplooj ntawv nplooj ntawv tau puv tag.
$(document).ready(function(){ //Our code next will run here });
2. Tom qab ntawd koj yuav tsum customize lub xa khawm. Ua txhua yam hauv tib cov ntaub ntawv.
$("form").submit(function(event) { event.preventDefault(); // cov cai hauv qab no yuav sau rau ntawm no });
thawj ntu ntawm txoj cai yog lub luag haujlwm xaiv lub ntsiab ntawm nplooj ntawv, thiab qhov thib ob yog lub luag haujlwm tiv thaiv qhov kev ua txhaum cai.
3. Tom qab ntawd, piv txwv li, muab daim ntawv ajax xa mus rau qhov ua tiav.
$.ajax({type: $(this).attr('method'), url: $(this).attr('action'), data: new FormData(this), cov ntsiab lusType: cuav, cache: cuav, txheej txheemData: cuav, kev vam meej: muaj nuj nqi(tseem ceeb){ ceeb toom(tshem tawm); } });
Cov hauv qab no yog cov lus piav qhia ntxaws ntawm txhua qhov chaw.
- type -qhov no yog hom kev thov uas xa tuaj rau hauv daim ntawv; txij li nws raug nqi POST, hom kev thov yuav tsim nyog;
- qhov no - xaiv cov khoom hauv qhov tsim;
- attr - luv rau kev nyiam (kev nyiam), uas yog, ib qho kev txwv ntawm lub hom phiaj xaiv (daim ntawv) yog nyiam;
- url - parameter lub luag haujlwm rau qhov chaw thov yuav raug xa mus; Nyob rau hauv cov ntaub ntawv no, dab tsi yog sau nyob rau hauv daim ntawv parameters (form1.php);
- data - qhia daim ntawv cov ntaub ntawv;
- contentType - lub luag haujlwm xa cov headers rau lub server; Hauv qhov no nws tsis xav tau;
- cache - lub luag haujlwm rau txuag tus neeg siv lub cache;
- processData - lub luag haujlwm rau hloov cov ntaub ntawv rau ib txoj hlua;
- success - qhia cov txiaj ntsig ntawm kev xa cov ntaub ntawv tiav; Yog li ntawd, yog tias xa cov ntaub ntawv ua tiav, ces cov haujlwm ntawm kev ua haujlwm raug tua.
4. Ua tiav, tam sim no thaum xa daim foos ajax, koj yuav tau txais cov ntaub ntawv yam tsis muaj nplooj ntawv tshiab.
Qhov tshwm sim tuaj yeem hloov pauv siv cov ntaub ntawv form1.php, qhov twg koj tuaj yeem qhia meej tias qhov twg yuav tshwm sim. Piv txwv li, Koj tuaj yeem sim thiab tsim daim tshev rau qhov tseeb ntawm kev nkag mus rau qee cov ntaub ntawv: yog tias cov ntaub ntawv tsis raug, ces cov lus xav tau tshwm sim, txwv tsis pub nws rov mus rau nplooj ntawv raug. Muaj ntau yam ua tau, txawm koj lub siab xav.
Tseem tseem xa cov ntaub ntawv mus rau lub server asynchronously. Qhov no yog thaum tus neeg siv nkag mus rau cov ntawv nyeem, thiab nws tseem ceeb tam sim ntawd hauv liab, qhia tias cov ntaub ntawv nkag tsis raug. Txog nwsMuaj ntau phau ntawv qhia hauv Is Taws Nem, qhov twg txhua yam tau piav qhia meej thiab qhia nrog cov piv txwv.
Zoo kawg
Undoubtedly, ajax yog cov cuab yeej siv tau zoo hauv kev tsim lub vev xaib. Txhawm rau ua cov nplooj ntawv zoo thiab cov interfaces, nws tsuas yog tsim nyog. Nws yog tsim nyog sau cia tias nws tseem ceeb heev uas yuav tsum paub jQuery kom nkag siab tag nrho cov duab thiab qhov uas tau sau rau hauv cov cai, vim tias daim ntawv luam yooj yim tsis tuaj yeem pab thiab qhia koj kom nkag siab cov cai. Nws yog ib txwm tsim nyog nco ntsoov tias cov lus hloov kho tshiab thiab qee qhov nta yuav ploj mus. Yog li ntawd, tsis yog txhua qhov kev daws teeb meem yuav cuam tshuam, feem ntau cov ntawv sau yooj yim tsis ua haujlwm lossis tsis tsim cov txiaj ntsig uas koj xav pom ntawm koj lub vijtsam.