0

I am very new to javascript - mainly only used it to go back a page or go to a link ..

I got it working on the example page, now I'm trying to get it to work on my website ... However nothing occurs when the link is pressed...

The files exist in their linked locations. The script is copied from the example.

HEADER:

<script type="text/javascript" src="js/jquery-impromptu.min.js"></script>
<link rel="stylesheet" media="all" type="text/css" href="css/jquery-impromptu.css" />

<script type="text/javascript">
function removeUser(id){
                var txt = 'Are you sure you want to remove this user?<input type="hidden" id="userid" name="userid" value="'+ id +'" />';

                $.prompt(txt,{ 
                    buttons:{Delete:true, Cancel:false},
                    close: function(e,v,m,f){

                        if(v){
                            var uid = f.userid;

                            window.location = "deletemember.php?id=" + id;                                  

                        }
                        else{}

                    }
                });
            }
</script>

LINK :

<a href='javascript:;' onclick='removeUser(544666);'>Delete</a>
Ds.109
  • 714
  • 8
  • 16
  • 32
  • And what isn't working? Some error message? – Reeno Dec 12 '13 at 16:13
  • And what does it do, i.e. where's the problem? I would try window.location.href and question, whether a relative url is possible. – Chrisissorry Dec 12 '13 at 16:13
  • It's not doing anything .. no pop up , no redirect. – Ds.109 Dec 12 '13 at 16:13
  • Did you check your browsers error console? – Chrisissorry Dec 12 '13 at 16:14
  • I tried window.location = a.href; and it worked! but the popup doesn't show anymore because the href used to equal javascript:; .. now it's a link – Ds.109 Dec 12 '13 at 16:14
  • As I said, try window.location.href. – Chrisissorry Dec 12 '13 at 16:16
  • @Chris I did - and now it redirects properly - however before, the href used to be javascript:; - once I removed that and replaced it with a link - the javascript for the popup doesn't show. – Ds.109 Dec 12 '13 at 16:17
  • The href of window.location or the href of a used to be JavaScript? This should be a rhetorical question. – NobleUplift Dec 12 '13 at 16:57
  • @NobleUplift I got it fully functioning, I didn't link the js file properly - however now when I try putting it in my site , it doesn't work .. the href in the example was "javascript:;" in html, and that worked. However now the function isn't executing... -- here's a jsfiddle - http://jsfiddle.net/x86rt/ – Ds.109 Dec 12 '13 at 17:02
  • So now the opposite of what was occurring before my comment is happening now? You're sure that your JavaScript console is free of errors? – NobleUplift Dec 12 '13 at 17:39
  • Yes - I tested it and the javascript works - the only problem is that it is not executing ... it worked on a different html page (local) but not through the PHP on the host. – Ds.109 Dec 12 '13 at 18:37

2 Answers2

1

Check this demo: https://github.com/trentrichardson/jQuery-Impromptu/blob/master/demos/user_manager.html

What you should do is make a function. This function is called when a user does something, but it should contain an ID. For example:

<a href="javascript:;" title="Edit User" class="edituser" onclick="editUser(4);">Edit</a>

So, as you can see you will call the function 'editUser(4)' where 4 is the ID.

Back to JS

 function editUser(id){


 }

In this function you add your part, and you end up with this:

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    <script type="text/javascript" src="../jquery-impromptu.js"></script>

    <script type="text/javascript">

        function removeUser(id){
            var user = $('#userid'+id)
            var fname = user.find('.fname').text();
            var lname = user.find('.lname').text();

            var txt = 'Are you sure you want to remove this user with id: '+id+'?';

            $.prompt(txt,{ 
                buttons:{Change:true, Cancel:false},
                submit: function(e,v,m,f){
                    var flag = true;
                    if (v) {

                        window.location = "deletemember.php?id=" + id; 

                    }
                    return flag;
                }
            });
        }

    </script>

    <a href='javascript:;' onclick='removeUser(544666);'>Delete</a>

Now the ID is useable for your window.location.

  • If I wanted to add 2 ids in that function (one for the member, one for the club) so that the code says "window.location = "deleteuser.php?id=" + id + "&club=" + clubid;" - is that possible ? – Ds.109 Dec 12 '13 at 16:23
  • Ok - so i copied over that script to my page - previously I was using it by editing the sample .. now when I click it (the link is the same) but it goes nowhere (at the bottom of chrome it says "javascript:;" .. but nothing happens ... I copied over the javascript and css files and linked to the jquery library .. – Ds.109 Dec 12 '13 at 16:46
  • I made a fix to the script. I checked it on my localhost now, and it is working for me. – douweegbertje Dec 12 '13 at 19:57
  • extra: If you also want to include the clubid, you can call the function with: removeUser(544666, 1234). Same goes for the function: function removeUser(id, club){ – douweegbertje Dec 12 '13 at 19:59
0

Use window.location.href or window.location.replace:

<script type="text/javascript">
function removeUser(id) {
    var txt = 'Are you sure you want to remove this user?<input type="hidden" id="userid" name="userid" value="' + id + '" />';

    $.prompt(txt, {
        buttons: {
            Delete: true,
            Cancel: false
        },
        close: function (e, v, m, f) {

            if (v) {
                var uid = f.userid;

                //window.location.href = "deletemember.php?id=" + uid;
                window.location.replace("deletemember.php?id=" + uid);

            } else {}

        }
    });
}
</script>

You can read about the difference here.

Community
  • 1
  • 1
NobleUplift
  • 5,631
  • 8
  • 45
  • 87