1.11M Members

Ajax structure

 
0
 

I have this part of code, ajax will call a function saveContact with parameters name, phone, email, address, is this code ok?

$.ajax({ 
 url: 'addressbook.php', 
 data: 'action=saveContact()','&name='+name+'&phone='+phone+'&email='+email+'&adresa='+adresa; 
 dataType: 'json', 
 type: 'post'
 
0
 

I don't think so. It should be like this:

$.ajax({ 
     url: 'addressbook.php?action=saveContact', 
     data: {
        name: name,
        phone: phone,
        email: email,
        adresa: adresa
    },
    dataType: 'json', 
    type: 'post'
});
 
0
 

I did like this

url: 'addressbook.php?action=add'   

this is in the .js file
in the .php file i have

  $action=$_POST['action']

and there are two possibilities add or delete, i do the add but it's not uploading in the db. No errors show.

 
0
 

Check the response using the success event. Like this:

sucess: function(data, textStatus) {

}

You should use your browser JS debugger and Networking monitoring to identify the errors.

You should also certify that your PHP configuration is set to show all the errors.

 
0
 

The error i get is indefined index 'action' on addressbook.php, how can it be undefined?
this is my success function :

success: function (j) {   
  //show the notice   
$('#notice').empty().html(); 
//empty the input fields 
$('#names').val(''); 
$('#phone').val(''); 
$('#email').val(''); 
$('#adresa').val(''); 
//refresh the address list 
displayAddressList(j.kontakte); 
 } 
 
0
 

Try like this:

$.ajax({ 
     url: 'addressbook.php', 
     data: {
        action: 'add',
        name: name,
        phone: phone,
        email: email,
        adresa: adresa
    },
    dataType: 'json', 
    type: 'post'
});
 
0
 

It worked!!! Finally it was stored in the db. I can't see the contacts list , this is the code in the .js file

function displayAddressList(items){ 
//empty the contacts lists 
var list=$('#kontakte-lists'); 
//save a client copy of the items array for validation whenever its refreshed from server 
addresslist=items; 
//loop thru all the items and add to the list 
var lh=""; 
for(var i=0;i<items.length; i++){ 
lh+="<li>"+items[i].names; 
lh+=" [ "+items[i].phone+" ] ",
lh+=" [ "+items[i].email+" ] ",
lh+=" [ "+items[i].adresa+" ] ";
lh+='<a href="#delete-id" class="deletebtn" contactid="'+items[i].id+'"> delete contact </a>' 
lh+="</li>"; 
} 
list.html(lh); 
//set the delete button event after every reload 
setDeleteButtonEvents() 
} 

And this is the code in the html

<ul id="#kontakte-lists"> 
        <li><a href="#delete-id" class="deletebtn" contactid='1'> delete contact </a></li> 
        <li> <a href="#delete-id" class="deletebtn" contactid='2'> delete contact</a></li> 
        <li><a href="#delete-id" class="deletebtn" contactid='3'> delete contact</a></li> 
</ul>

Actually class and contactid appear underlined. Otherwise could you show me another way to display contacts i have in the db?Thanks in advance

 
0
 

I'd use something like this:

var lis = [];

for(var i=0, il=items.length,item; i<il, item=items[i]; i=i+1){ 
    lis.push(String.format(
        '<li> ' +
            '{0} [ {1} ] [ {2} ] [ {3} ] ' +
            '<a href="void(0);" class="deletebtn" contactid="{4}"> delete contact </a>' +
        '</li>'
        , item.names, item.phone, item.email, item.adresa, item.id
    ));
} 

list.html(lis.join(""));

The String.format method is this one:

// --------------------------------------
// String.format
// --------------------------------------

String.prototype.format = function () {
    return String.format(this, arguments.length == 1 ? arguments[0] : arguments);
};

String.format = function (source, params) {
    var _toString = function (obj, format) {
        var ctor = function (o) {
            if (typeof o == 'number')
                return Number;
            else if (typeof o == 'boolean')
                return Boolean;
            else if (typeof o == 'string')
                return String;
            else
                return o.constructor;
        } (obj);
        var proto = ctor.prototype;
        var formatter = typeof obj != 'string' ? proto ? proto.format || proto.toString : obj.format || obj.toString : obj.toString;
        if (formatter)
            if (typeof format == 'undefined' || format == "")
                return formatter.call(obj);
            else
                return formatter.call(obj, format);
        else
            return "";
    };
    if (arguments.length == 1)
        return function () {
            return String.format.apply(null, [source].concat(Array.prototype.slice.call(arguments, 0)));
        };
    if (arguments.length == 2 && typeof params != 'object' && typeof params != 'array')
        params = [params];
    if (arguments.length > 2)
        params = Array.prototype.slice.call(arguments, 1);
    source = source.replace(/\{\{|\}\}|\{([^}: ]+?)(?::([^}]*?))?\}/g, function (match, num, format) {
        if (match == "{{") return "{";
        if (match == "}}") return "}";
        if (typeof params[num] != 'undefined' && params[num] !== null) {
            return _toString(params[num], format);
        } else {
            return "";
        }
    });
    return source;
};
 
0
 

mmmm i didn't get this very much, anyway thanks for everything

 
0
 

What didn't you understand?

Question Answered as of 1 Year Ago by AleMonteiro
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: