1

i'm using ci built-in validation_errors() function in my login form. everything works fine and i can see true validation errors in my view. but when i echo validation errors into my js variable in the script to show it in the customized notifications, nothing shows. here is my code:

    <?php if (isset($validation_error)){
        echo $validation_error;} ?>
    <script>
        var text=  "<?php echo $validation_error;?>";
    </script>
    <script src="<?php echo base_url();?>resources/js/assets/errore-not.js" type="text/javascript">
    </script>

when i pass any php variables or strings in "text" variable of script, it shows the notification correctly. this is my notify code:

$(document).ready(function notification () {
    $.notify({
        icon: "notifications",
        message: text

    }, {
        type: 'danger',
        timer: 4000,
        placement: {
            from: 'top',
            align: 'center'
        }
    });
});

How can I do this?

10
  • <script> var text= $validation_error; </script> here you allocate php variable without php tag to javascript variable... Commented Jun 21, 2018 at 6:07
  • i've been used this mechanism to show upload errors of ci in exactly this way and it works fine.the return value of this function: $this->upload->display_errors() @Teemu Commented Jun 21, 2018 at 6:11
  • The original source code was var text= $validation_error; which certainly won't work. Later you've edited the code to a working version. Commented Jun 21, 2018 at 6:40
  • 1
    In Firebug or on chrome inspect, are you getting any errors or warnings? Commented Jun 21, 2018 at 6:48
  • To test your $.notify call just hard-code message: 'my temp text' and check if its working. If its not wroking then you have check notify call first Commented Jun 21, 2018 at 6:51

1 Answer 1

2

You can pass the php variable value inside javascript variable like this.

<?php if (isset($validation_error)){
    echo $validation_error; ?>
<script>
    var text= '<?php echo $validation_error;?>';
</script>
<?php } ?>
Sign up to request clarification or add additional context in comments.

1 Comment

tnx but my code was like this before and it didn't work for me.

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.