PHP+Ajax innerHTML 不起作用后可点击的 JS 函数

发布于 2024-12-22 21:01:17 字数 3760 浏览 1 评论 0原文

首先:我不想使用 JQuery,我知道它会让事情变得更容易,但它会破坏这个东西的功能。 注意:ajaxFunction 是 GET/POST 的默认 ajax 示例

现在,第一次单击该函数后,它确实可以工作,并且 everyrhing 被 php-echo 替换。问题是之后。

将 initia-div(“login-place”)更改为下一个 div(“regit”)后,sregit.onClick = function() 的 javascript 函数不起作用。

我该如何解决这个问题?我应该在 PHP 中创建 DOMElement 并使用 xmlSave("index.php") 吗?

初始div:

<div id="login-place">
    <table id="login-init" name="login-init">
    <tr>
        <td>username</td>
        <td><input type="text" name="user" id="user" /></td>
        <td>password</td>
        <td><input type="password" name="pass" id="pass" /></td>
    </tr>
    <tr>
        <td colspan="2"><input name="login" id="login" type="button" value="entrar" /></td>
    </tr>
    </table>
</div>

JS代码:

            var slogin = document.getElementById("login");
            slogin.onclick = function() {
                //alert("inside."+user.value);
                s = "goforth=1&user="+user.value+"&pass="+pass.value;
                ajaxFunction('4',""+s); 
            }

            var sregit = document.getElementById("regit");
            sregit.onclick = function () {
                alert("inside."+user.value);
                s = "regit=1&user="+user.value+"&pass="+pass.value;
                ajaxfunction('4',""+s); 
            }
    function ajaxFunction(arg,string) {
      var getdate = new Date();  //Used to prevent caching during ajax call
      if(xmlhttp) {

          if (arg==4) {
            nextland = "login-place";
            document.getElementById("all-burps").innerHTML=string;
            xmlhttp.open("POST","session.php",true);
          }
        xmlhttp.onreadystatechange  = handleServerResponse;
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

        if (arg==4) {
            xmlhttp.setRequestHeader('Content-length', string.length);
            xmlhttp.send(""+string); 
        }
        }
    }
function handleServerResponse() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
       if (nextland != "login-place") { document.getElementById(nextland).innerHTML=xmlhttp.responseText; } //Update the HTML Form element
       else {
           // alert(xmlhttp.responseText); -> this is true no error up here.
           var thediv = document.getElementById("login-init"); 
           thediv.parentNode.removeChild(thediv); //this happens fine.
           var theplace = document.getElementById("login-place");
           var thelement = document.createElement("div"); //this too.
           thelement.innerHTML=xmlhttp.responseText;  //this too'
           theplace.appendChild(thelement); //this too.

       }
     }

并且,innerhtml.responseText是=

        echo '
            <table id="login-2" name="login-2">
            <h3>o username ' . $username . ' não existe. <a href="#" id="register-link" name="register-link">Queres registar?</a></h3>
            <tr>
                <td>username</td>
                <td><input type="text" name="user" id="user" value="' . $username . '"/></td>
            <td>password</td>
                <td><input type="password" name="pass" id="pass" value="' . $password . '" /></td>
            </tr>
            <tr>
                <td colspan="2"><input type="button" value="entrar" id="regit" name="regit"/></td>
            </tr>
        </table>
        ';

我希望这足以让任何人理解我的疑问。否则我会重写它。

First off: I do not want to use JQuery, i know it would make it easier but it would ravish the porpuse of the thing.
Note: ajaxFunction is the default ajax exemple for a GET/POST

Now, after clicking the function, the first time, it does work and everyrhing gets replaced by the php-echo. The problem is AFTER.

After changing the initia-div ("login-place") to the next div ("regit") the javascript function of sregit.onClick = function() does not work.

How do i go around this? Should i create the DOMElement in PHP and use xmlSave("index.php")?

initial-div:

<div id="login-place">
    <table id="login-init" name="login-init">
    <tr>
        <td>username</td>
        <td><input type="text" name="user" id="user" /></td>
        <td>password</td>
        <td><input type="password" name="pass" id="pass" /></td>
    </tr>
    <tr>
        <td colspan="2"><input name="login" id="login" type="button" value="entrar" /></td>
    </tr>
    </table>
</div>

the JS code:

            var slogin = document.getElementById("login");
            slogin.onclick = function() {
                //alert("inside."+user.value);
                s = "goforth=1&user="+user.value+"&pass="+pass.value;
                ajaxFunction('4',""+s); 
            }

            var sregit = document.getElementById("regit");
            sregit.onclick = function () {
                alert("inside."+user.value);
                s = "regit=1&user="+user.value+"&pass="+pass.value;
                ajaxfunction('4',""+s); 
            }
    function ajaxFunction(arg,string) {
      var getdate = new Date();  //Used to prevent caching during ajax call
      if(xmlhttp) {

          if (arg==4) {
            nextland = "login-place";
            document.getElementById("all-burps").innerHTML=string;
            xmlhttp.open("POST","session.php",true);
          }
        xmlhttp.onreadystatechange  = handleServerResponse;
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

        if (arg==4) {
            xmlhttp.setRequestHeader('Content-length', string.length);
            xmlhttp.send(""+string); 
        }
        }
    }
function handleServerResponse() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
       if (nextland != "login-place") { document.getElementById(nextland).innerHTML=xmlhttp.responseText; } //Update the HTML Form element
       else {
           // alert(xmlhttp.responseText); -> this is true no error up here.
           var thediv = document.getElementById("login-init"); 
           thediv.parentNode.removeChild(thediv); //this happens fine.
           var theplace = document.getElementById("login-place");
           var thelement = document.createElement("div"); //this too.
           thelement.innerHTML=xmlhttp.responseText;  //this too'
           theplace.appendChild(thelement); //this too.

       }
     }

And, the innerhtml.responseText is =

        echo '
            <table id="login-2" name="login-2">
            <h3>o username ' . $username . ' não existe. <a href="#" id="register-link" name="register-link">Queres registar?</a></h3>
            <tr>
                <td>username</td>
                <td><input type="text" name="user" id="user" value="' . $username . '"/></td>
            <td>password</td>
                <td><input type="password" name="pass" id="pass" value="' . $password . '" /></td>
            </tr>
            <tr>
                <td colspan="2"><input type="button" value="entrar" id="regit" name="regit"/></td>
            </tr>
        </table>
        ';

I hope this is enough for anyone to understand my doubt. else i'll reword it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆悲凉 2024-12-29 21:01:17

不起作用的函数中的调用为 ajaxfunction,而该函数名为 ajaxFunction - JavaScript 区分大小写。

此外,您需要将 regitonclick 函数声明移动到 handleServerResponse() 函数内部,位于 thelement 行之后.innerHTML=xmlhttp.responseText;,因为在调用此行之前,没有id="regit"的元素,所以函数的绑定不起作用。

另外,您应该使用 ; 终止声明为 element.onclick = function () {} 的函数。以这种方式声明函数是一个赋值语句,因此需要一个分号来终止它。

编辑这是您重新编写的代码,希望它能达到您想要的效果,并删除一些额外的臃肿。我假设 xmlhttpnextlanduserpass 已在您尚未发布的某些代码中声明- 有吗?

document.getElementById("login").onclick = function() {
  //alert("inside."+user.value);
  var s = "goforth=1&user="+encodeURIComponent(user.value)+"&pass="+encodeURIComponent(pass.value);
  ajaxFunction(4, s); 
};

function ajaxFunction(arg, string) {
  //var getdate = new Date();  //Used to prevent caching during ajax call
  // ...but never actually *used* anywhere...
  if (xmlhttp) {
    if (arg == 4) {
      nextland = "login-place";
      document.getElementById("all-burps").innerHTML = string;
      xmlhttp.open("POST", "session.php", true);
    }
    xmlhttp.onreadystatechange = handleServerResponse;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    if (arg == 4) {
      xmlhttp.setRequestHeader('Content-length', string.length);
      xmlhttp.send(string); 
    }
  }
}

function handleServerResponse() {
  var theplace, thelement;
  if (xmlhttp.readyState == 4) {
    if (xmlhttp.status == 200) {
      if (nextland != "login-place") {
        // Update the HTML Form element
        document.getElementById(nextland).innerHTML = xmlhttp.responseText;
      } else {
        // Remove the old div and add the new content
        theplace = document.getElementById("login-place");
        theplace.removeChild(document.getElementById("login-init"));
        thelement = document.createElement("div");
        thelement.innerHTML = xmlhttp.responseText;
        theplace.appendChild(thelement); //this too.
        // Now add the click handler
        document.getElementById("regit").onclick = function() {
          // alert("inside."+user.value);
          var s = "regit=1&user="+encodeURIComponent(user.value)+"&pass="+encodeURIComponent(pass.value);
          ajaxfunction(4, s); 
        };
      }
    } else {
      // Handle HTTP errors
      alert('HTTP ' + xmlhttp.status);
    }
  }
}

The call in the function that does not work says ajaxfunction, whereas the function is called ajaxFunction - Javascript is case sensitive.

Also, you need to move the declaration of the onclick function for regit to inside the handleServerResponse() function, after the line thelement.innerHTML=xmlhttp.responseText;, because before this line is called, there is no element with id="regit", so the binding of the function won't work.

Also, you should terminate the functions declared as element.onclick = function () {} with a ;. Declaring functions in this manner is an assignment statement and as such it requires a semi-colon to terminate it.

EDIT here is your code reworked so hopefully it does what you want, with some extra bloat removed. I am assuming that xmlhttp, nextland, user and pass have been declared in some code you haven't posted - have they?

document.getElementById("login").onclick = function() {
  //alert("inside."+user.value);
  var s = "goforth=1&user="+encodeURIComponent(user.value)+"&pass="+encodeURIComponent(pass.value);
  ajaxFunction(4, s); 
};

function ajaxFunction(arg, string) {
  //var getdate = new Date();  //Used to prevent caching during ajax call
  // ...but never actually *used* anywhere...
  if (xmlhttp) {
    if (arg == 4) {
      nextland = "login-place";
      document.getElementById("all-burps").innerHTML = string;
      xmlhttp.open("POST", "session.php", true);
    }
    xmlhttp.onreadystatechange = handleServerResponse;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    if (arg == 4) {
      xmlhttp.setRequestHeader('Content-length', string.length);
      xmlhttp.send(string); 
    }
  }
}

function handleServerResponse() {
  var theplace, thelement;
  if (xmlhttp.readyState == 4) {
    if (xmlhttp.status == 200) {
      if (nextland != "login-place") {
        // Update the HTML Form element
        document.getElementById(nextland).innerHTML = xmlhttp.responseText;
      } else {
        // Remove the old div and add the new content
        theplace = document.getElementById("login-place");
        theplace.removeChild(document.getElementById("login-init"));
        thelement = document.createElement("div");
        thelement.innerHTML = xmlhttp.responseText;
        theplace.appendChild(thelement); //this too.
        // Now add the click handler
        document.getElementById("regit").onclick = function() {
          // alert("inside."+user.value);
          var s = "regit=1&user="+encodeURIComponent(user.value)+"&pass="+encodeURIComponent(pass.value);
          ajaxfunction(4, s); 
        };
      }
    } else {
      // Handle HTTP errors
      alert('HTTP ' + xmlhttp.status);
    }
  }
}
云淡风轻 2024-12-29 21:01:17

尝试调用

“sregit.onclick = function () { ....”

在handleServerResponse()函数中添加内容后 。当您之前调用它时,不会绑定任何操作,因为 DOM 中没有 #regit 元素。

Try to call

"sregit.onclick = function () { .... "

after appending content in handleServerResponse() function. When you call it earlier, no action is binded, because there is no #regit element in DOM.

绮筵 2024-12-29 21:01:17

当您像这样修改 HTML 时,onClick 事件就会丢失。每次替换 HTML 或使用 onClick 内联(如 onclick="something()")时,您都必须重新添加 onClick 事件。

您还存在语法错误,您在 ajaxfunction('4',""+s); 中使用了小写 F,而该函数名为 ajaxFunction

The onClick events are lost when you modify the HTML like that. You'll have to re-add the onClick events each time you replace the HTML or use the onClick inline like onclick="something()".

You also have a syntax error, you're using a lowercase F in ajaxfunction('4',""+s);, whereas the function is named ajaxFunction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文