tags:

views:

581

answers:

2

Tomas has been helping me on this problem and we have made some real progress I think. Here is the modified code. What this is supposed to do is to first, take my query string and pass it to the server where the data is parsed and inseted in the database. When the class method returns a success, the JQ function should echo a 100% css styled div (id=message) with the message from the server. It can also return a failure message as well. This message is imported?? with json. I can see the message in the FF console but I just can't see the message in the browser.

Another issue I am having is that every div on the page is having the id=message appended to it.

EDIT:

$.ajax({
    type: "POST",
    url: "body.php?action=admCust",
    data: dataString,
    success: function(data){
        $('#admCust input[type=text]').val('');
        var div = $('<div>').attr('id', 'message').html(data.message);
        if(data.success == 0) {
            $(div).addClass('error');
        } else {
            $(div).addClass('success');
        }
        $('body').append(div);
        $(div).show();
      }
});
return false;
A: 

Not sure if this is what you're after, but as I understand your problem, do this:

$(function() {
    $('.error').hide();
    $(".admCustBtn").click(function() {
        $('.error').hide();
        var admCustRPSecPhone = $("input#admCustRPSecPhone").val();
        var dataString = '&admCustRptSumm='+ admCustRptSumm + '&admCustRptDtl='+ admCustRptDtl;
        $.ajax({
            type: "POST",
            url: "index.php",
            data: dataString,

            // add an input parameter named data
            success: function(data){
                $('#admCust input[type=text]').val('');

                // remove the alert box
                // alert( "Success! Data Saved");

                // add the success handling code from the other method
                var div = $('#message').attr('id', 'message').html(data.message);
                if(data.success == 0) {
                    $(div).addClass('error');
                } else {
                    $(div).addClass('success');
                }
                $(div).show();
              }
        });
        return false;
    });
});

EDIT: Renamed $div to div and changed the selector to 'div' from '<div>'.

EDIT2: Modified the selector (add an id="message" in the html!), added $()-wrappers around the div variable, and changed the .append() line to a .show() command.

Tomas Lycken
Thank you Tomas. I will try that and let you know what happens if anything. :) Thanks for helping me.
Tomas, I am getting this error:message: Statement on line 129: Type mismatch (usually non-object value supplied where object required)That line has:$div.addClass('success');It is certainly doing more that I have even gotten it to do before. Thanks again.
Tomas, I just checked the console and it says:$div.addClass is not a functionsuccess()()admCust.js (line 129)success()jquery.js (line 3645)onreadystatechange()()jquery.js (line 3602)[Break on this error] $div.addClass('success');
Ah! I just copy-pasted your code, but it seems jQuery doesn't like the variable name starting with a $. I edited the post (just removed the $ from $div on all occations) - what happens if you run the code now?
Tomas Lycken
Hi Tomas, I will try that now and get back to you. Thanks!
Tomas, it still isn't working as expected. I am getting this js error. The error is on the same line as before. The only difference in the way it renders now is that all of my black fonts are changed to white and the tables are shifted right. :) div.addClass is not a function admCust.js Line 129
With regard to your comment about the div showing.. I guess that's what I need, I honestly don't know. What I am expecting to happen is that when my php class outputs a success for saved data, I am expecting a css box to show up with a message that I am importing with JSON. I'm sure my terminology is off here but hopefully you can understand what I need.
Tomas, I'm no JQ expert but I just noticed that there is no json datatype in the function anymore. I'm not sure if this would be the reason that it isn't working but I thought I would mention it. The data is brought back in using json. Again, sorry if my terminology os off but I'm brand new to all this client side stuff. :)
It seems jQuery doesn't work with the div as a jQuery object correctly. Post updated - if $(div) doesn't work, try using the same selector again. Note that I changed the selector, so add an id="message" to the div where you want the message to show up.
Tomas Lycken
Tomas, I'm excited because I think I see something positive. I tried your code changes and I got some for loop error on line 3200 of jquery. I changed the selector back to $('div').attr('id', and I got the fonts again in white and everything was pushed over to the right. I looked in the html part of the console and I can see a div with an id of message there!! :) I'm not 100% if this is the same message id that you are referring to in your code but when I pass my mouse over the selector in the console, the area highlights with purple borders in the exact area where I want the div placed.
I think we are are on the right track. Would you have any idea why I can't see the json message? I already checked in the console and the json message is there under "Response". It says, Success.
Tomas, I think these tables are being pushed over because I don't see a closing div for the message.
Ok, here is the reason why all my tables are being shifted over. In every div in the page, I can see where there is a id="message" there. Have no idea how to fix it, but at least I know what it is. :)
"Renamed $div to div and changed the selector to 'div' from '<div>'." ... That completely changes what the code does. Doing $('<div>' is telling jQuery to CREATE a new DIV element, doing $('div') is SELECTING all DIV elements in the code.
Paolo Bergantino
Paolo, I'm aware that the outcome changes - I'm just not sure the original outcome was really what the OP desired...nutjob, could you post some of your html code? It is really hard to know exactly what you want jQuery to do with your html, when we haven't seen it.
Tomas Lycken
Hi Tomas.. If you are still interested.. I'd be happy to post the html that went with this. It would be a learning experience for me as well. Thanks for helping me. I really appriciate it.
Please do! =) I'm not promising miracles, but I'll take a look at it.
Tomas Lycken
A: 

Try this code (since it has a return false; at the end I'm assuming this is inside a submit function or something and dataString is being populated from elsewhere....):

$.ajax({
    type: "POST",
    url: "body.php?action=admCust",
    data: dataString,
    dataType: 'json',
    success: function(data){
        $('#admCust input[type=text]').val('');
        var div = $('<div>').attr('id', 'message').html(data.message);
        if(data.success == 0) {
            $(div).addClass('error');
        } else {
            $(div).addClass('success');
        }
        $('body').append(div);
      }
});
return false;

Your biggest mistake was that you forgot to specify a dataType (which I had in my example...) so jQuery did not know what format the server would be returning. It simply assumed it was a string instead of a JSON object so your data variable wasn't populated correctly. You also don't need the show() as appending is enough.

I tested the above and it works, assuming, again, dataString is populated elsewhere and body.php is returning a valid JSON string with a message and a success code.

Paolo Bergantino
Hi Paolo. Thanks for the help once again. I figured you threw in the towel. Anyhow, about the datatype... I have been saying this all along. The values from the form are sent over a query string to the server first. This was the whole reason that Tomas, the guy that was trying to help me yesterday wasn't successful. Again, I'm no JQ expert but the datatypes are what were hanging me up from the very start. In your example above, you specify the json datatype with a url of body.php?action=admCust. That php script returns database values to populate the input boxes....
The success function part just alerts the user that the data was sent over the query string. I don't think I can use that same php script for the server callback.
I think you may be confusing the dataType that is SENT against the dataType that is RECEIVED. You are sending a POST request to the server, and the server can then return a JSON response. dataType only specifies the latter part. type specifies the first one. I can't really tell what the heck you're doing without your PHP code, but the code in my answer can properly alert the user that the data was sent if you return a JSON response from the server.
Paolo Bergantino
Ahh... I think I see where I am getting messed up on this.. Thanks Paolo. OK, so the data: dataString is NOT the RECEIVE but the SEND and the dataType: json; is the RECEIVE or the callback. I'm going to give this a shot and see if it works. :) Thanks again!!!
See.. I am actually learning in this whole process. :)