Link to home
Start Free TrialLog in
Avatar of aej1973
aej1973

asked on

Update db using codeigniter/modal window

Hello, can someone help me to be able to update a row in the db using codeigniter/jquery. I have been able to add and view my data but not sure how to proceed with the update/edit part. I have the following on my td tag and am able to pass this value to the controller but not sure how to proceed after this to pull the up the record as a popup window and edit it;

<td><a href="companyEdit/<?php echo $post->vendor_id; ?>" ><span class="glyphicon glyphicon-pencil"></span></a></td>

Thanks for the help.

A
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Without seeing any code is a bit difficult guess what you have to do, but the general pattern is the following:
I argue that companyEdit is a method of your controller and <?php echo $post->vendor_id; ?> is the parameter, in this method you have to get the parameter and pasing it toa model method which has to use it to update the database returning ideally true or false. The returning value will be used to present a view with success or error.

public function($vendor_id)
{
    $data['result'] = $this->modelname->updateVendor($vendor_id);
    $this->load->view('header');
    $this->load->view('db_result', $data);
    $this->load->view('footer');
}

Open in new window


In your model you have to put a method to update the database using ActiveRecord or plain mysql ActiveRecord.

Hope this is clear enough. If you need help with the model method, I'll help you later, because now I have to run away :-)
Avatar of aej1973
aej1973

ASKER

Hello Marco, thank you for getting back to me. Can you please let me know how I can set up the modal window.
Thank you very much.

A
Ok. Can you explain me the application flow? When the modal window has to appear? What is the modal window content? What happens when the user click Ok or Cancel button? And, last but not least, have you some code to start with? :-)
Cheers
Avatar of aej1973

ASKER

Hello Marco, my workflow is as follows:

1. I have a db that I update via  form with the following values;
vendor name/address/contact/status.

2. I am able to display the data from this db on my web page.

3.Ihave a link on every row that had a edit button enabled and it has the row Id passed to this link (<td><a href="companyEdit/<?php echo $post->vendor_id; ?>" ><span class="glyphicon glyphicon-pencil"></span></a></td>).

4.When I click on this link I need to have a popup/modal window that displays the records for that row which I need to be able to edit and update the db. I am not sure how to do this.

Thank you for your help and please let me know if there is any other information you need.

A
Avatar of aej1973

ASKER

Sorry, I did not let you know. I am using active records.  Attached is my controller file and my modal.

Thanks,
A
insertdata.php
modelviewdata.php
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aej1973

ASKER

Marco, thank you very much for your time. I will go through this and will get back to. Really appreciate your help.

A
Avatar of aej1973

ASKER

Hello Marco, I have made a quite a bit of progress but need some help with the last part.

1. I was able to capture the db values with reference to the id
2. In the pop up when I click the update button the alert message captures the correct controller path but my db is not getting updated. Can you let me know why? I have attached the model and the controller for your reference.

Also, can you let me know how I can build in some validation into my pop up form. Thanks again for all your help.


A
url-image.png
update-db-form.php
modelviewdata.php
form-edit.php
Hi A.
The javascript is missing. You have to follow my example and use javascript to post data to the controller method which actually post the data. Perhaps your code doesn't update the db because something went wrong with the javascript.
About the validation, you have to do iot too within the javascript function which post the data, before posting them.
And then put some validation code in the server side also, within the controller method which receive the data from javascript.
Avatar of aej1973

ASKER

Hi Marco, I do have the jquery in place, attached is the script.

//########update script for the controller##############
    //@ updatedb class is on the edit link on the from
    //@ the updatedb class also has the vendorid that we are caputuring using the split command.
	$( document ).on( 'click', '.updateDB', function ( e ) {
		e.preventDefault();
		        var baseurl = 'http://localhost:8888/forms/index.php/update_db_form/';
                var classes = $(this).attr('class');
                var classes_array = classes.split(' ');
                var vendorId = classes_array[classes_array.length -1]; //here is the vendor_id
		$.fancybox( {
			type: "ajax",
			scrolling: "no",			
			href: baseurl + "editInfo/" + vendorId, 
			fitToView: false,
			autoSize: true,
			closeClick: false
		} );
	} );
	//###### End Script....
    
    //#########update script for the pop up ##################
    $( document ).on( 'click', '#post_vendor_id', function ( e ) {
		e.preventDefault();
		var baseurl = 'http://localhost:8888/forms/index.php/update_db_form/';
		var vendorId = $( '#vendor_id' ).val();
		if ( vendorId === '' )
		{
			jQuery.fancybox.close();
			return;
		}
		else
		{
			jQuery.fancybox.close();
			var url = baseurl + 'updateCompanyInfo/' + vendorId;
			$.ajax( {
				type: 'post',
				cache: false,
				url: url,
				success: function (  )
				{
					alert(url);
					//here you could display a short message or just do nothing ;-)
				},
				error: function ( jqXHR, textStatus, errorThrown )
				{
					console.log( jqXHR + " textStatus: " + textStatus + ", errorThrown: " + errorThrown );
				}
			} );
		}
	} );
                                      
    //########End Script........

Open in new window

The problem is that in my script I just grabbed 'vendorId' value as example. But you have a lot of field to update so we can do two things: grab all them in our javascript, build an array and pass the array to the controller method; or try to keep things simple using normal submit tecnique. I'd prefer this second option, so you can build your form traditionally givint it method='post' and action='<?php echo base_url('updateCompanyInfo') ?>'.

Delete the parameter 'vendor_id' from the updateCompanyInfo() method and get id and other values from the $_POST array and go on with your model. This should work and avoids to create a too complex javascript function.
Avatar of aej1973

ASKER

Marco, thank you so very much, you have been such a great help.

Thanks,
A
Avatar of aej1973

ASKER

Thank you.
Glad to help you and good luck with your project.