0

Is there a way to call a (jquery action/write an html text) to a div of a new page after calling the window.location command?

Im trying to make an ajax form submit where the user will be redirected to a new page upon submit,

and in that new page a hidden div will appear with text inside of it

currently this is my code

script.js

$.ajax({
            url:url,
            type:'POST',
            data:datastr,
            success:function(result){
            if(result=="duplicate"){
                $("#status").attr('class', 'span12 alert alert-error');
                $("#status").show();
                $("#status").html("<h4><center>Duplicate Username</center></h4>");

                $("#dept_name").closest(".control-group").attr('class', 'control-group');
                $("#username").closest(".control-group").attr('class', 'control-group error');
                $("#password").closest(".control-group").attr('class', 'control-group');

                $("#username").focus();
                }
            else{
                $("#dept_name").closest(".control-group").attr('class', 'control-group');
                $("#username").closest(".control-group").attr('class', 'control-group');
                $("#password").closest(".control-group").attr('class', 'control-group');
                window.location = base + 'admin/departments/edit_dept/' + result;
                }
            }

        });

i want to make this block of code below work on the page where window.location is going

                $("#status").attr('class', 'span12 alert alert-error');
                $("#status").show();
                $("#status").html("<h4><center>Successfully added Department</center></h4>");

is it possible?

thanks

itsover9000
  • 561
  • 2
  • 12
  • 32
  • Can you not have static text on the second page? i.e. a page with an error message and another page with success message. You seem to be redirecting to a page depending on the ajax result anyway. – reggaemahn Aug 14 '13 at 13:16
  • have a look at http://stackoverflow.com/questions/10525584/what-happens-to-code-after-a-javascript-redirect-setting-window-location-href – Nicolae Olariu Aug 14 '13 at 13:21
  • i want to make my files as few as possible so i do it with ajax – itsover9000 Aug 14 '13 at 13:26

3 Answers3

1

You can use CI session flash data.

http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

Before triggering window.location, set the message in flashdata. On the landing/redirected-to page, check to see if flashdata has a value (success/failure). If so, display (or trigger a js method to show) the message.

stormdrain
  • 7,915
  • 4
  • 37
  • 76
1

You could add a parameter with your window.location, like:

window.location = base + 'admin/departments/edit_dept/' + result + '?showMessage=12'

Then on the next page, have a jquery script that looks for that parameter and shows the message. See this question.

Or you can do it in on the server. But with jquery it works with static html too.

Community
  • 1
  • 1
Gunslinger
  • 729
  • 1
  • 9
  • 19
0

This is a patchup, May require some tuning though.

window.location = base + 'admin/departments/edit_dept/' + result+'/err';  //changed to catchup with the view part

In the Controller:

 <?php
    if($this->uri->segment(4) == "err"){
        $data['err']    = true;                 #will reflect that we need to show the js in the view
        $this->load->view('view', $data);
    }
?>

In the view part:

<?php if(isset($err)){ ?>
<script type="text/javascript">
$("#status").attr('class', 'span12 alert alert-error');
$("#status").show();
$("#status").html("<h4><center>Successfully added Department</center></h4>");
</script>
<?php } ?>
Nil'z
  • 7,487
  • 1
  • 18
  • 28