简体   繁体   中英

Write an html text inside a div of the next page after window.location redirect using jquery ajax

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

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.

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.

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 } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM