Ok, here is my script....it's not working. When I comment out the
whole ajax request it works, but I just can't figure out what's going
on. Maybe I've just looked over it to many times.....

function addNewRole(type) {
        var name = prompt("New " +type+ " Name");
        var url = "ajax/addRole.php?newDefName=" +name+ "&type=" +type+;
        new Ajax.Response(url, {
                onSuccess: function(response) {
                        var json;
                        json = response.responseJSON;
                        if(!json) {
                                alert("The New Defendant Role Could Not Be 
Added. Please Contact
Your Administrator");
                        }
                        else {
                                if(json.success) {
                                        alert("Success!");
                                }
                        }
                }
        };
}

Thanks in advance!

On Sep 18, 6:06 am, bluezehn <[EMAIL PROTECTED]> wrote:
> There ya go - knew prototype did that somewhere! Cheers for filling in
> the gaps T.J.
>
> On Sep 18, 11:02 am, "T.J. Crowder" <[EMAIL PROTECTED]> wrote:
>
> > > Now in prototype, it does automatically decode this for you. I can't
> > > remember off the top of my head where it goes!
>
> > The Ajax.Response object[1] passed to the handlers has a responseJSON
> > property, so if your server-side page returns this JSON:
>
> >     {
> >         "success": true,
> >         "message": "Defendant added"
> >     }
>
> > ...your onSuccess handler can access that like so:
>
> >     onSuccess: function(response)
> >     {
> >         var json;
>
> >         json = response.responseJSON;
> >         if (!json)
> >         {
> >             // Your server-side code did not return
> >             // JSON data as expected; handle that.
> >         }
> >         else
> >         {
> >             // Good, got JSON data
> >             if (json.success)
> >             {
> >                 // Your server-side code says all went well;
> >                 // you'd probably show json.message, e.g.:
> >                 alert(json.message);
> >             }
> >             else
> >             {
> >                 // Your server-side code ran correctly, but
> >                 // returned an error; handle that.
> >             }
> >         }
> >     }
>
> > [1]http://www.prototypejs.org/api/ajax/response
>
> > HTH,
> > --
> > T.J. Crowder
> > tj / crowder software / com
>
> > On Sep 18, 10:33 am, bluezehn <[EMAIL PROTECTED]> wrote:
>
> > > Whenever a web page is requested and returned to the browser - whether
> > > this is through AJAX or a new page view - the server tells the browser
> > > what type of content it is being served and whether or not the request
> > > was carried out successfully. Prototype can access this information,
> > > and indeed does to work out whether to use onSuccess and onFailure.
>
> > > But it can also see what type of content is returned, and if it sees
> > > JSON, prototype will automatically decode it into a javascript object
> > > for you. You still need php to tell it what the content type is and
> > > you do that before any output is made with the following line:
>
> > > header('Content-type: text/json');
>
> > > You can then return an associative array encoded into json:
>
> > > return json_encode($assocArray);
>
> > > Now in prototype, it does automatically decode this for you. I can't
> > > remember off the top of my head where it goes! But I remember reading
> > > that prototype will do this automatically. The long way round is:
>
> > > onSuccess: function(transport){
> > >      var json = transport.responseText.evalJSON();
> > >    }
>
> > > And in fact, the prototype short cut is rendered somewhat irrelevant
> > > here as if the data you're transporting needs to be secure according
> > > to a logged in user, you need to make the returned JSON non-
> > > executionable, or a default constructor included through XSS could
> > > hijack your data. So the correct way is something like this:
>
> > > in PHP:
> > > return '/*' . json_encode($assocArray) . '*/';
>
> > > then in JS:
>
> > > onSuccess: function(transport){
> > >      var jsObj = transport.responseText.substr(2,
> > > transport.responseText - 2).evalJSON(); // chop the comments off start
> > > and end
> > >    }
>
> > > Then json has your javascript object in.
>
> > > For your second issue.
>
> > > From ajax you get a name and id, you insert those into the dropdown.
> > > Then, you need a way of selecting the <option> you just inserted into
> > > the <select>, there are lots and lots of ways to do this. I'm sure
> > > there is a better way of doing this, but what I'd do is when I add it,
> > > give it an id. You can use Element#Identify() or just use it's id
> > > prefixed by some arbitrary string. Then something like this:
>
> > > $$('select#selectID > option').each(function(option) { option.selected
> > > = false; // make sure nothing else is set to selected });
> > > $('idofOptionYouJustAdded').selected = true;
>
> > > I haven't tested any of the code in this, but the basis of all of it
> > > should be right. Come to think of it I've never seen selected used
> > > like that as a property of a JS object, apart from the googling I
> > > literally just did!
>
> > > On Sep 17, 11:22 pm, "Bobby.D" <[EMAIL PROTECTED]> wrote:
>
> > > > I've fiddled with Prototype/Script.aculo.us and want to start using
> > > > JSON when interacting with my PHP Code. So far I'm good, I've got some
> > > > data being passed to my PHP, it gets process and then encoded to JSON.
> > > > Now, I need to figure out how to use that data in my onSuccess
> > > > function....Can anyone help?
>
> > > > Also, this is a side now, but I've got a drop down box populated from
> > > > a DB and an option to add to the list. I'm using just the AJAX request
> > > > to make the change. Basically, user click "Add New Defendant" a JS
> > > > Prompt appears, the user enters the new title, upon submit the AJAX
> > > > gets called inserts the new defendant name into the database and
> > > > return the Name (which I've already got) and the id (this is important
> > > > because it's the value of each select...) I am looking to change the
> > > > selected index of the drop down box to match what the user just
> > > > entered, but I can't figure out how to do it. I know this isn't a
> > > > Prototype or Script.aculo.us issue, but if anyone has a suggestion it
> > > > would be much appreciated.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to