Question

I have created a form using ajax and php. From the main page the user can either edit the clients details, or add a new client. When the user clicks on the edit client button, the form is loaded using ajax, and the client details are inserted by using the id, and for adding a new client, no id is sent. What I want to know is, how do I alter the 'submit' button, for the add client, and an 'update' button for editing the client. The form markup is the same for both stages, but here is the jquery used to differentiate between the two:

Add:

$.ajax({
    dataType: "html",
    url: "ajax.php?action=client_form",
    beforeSend: function() {
        $('.error, .success, .notice').remove();
    },
    success: function(html) {
        $('#users-contain_t').html(html);
    }
});

and for the edit part I have created a function, like so:

function editClient(client_id){

    $('#users-contain_t').load('ajax.php?action=client_form&client_id='+client_id);

};

The markup for the edit button is:

<button class="editClient" type="button" onclick="editClient('.$client['client_id']. ')">Edit Client</button>

This brings up the details in the form. What I want to know, is how to load two different buttons on the same form, when editing, and when adding a new record, using php?

The markup for the two buttons would be something like:

<button id="updateList" type="button" >Update</button>
<button id="addClient" type="button" >Submit</button>
Was it helpful?

Solution

you can set default style of display in form is none like style="display:none" for update list

but when you are calling editClient function you can add show() hide() effect to both button

like

$('#updateList').show();
$('#addClient').hide();

default for form like this

<button id="updateList" type="button" style="display:none;" >Update</button>
<button id="addClient" type="button" >Submit</button>

OTHER TIPS

place a common button for add and update and it can diffentiate on php following code: if you have an unique column on client details like email

if(isset($_POST['save']))
{
//get all client email in your table
//check if the email already exist
//if (yes) 
//update query exected
//else
//insert
}

"save" is your button name i hope this is you really want!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top