简体   繁体   English

使用XMLHTTPRequest根据php响应更改变量

[英]Use XMLHTTPRequest to change variable based on php response

I'm trying to write a function that removes an element from a select box but only if the element isn't being used for something else. 我试图编写一个从选择框中删除元素的函数,但前提是该元素未用于其他用途。 For example, if the select box has a list of student names, it checks if the student name is being used somewhere (or stored in a database) first. 例如,如果选择框具有学生姓名列表,则它首先检查学生姓名是否在某处使用(或存储在数据库中)。 The check script simply echos "true" or "false" depending on if the name is in use. 检查脚本根据名称是否在使用中简单地回显“ true”或“ false”。 If the name isn't used anymore, then it proceeds to remove it. 如果不再使用该名称,则将其删除。 Otherwise, it alerts you. 否则,它会提醒您。

function removeListItem(selectBox, value)
{
  var removed = 0;
  for (var x = 0; x < selectBox.options.length; x++) {
    if (selectBox.options[x].value == value) {
      var formdata = new FormData();
      var check = "false";
      formdata.append('number', value);
      var ajax = new XMLHttpRequest();
      ajax.open("POST", "check.php");
      ajax.send(formdata);
      ajax.onreadystatechange = function() {
        if (ajax.readyState == 4 && ajax.status == 200) {
          check = ajax.reponseText;
          if (check == "true") {
            alert("You have selected a number in use!");
          }
        }
      }
      if (check == "false") {
        selectBox.remove(x);
        removed = 1;
      }
    }
  }
  return removed;
}

The problem is that it pretty much never runs the check (to my observation), so an alert is never issued when I'm trying to remove something I shouldn't. 问题在于,它几乎永远不会运行检查(据我观察),因此当我尝试删除不该执行的操作时,永远不会发出警报。

I think this has something to do with synchronous vs. asynchronous javascript, so I was wondering what I should do in order for me to get the desired functionality. 我认为这与同步与异步javascript有关,所以我想知道应该怎么做才能获得所需的功能。

I want to return removed because it is being added to a counter when I call removeListItem. 我想返回已删除,因为当我调用removeListItem时将其添加到计数器中。 I want a counter because the selectBox allows for multiple removals at the same time, so I'm keeping track of each removal. 我想要一个计数器,因为selectBox允许同时进行多次删除,因此我一直在跟踪每次删除。

Yes, you are right. 是的,你是对的。 Try to move removing dom element inside onreadystatechange callback. 尝试在onreadystatechange回调中移动删除dom元素。

And your function should return promise to work with async request. 并且您的函数应该返回承诺以与异步请求一起工作。 Because function call will be finished before XMLHttpRequest. 因为函数调用将在XMLHttpRequest之前完成。

function removeListItem(selectBox, value)
  {
      var promise = new Promise(function(resolve, reject) {
        var removed = 0;
        for (var x = 0; x < selectBox.options.length; x++)
        {
          if(selectBox.options[x].value == value)
          {
            var formdata = new FormData();
            var check = "false";
            formdata.append('number', value);
            var ajax = new XMLHttpRequest();
            ajax.open("POST", "check.php");
            ajax.send(formdata);
            ajax.onreadystatechange = function() 
            {
              if (ajax.readyState == 4 && ajax.status == 200) 
              {
                check = ajax.reponseText;
                if(check == "true")
                {
                  alert("You have selected a number in use!");
                  reject();
                } 
                else 
                {
                  selectBox.remove(x);
                  removed = 1;
                  resolve(removed);    
                }
              }
            }
          }
        }
      });

      return promise;
  }

to use this function, call 要使用此功能,请致电

removeListItem(selectBox, 1).then(function(removed){ 
  // removed successful
}).catch(function() {
  // doesn't removed
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM