单击按钮时填充javascript变量


fill javascript variable when button is clicked

对于与api组合构建分页,当我按下下一个按钮时,我希望将值next分配给javascript变量,以便将其发送到PHP脚本。到目前为止,我有这个,但我对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(排序){var ajaxRequest;//Ajax 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;
    }
  }
}

如何使Javascript变量(nextpage)保持为空,直到按下按钮(name="nextpage"value="nextpage")?按下时,javascript变量nextpage应包含"nextpage"

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;
    }
  }
}

如果我答对了你的问题,你的问题是nextpage = [...]

试试这个方法:

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

以下是一个示例:https://jsfiddle.net/nmLeetgu/