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'

Recommended Answers

All 9 Replies

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'
});

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.

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.

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); 
 } 

Try like this:

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

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

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

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

What didn't you understand?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.