Jump to content

Recommended Posts

I'm trying to work myself through learning this framework and though I have a lot of PHP experience MVC frameworks just frustrate the hell out of me.

 

I've been able to write a controller that queries a database and return the results but I am stumped at how to pass data back to the controller say from a form...so user fills out form with items they want to search the db on, when form is submitted how is the data sent back to the controller or model so I can build a custom query? My understanding of CI (and I'll admit it is limited right now) is that I have to put variables in the URI...is that correct? I could have 10 different search items so I have to build out a string like index.php/controller/item1/item2/item3/item4/item5...? I'd rather not have the search variables exposed, is there a way to pass vars through $_POST?

 

Any help or links to help would be appreciated. 

Link to comment
https://forums.phpfreaks.com/topic/210340-i-need-a-codeigniter-tutorial/
Share on other sites

Sorry, Ive not used CI so I'm not sure I'll be much help but any post data likely still turns up within $_POST. Unless they unset it somewhere along the line.

 

I would look at the documentation in regards to the Request object or similar. Post data should be within there somewhere. There is likely form helpers as well.

There is honestly a few way's todo this. CI Has a great form validation tool that has a function called set_value which allows you to present the previously typed data into it, but thats not the only way... One thing I find myself doing all the time is storing the user input as a array on the controller, and since I like to reuse my views, I simply tell the controller to pass any post data to that array and pass that array to the view. Then, the view will always look to see if the array has data in it for the specific field, if its there... put the contents into the value section... It's actually rather simple once you do it a few times...

 

Here's an example.

 

MyController.php

<?php

class Welcome extends Controller {

function Welcome()
{
	parent::Controller();	
}

function index()
{
	if($_SERVER['REQUEST_METHOD'] == "POST"){

		//get the form data
		$data['search_keyword'] = array(
			"field1" => $this->input->post('field1'),
			"field2" => $this->input->post('field2')
		); 

		//load the database model
		$this->load->model('my_model');

		//pass the data to the database
		$data['results'] = $this->my_model->search($data['search_keywords']);

		//load the view with the results and form data
		$this->load->view('search', $data);

	} else {

		$this->load->view('search');

	}

}

}

 

Then my view

<html>
<head>
	<title>My View</title>
</head>
<body>
<form method="post" action="MyController/form">

<input type="text" name="field1" value="<?php if(!empty($search_keyword['field1'])){ echo $search_keyword['field1'];?>">

<input type="text" name="field2" value="<?php if(!empty($search_keyword['field2'])){ echo $search_keyword['field2'];?>">

</form>
</body>
</html>

 

Now I havent tested this stuff, but its basically what I do most of the time I am trying to accomplish what you are doing.

 

Even though this all works fine, CI has some great libraries for forms and validation and databases and I highly recommend spending the time looking into to leveraging those over just plain old php and html.

 

Let me know if that helps!

 

Thanks,

Peter

 

 

 

This worked nicely but I did run into a problem trying to pass a <select> option back to the controller -

 

"field1" => $this->select->post('field1')

 

didn't seem to be an option...

 

can anyone point me in the right direction here? Thanks

 

$this->input->post('field1');

 

input does not indicate an input element, but rather a class that you are calling the post method from. This means that even though the post value came from a select element, the value of that element was filtered and retrieved through the input class/post method.

  • 1 month later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.