Reputation: 997
I got one controller called Profile
with several function such as get_user_timeline
and do_upload
. The get_user_timeline
loaded into profile_view
with ajax call from JQuery. It's functionality is to pull twitter timeline every 60 second.
It was running well after i put a form into the view to call the do_upload function. do_upload
functionality is to upload file such as picture or mp3 to server. I put form validation into that function so when user didn't fill the entire from he/she will redirect back to profile_view
with some warning. I did it by calling $this->index()
in the do_upload
function.
When i tested my code, I submitted the form with several input left blank. The profile_view
loaded with nice form validation error warning, but it seems my Jquery won't work anymore because it doesn't display the twitter timeline. I checked the url.. it's say http://localhost/myproject/profile/do_upload
instead http://localhost/myproject/profile
. Is it CI default behavior? how to deal with it? it seems my Jquery only works with the http://localhost/myproject/profile
could you give any advices guys? thanks
here's my code. The profile controller
<?php
class Profile extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('tweet');
$this->load->library('form_validation');
$this->load->model('user_model');
}
function index()
{
if($this->session->userdata('is_logged_in') == TRUE)
{
$data['biography'] = $this->user_model->get_user_by_id($this->session->userdata('user_id'));
$data['user'] = $this->tweet->call('get', 'users/show', array('id' => $this->session->userdata('user_id')));
$data['main_content'] = 'private_profile_view';
$this->load->view('includes/template', $data);
}
else
{
redirect('welcome');
}
}
/** timeline features **/
function get_home_timeline()
{
$timeline = $this->tweet->call('get', 'statuses/home_timeline');
echo json_encode($timeline);
}
//the do_upload function
function do_upload()
{
$config['upload_path'] = './media/';
$config['allowed_types'] = 'mp3';
$config['max_size'] = '30720';
$this->load->library('upload', $config);
//set validation rules
$this->form_validation->set_rules('title', 'Song title', 'trim|required|xss_clean');
$this->form_validation->set_rules('album', 'Album', 'trim|required|xss_clean');
$this->form_validation->set_rules('artist', 'Artist', 'required|xss_clean');
$this->form_validation->set_rules('song_license_agreement', 'License', 'required|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$this->index(); //this return the warning but make my Jquery fail
}
}
THE .JS
$(document).ready(function(){
update_timeline('profile/get_home_timeline', '#home_timeline ul', 'home'); //this is call to the timeline, this won't work if i submit the upload form
});
function update_timeline()
{
//my ajax call goes here
}
THE VIEW
<div id="timeline" class="">
<ul>
<li><a href="#home_timeline" id="home_timeline_menu">Home</a></li>
<li><a href="#user_timeline" id="user_timeline_menu">MyStatus</a></li>
<li><a href="#mentions_timeline" id="mentions_timeline_menu">@me</a></li>
</ul>
<section id="home_timeline">
<ul>
<!-- this is where the timeline goes -->
</ul>
</section>
<div>
<!-- and this is the form -->
<article id="music_upload_form">
<?php echo validation_errors(); ?>
<?php echo form_open_multipart('profile/do_upload');?>
<input type="hidden" id="user_id" name="user_id" value="<?php echo $user->id_str;?>" />
<label for="title">Song Title</label>
<input type="text" name="title" id="title" size="30" value="<?php echo set_value('title'); ?>"/><br />
<label for="album"> Album Title </label>
<input type="text" name="album" id="album" size="30" value="<?php echo set_value('album'); ?>"/><br />
<label for="artist">Artist</label>
<input type="text" name="artist" id="artist" size="20" value="<?php echo set_value('artist'); ?>"/><br />
<input type="file" name="userfile" size="20" /><br />
<input type="radio" name="owner" id="license" value="<?php echo set_value('owner'); ?>"/>I AM the artist/owner of this song and I have the right for distribution<br />
<input type="radio" name="licensed_user" id="license" value="<?php echo set_value('licensed_user');?>"/>I AM NOT the artist/owner of this song BUT I have the right for distribution</br>
<input type="checkbox" name="song_license_agreement" value="<?php echo set_value('song_license_agreement');?>"/>By selecting this option, I swear that all information entered is right<br />
<input type="submit" value="Upload Your Song" />
</form>
</article>
Upvotes: 1
Views: 281
Reputation: 102745
OK, I think I get what's going on here.
Instead of calling $this->index()
, which just runs the index()
method in the current one, redirect the user instead.
function do_upload()
{
if ($error)
{
redirect('profile');
}
}
Calling $this->index()
doesn't actually change the URL or current method, it just runs whatever code you have there.
To get the errors to display on the next page, you can assign them to a session item:
$this->session->set_flashdata('errors', validation_errors());
Then in your view:
echo $this->session->flashdata('errors');
I used flashdata because it will automatically be cleared on the next request. You might want to look into writing or adopting some kind of feedback/message library.
Upvotes: 1