1
<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>

I use more jQuery and Ajax in my application. I always write the Ajax(with php code) in view.php file. If there any option to add the Ajax in the external js file. Any help would be appreciated.

3 Answers 3

2

The issue you have is that you are inserting the output of some PHP code to the JS, which won't work in a .js file.

To get around this you could place that PHP output as an attribute on the element itself and read it back within the external JS file. Try this:

<!-- 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('');
            }
        });
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

<script type="text/javascript"> $(function () { $.ajax({ url: '<?php echo base_url() ?>task/get_sent_sms', type: 'POST', data: { client_id: <?php echo $client->client_id ?>, project_id: <?php echo $client->project_id ?> }, success: function (response) { //$('#sms_message').val(''); // empty the value field //alert(response); $('#sent_sms').html(response); } }); }); </script> what about this
any other solution
Not really. Unless you want to start processing JS files for PHP code which is a bad thing to be doing. Do you have a specific issue with the above solution? It's a widely used technique.
0

Define js variable in main layout file before you call your external js

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

Now in external 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);
    });
});

Comments

0

Yes you can.

Your view.php

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

your external .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);
      });
  });
}); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.