1

new to CI and trying to figure out how to put it all together. I have a form that i'm submitting via the form helper and using the form validation library to send feedback to the user. I'm submitting the form with a simple $('#editForm').submit();

I'd like to be able to show a success notification to the user and use jquery animations to make it appear in a smooth transition versus just showing up.

All I've been able to do so far is use flashdata to conditionally display the div containing the notification in the view file.

  <div id="success-alert" style="display:none;" class="alert alert-success alert-dismissable">
      <button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
      Success!
</div>

Is there a way to trigger jquery to animate this? I tried just echoing out some php to conditionally display the jquery code, but it wasn't working:

<?php if($this->session->flashdata('result') == 'success') { ?>
  $('#success-alert').slideDown('slow');
  setTimeout(function() {$('#success-alert').slideUp('slow');}, 2100);
<?php }; ?>

I get this error: Uncaught SyntaxError: Unexpected token <

I'd prefer not to use flashdata, other than to conditionally execute jquery, if possible. I know there must be a way, but unfortunately too I'm new to CI to know what it is.

2
  • where did you write your javascript code it should be inside script Commented Aug 19, 2017 at 3:10
  • Where are you placing the javascript code? if it is in a .js file you cannot use PHP there, if it is in a php view file, you should use <script> tag and use $(document).ready(function { ..... }); Can you share more code of your view? Commented Aug 19, 2017 at 3:11

1 Answer 1

2

you dont have to check the value of flash data if the flash data is set then it will trigger

<?php if($this->session->flashdata('result')) { ?>
  <script>
    $('#success-alert').slideDown('slow');
    setTimeout(function() {$('#success-alert').slideUp('slow');}, 2100);
  </script>
<?php }; ?>

Note this should be in same page where your form redirects after success.

Sign up to request clarification or add additional context in comments.

2 Comments

ok, that makes more sense. I had put the code in the document.ready block. However, now I've got the code in the right place and it's not showing the div. I am echoing out the flashdata right before and it shows up, so I know it's not the flashdata not getting set. Not sure what the deal is.
just try alert(1); if it works then your problem is with $('#success-alert').slideDown('slow'); it is not triggering your div

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.