如何将 Ajax 代码放在 PHP Codeigniter 中的外部.js文件中


how to put the ajax code in external.js file in php codeigniter

<script type="text/javascript">
    $(document).ready(function() {
        $("#fun").focusout(function() {
            // alert("Value: " + $("#fun").val());
            $.post("<?php echo base_url() ?>index.php/login/email_exists", {
                email: $("#fun").val(),
            }, function(data, status) {
                if (data == 0) {
                    document.getElementById("error").innerHTML = "Email already exists, Please select another email";
                    document.getElementById("ddfun").focus();
                    // alert("email already exists  " + data);
                } else {
                    document.getElementById("error").innerHTML = "";
                }
                //alert("Data: " + data);
            });
        });
    });
</script>

我在应用程序中使用了更多的jQuery和Ajax。我总是在view.php文件中编写Ajax(带有php代码)。如果有任何选项可以在外部js文件中添加Ajax。任何帮助将不胜感激。

您遇到的问题是您将某些 PHP 代码的输出插入到 JS,这在 .js 文件中不起作用。

为了解决这个问题,您可以将该PHP输出作为属性放在元素本身上,并将其读回外部JS文件中。试试这个:

<!-- in the head -->
<script src="/jquery.js"></script>
<script src="/my-js-code.js"></script>
<!-- example HTML element, the only relevant part is the 'data-base-url' attribute -->
<div id="fun" data-base-url="<?php echo base_url() ?>"></div>
// in my-js-code.js
$(document).ready(function() {
    $("#fun").focusout(function() {
        $.post($(this).data('base-url') + "index.php/login/email_exists", {
            email: $("#fun").val(),
        }, function(data, status) {
            if (data == 0) {
                $("#error").html("Email already exists, Please select another email");
                $("#ddfun").focus();
            } else {
                $("#error").html('');
            }
        });
    });
});

在调用外部 js 之前在主布局文件中定义 js 变量

<script type = "text/javascript">
    var base_url = '<?php echo base_url() ?>';
</script>

现在在外部 js 中

$(document).ready(function() {
$("#fun").focusout(function() {
  // alert("Value: " + $("#fun").val());
  $.post(base_url + "index.php/login/email_exists", {
      email: $("#fun").val(),
    },
    function(data, status) {
      if (data == 0) {
        document.getElementById("error").innerHTML = "Email already exists, Please select another email";
        document.getElementById("ddfun").focus();
        //  alert("email already exists  " + data);
      } else {
        document.getElementById("error").innerHTML = "";
      }
      //alert("Data: " + data);
    });
});

是的,你可以。

你的观点.php

<script type = "text/javascript">
    var base_url = '<?php echo base_url() ?>';
</script>
<script src="path/to/external.js"></script>

您的外部.js

$(document).ready(function() {
  $("#fun").focusout(function() {
    // alert("Value: " + $("#fun").val());
    $.post(BASE_URL+"index.php/login/email_exists", {
        email: $("#fun").val(),
      },
      function(data, status) {
        if (data == 0) {
          document.getElementById("error").innerHTML = "Email already exists, Please select another email";
          document.getElementById("ddfun").focus();
          //  alert("email already exists  " + data);
        } else {
          document.getElementById("error").innerHTML = "";
        }
        //alert("Data: " + data);
      });
  });
});