2016-05-02 28 views
0

對於與api結合的構建分頁,當我按下下一個按鈕時,我希望將值next分配給一個javascript變量,以便它可以發送到PHP腳本。我有這到目前爲止,但我不是很熟悉JavaScript:當點擊按鈕時填充javascript變量

nextpage = document.getElementById('nextpage').onclick = document.getElementById('nextpage').value; 

if(!nextpage==0){ 
    link =link+"&nextpage="+nextpage; 
} 

這樣的JavaScript變量被髮布到URL等等,另一方面PHP腳本可以把它撿起來像這樣。對於其他變量使用:

mantinee = document.zoekform.mantinee.value; 

if(!mantinee==0){ 
    link =link+"&mantinee="+mantinee; 
} 

如果我這樣做,那麼它直接發佈到URL,這樣的PHP腳本總是認爲它需要跳到下一個頁面是不是我的本意。點擊時,其側面的按鈕將調用ajaxgetinfo()

query.php

if(isset($_POST['nextpage'])) 
{ 
    $nextpage = $_POST['nextpage']; 
} 

這裏是所有得到每個變量並將它們傳送的JavaScript。這是那裏的下一頁也需要運行

function vorige(){ 
sort = 'vorige'; 
ajaxGetInfo(sort); 
} 

功能ajaxGetInfo(排序) { 變種ajaxRequest; //德可變笏阿賈克斯mogelijk maakt

try{ 
    // Chrome, Opera 8.0+, Firefox, Safari 
    ajaxRequest = new XMLHttpRequest(); 
} catch (e){ 
    // Internet Explorer Browsers 
    try{ 
     ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
    } catch (e) { 
     try{ 
      ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
     } catch (e){ 
      // Something went wrong 
      alert("Your browser broke!"); 
      return false; 
     } 
    } 
} 

link="query.php?"; 

datumreeks=''; 
if(document.zoekform.zo.checked==true){datumreeks="zondag";} 
if(document.zoekform.ma.checked==true){datumreeks=datumreeks+"maandag-";} 
if(document.zoekform.di.checked==true){datumreeks=datumreeks+"dinsdag-";} 
if(document.zoekform.wo.checked==true){datumreeks=datumreeks+"woensdag-";} 
if(document.zoekform.don.checked==true){datumreeks=datumreeks+"donderdag-";} 
if(document.zoekform.vr.checked==true){datumreeks=datumreeks+"vrijdag-";} 
if(document.zoekform.za.checked==true){datumreeks=datumreeks+"zaterdag-";} 
link = link+"&datumreeks="+datumreeks; 

datepicker = document.zoekform.datepicker.value; 
if(!datepicker==0){link =link+"&datepicker="+datepicker;} 

datepicker2 = document.zoekform.datepicker2.value; 
if(!datepicker2==0){link =link+"&datepicker2="+datepicker2;} 

zaal = document.zoekform.zaal.value; 
if(!zaal==0){link =link+"&zaal="+zaal;} 

genre = document.zoekform.genre.value; 
if(!genre==0){link =link+"&genre="+genre;} 

profiel = document.zoekform.profiel.value; 
if(!profiel==0){link =link+"&profiel="+profiel;} 

internationaal = document.zoekform.internationaal.value; 
if(!internationaal==0){link =link+"&internationaal="+internationaal;} 

prijslaag = document.zoekform.prijslaag.value; 
if(!prijslaag==0){link =link+"&prijslaag="+prijslaag;} 

prijshoog = document.zoekform.prijshoog.value; 
if(!prijshoog==0){link =link+"&prijshoog="+prijshoog;} 

mantinee = document.zoekform.mantinee.value; 
if(!mantinee==0){link =link+"&mantinee="+mantinee;} 

document.getElementById('nextpage').onclick = function(e){ 
    ajaxRequest.open("POST", link, true); 
    if (nextpage) ajaxRequest.send("nextpage=yes"); 
} 

ajaxRequest.open("GET", link, true); 
ajaxRequest.send(null); 
document.getElementById("info").innerHTML = '<div align=center><i class="material-icons w3-spin w3-jumbo">refresh</i></br>Blijft dit staan? <a href="" onclick="ajaxGetInfo()">Klik hier.</a></div>'; 


ajaxRequest.onreadystatechange = function(){ 
    if(ajaxRequest.readyState == 4){ 
     document.getElementById("info").innerHTML = ajaxRequest.responseText; 
    } 
    } 
} 

我怎樣才能讓這個直到按鈕(name="nextpage"value="nextpage")按下JavaScript變量(nextpage)仍然是空的?當按下時,JavaScript變量nextpage應該包含「下一頁」

回答

2

POST而不是使用GET的變量:

document.getElementById('nextpage').onclick = function(e){ 

    ajaxRequest.open("POST", link, true); 
    if (nextpage) ajaxRequest.send("nextpage=yes"); 
    document.getElementById("info").innerHTML = '<div align=center><i class="material-icons w3-spin w3-jumbo">refresh</i></br>Blijft dit staan? <a href="" onclick="ajaxGetInfo()">Klik hier.</a></div>'; 


    ajaxRequest.onreadystatechange = function(){ 
    if(ajaxRequest.readyState == 4){ 
     document.getElementById("info").innerHTML = ajaxRequest.responseText; 
    } 
    } 
} 
+0

但不會有任何按鈕,點擊?並提交刷新頁面。我用選項卡建立網站,所以刷新不是一個選項 – Mark

+0

也應該只填充nextpage的值時按其他明智需要有一個不同的值,這將是隨機的,因爲如果該值爲空腳本將不會運行 – Mark

+0

你在你的問題中說過會有'下一個'按鈕?自您編輯問題後,我不理解您的問題。 – markt

0

你的問題是nextpage = [...]如果我得到你的問題的權利。

試試這樣說:

document.getElementById('nextpage').onclick = function(e){ 
    //do your request etc 
} 

這裏有一個例子:https://jsfiddle.net/nmLeetgu/

+0

這幾乎可以避免頁面不加載的事實。我已經在原帖 – Mark

+0

上發佈了完整的javascript,我設法讓它貼到鏈接上。但是當按鈕被點擊時,innerhtml不會重新加載其重新加載的其他值 – Mark