Reputation: 21
I am trying to pass 2 variables to a form page.
one is a query for the form fields and the 2nd is a error variable.
when load the page, i get the following error:
Severity: Notice
Message: Undefined variable: error
Filename: views/upload_form.php
Line Number: 7
Here is my controller:
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->database();
}
function index()
{
$this->load->helper('url');
$this->load->model('Image_model');
$data['query'] = $this->Image_model->image_biz();
$this->load->view('upload_form', $data, array('error' => ' ' ));
}
function do_upload()
{
$config['upload_path'] = dirname($_SERVER["SCRIPT_FILENAME"]).'/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
} else {
$image_data = array('upload_data' => $this->upload->data());
// can be add extra feature here.
$insert_data = array(
'image_file' => $image_data['upload_data']['file_name'],
'image_path' => $image_data['upload_data']['full_path'],
'image_ext' => $image_data['upload_data']['file_ext'],
'image_width'=> $image_data['upload_data']['image_width'],
'image_height' => $image_data['upload_data']['image_height'],
);
$this->db->insert('image', $insert_data);//load array to database
$this->load->view('upload_success', $image_data);
}
}
}
?>
Here is my view page "upload.php":
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
Image: <input type="file" name="userfile" size="20" /><br>
<label for="select">Business ID:</label>
<select name="image_bizID">
<?php foreach ($query->result() as $row) { ?>
<option value=""><?=$row->biz_id;?></option>
<?php } ?>
</select>
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
Any help to solving this error would be greatly appreciated.
Thank you.
Upvotes: 0
Views: 1074
Reputation: 151
just change these lines in your index function
$data['error']='';
$this->load->view('upload_form', $data);
You may optionally pass third parameter which is boolean data.
Or you may just use isset function for checking error variable whether is set or not
Reference link :http://php.net/manual/en/function.isset.php
Upvotes: 0
Reputation: 37711
Undefined variable: error
means that there is a variable called error
that is not defined.
In your view, you have this line:
<?php echo $error;?>
This expects $image_data['error']
to be set in your controller because $image_data['error']
in the controller becomes $error
in the view.
To fix it the easiest way, just assign it to an empty string in the controller:
$image_data['error'] = '';
or simply check if it's set first in the view:
<?php
if (isset($error))
echo $error;
?>
There are also ways to ignore the notices by setting error_reporting
appropriately, or by just supressing the errors on that line like this in the view:
<?php @echo $error; // notice @ before echo ?>
But it's always better to fix the problem than to ignore it.
UPDATE
Just noticed, you're passing the varibles the wrong way. So this:
function index()
{
$this->load->helper('url');
$this->load->model('Image_model');
$data['query'] = $this->Image_model->image_biz();
$this->load->view('upload_form', $data, array('error' => ' ' ));
}
should be:
function index()
{
$this->load->helper('url');
$this->load->model('Image_model');
$data['query'] = $this->Image_model->image_biz();
$data['error'] = '';
$this->load->view('upload_form', $data);
}
Everything you pass should be in a single array or object, so $data['query']
and $data['error']
is the right way because we're only sending the $data
array. The third parameter in the load view function is used to return the view as a string instead of rendering it, so you don't want to use it in this case.
Read more about views here: http://ellislab.com/codeigniter/user-guide/general/views.html
Upvotes: 2