How to insert zero rows in codeigniter - php

I'm click my submit button freely without enter values in the form..then in my database a row inserted that columns contain zeros
controller
function submit_expense()
{
$exp_date=$this->input->post('expense_date');
$expense_ids= $this->input->post('expense');
$expense_ids=substr($expense_ids,0,strlen($expense_ids)-1);
$expense_id_array = explode(',',$expense_ids);
$id=$this->session->userdata('userid');
for($i=0;$i<count($expense_id_array);$i++)
{
$required_id = TRIM($expense_id_array[$i]);
$this->form_validation->set_error_delimiters('<div style="color:#B94A48">', '</div>');
$this->form_validation->set_rules('exp_amount_'.$required_id, 'Expense Amount', 'required|numeric');
$this->form_validation->set_rules('comment_'.$required_id, 'Comments', 'required|alpha');
if ( $this -> form_validation -> run() === FALSE )
{
$this->index();
}
else
{
$amount= $this->input->post('exp_amount_'.$required_id);
$comment=$this->input->post('comment_'.$required_id);
$expense_data=array(
'expense_id'=>$required_id,
'expense_amount'=>$amount,
'user_id'=>$id,
'date'=>$exp_date,
'comments'=>$comment,
);
$this->home_model->insert_expense($expense_data);
$this->session->set_flashdata('message', 'Successfully submitted.');
redirect(base_url().'home/index');
}
}
}
And also my validation is not working
footer_view
var new_String ='<div class="form-group" id="ex_'+unqid1+'">'
+'<label for="exampleInputPassword1">'+name+'</label> </br>'
+'<input name="exp_amount_'+unqid1+'" id="id_'+unqid1+'" type="text" '+
'class="form-control" placeholder="Enter '+name+' expense amount" style="margin-right:20px;" required>'
+'<input name="comment_'+unqid1+'" type="text" id="comment" class="form-con" placeholder="Comments" style="margin-right:20px;" required ></div >' ;
$("#create_exp").append(new_String);
view
this is my view use the id create_exp in my footer
<form role="form" action="<?echo base_url()?>home/submit_expense" method="post">
<?$todays_date=date('Y-m-d');?>
<input id="expense_date" name="expense_date" value="<?echo $todays_date;?>" type="hidden" />
<div class="red">
<? echo $this->session->flashdata('message'); ?>
</div>
<div class="box-body" id="create_exp">
<?php echo validation_errors(); ?>
<span>Choose from the right which all you need to enter for today</span>
<!--here goes the input boxes-->
<!-- /.input boxes -->
</div><!-- /.box-body -->
<span id="errmsg1"></span>
<input id="expenseVal" name="expense" type="hidden" class="form-control">
<div class="box-footer" id="button_id">
<button type="submit" class="btn btn-primary" >Submit</button>
</div>
</form>

As the OP Wants to show the all the Errors inside a div
<div id="error">
<?php echo validation_errors(); ?>
</div>
As the OP Wants some additional info,
To include a view or common page
$this->load->view('yourownpage');
Then you should have yourownpage.php inside your views folder
To have pagination, Have this in your controller's function
$this->load->library('pagination');
$config['base_url'] = 'yourownpage';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
Note :
Make sure to place the error div in the place that didn't hurt your textbox

Related

Insert URL in database Codeigniter 3

I got a website delivered to me, and the dev doesn't reply to me anymore. As a beginner I am trying to find a solution by my own but I can't figure it out.
I would like to add a feature and maybe you guys can help me.
To explain our users can write some comments on a single page, before to be published our moderator is validating the comment, as soon as it is done the user is receiving an email saying your comment Blablabla has been validated.
We would like to add to the email a link to the page where he wrote his comment, as we have a lot of single pages with different URL, it will be easier for him to found his comment back.
So our goal is to save the URL into the database and then I will extract that URL to put it in the email.
I have added in the our user_comment DB the column url_link to store the URL.
Here is my controller, I have added urllink
public function add_comment(){
if ($this->session->userdata('user_id')){
$comment = $this->security->xss_clean($this->input->post('comment'));
$pageid = $this->security->xss_clean($this->input->post('page_id'));
$urllink = $this->security->xss_clean($this->input->post('url_link'));
$data = array(
'user_id' => $_SESSION['user_id'],
'page_id' => $pageid,
'url_link' =>$urllink,
'comment' => $comment,
'created' => date("Y-m-d H:i:s"),
'modified' => date("Y-m-d H:i:s")
);
echo $query = $this->page_model->add_comments($data);die;
}else{
redirect();
}
}
My model
public function add_comments($data){
if($this->db->insert('user_comment',$data)){
return 'true';
}else{
return 'false';
}
}
My view for the comment section
<h3>Post a Comment</h3>
<?php if ($this->session->userdata('user_id')){?>
<div class="textarea_editor" style="clear:left; ">
<div class="textarea_editorinner">
<div id="editor" data-id="<?php if(!empty($_SESSION['user_id'])){ echo $_SESSION['user_id'];}else{ echo '';}?>"></div>
<textarea name="comment" style="display:none" id="hiddenArea" class="add_comment"></textarea>
</div>
<?php }else{?>
<div class="write-comment">
<input style="margin-left:10px; " type="text" data-toggle="modal" data-target="#user_login" placeholder="Type your comment" class="add_comment">
<?php }?>
<?php
if ($this->session->userdata('user_id')){?>
<button class="sendmessage sendmessageclick" data-d-id="<?php echo $pages[0]->pageId;?>"><span class="far fa-paper-plane">
</span><span class="d-none d-block-sm">Send</span></button>
<?php }?>
</div>
</div>
My common.js
$('.sendmessageclick').click(function(){
$('#hiddenArea').val(quill.container.firstChild.innerHTML);
$.ajax({
type:'post',
async: false,
data:{page_id:$(this).attr('data-d-id'),comment:$('.add_comment').val()},
url: static_url+'/pages/add_comment',
success: function(result){
$('.add_comment').val('');
if(result == 'true'){
$('.comment_alert').show();
setTimeout(function() {
$('.comment_alert').hide();
location.reload();
}, 3000);
}else{
alert('Error');
location.reload();
}
console.clear();
}
});
});
Everything is working perfectly, except URL, for sure I am missing something to indicate the URL to be stored in the DB.
Thanks for your support.
As I can see your controller collect three POST variables they are 'comment', 'page_id', and 'url_link'. However, if you look at your view code only 'comment' is sent.
A simple solution would be to add two additional input boxes having the name 'page_id', and 'url_link'.
<h3>Post a Comment</h3>
<?php if ($this->session->userdata('user_id')){?>
<div class="textarea_editor" style="clear:left; ">
<div class="textarea_editorinner">
<div id="editor" data-id="<?php if(!empty($_SESSION['user_id'])){ echo $_SESSION['user_id'];}else{ echo '';}?>"></div>
<textarea name="comment" style="display:none" id="hiddenArea" class="add_comment"></textarea>
<input type='hidden' name='page_id' value='<<Page ID value added by PHP>>'>
<input type='text' name='url_link' value=''>
</div>
<div class="write-comment">
<input style="margin-left:10px; " type="text" data-toggle="modal" data-target="#user_login" placeholder="Type your comment" class="add_comment">
<?php }?>
<?php
if ($this->session->userdata('user_id')){?>
<button class="sendmessage sendmessageclick" data-d-id="<?php echo $pages[0]->pageId;?>"><span class="far fa-paper-plane">
</span><span class="d-none d-block-sm">Send</span></button>
<?php }?>
</div>
</div>

Breaking login flow if already logged in with another session

I am working on a codeigniter 3 app and ive recently implemented a session checker that deletes a user session if they're already logged in. Now we want a modal box to pop up if the user is already logged in with another session. I am able to get a modal box to pop up using a button but i want to implement it into the original flow of the login system. As it is the login form takes you straight to the validate login system. This is the login form now:
<form action="<?php echo site_url('login/validate_login/user'); ?>" method="post">
<div class="content-box">
<div class="basic-group">
<div class="form-group">
<label for="login-email"><span class="input-field-icon"><i class="fas fa-envelope"></i></span> <?php echo get_phrase('email'); ?>:</label>
<input type="email" class="form-control" name = "email" id="login-email" placeholder="<?php echo get_phrase('email'); ?>" value="" required>
</div>
<div class="form-group">
<label for="login-password"><span class="input-field-icon"><i class="fas fa-lock"></i></span> <?php echo get_phrase('password'); ?>:</label>
<input type="password" class="form-control" name = "password" placeholder="<?php echo get_phrase('password'); ?>" value="" required>
</div>
</div>
</div>
<div class="content-update-box">
<button type="submit" class="btn"><?php echo get_phrase('login'); ?></button>
</div>
<!-- Modal -->
<div class="modal fade" id="login" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">You are already logged in</h4>
</div>
<div class="modal-body">
<p>You are currently logged in on a different session on the site. Please note that if you continue, the existing session will be terminated. Please change your password if you suspect that your account has been conpromised.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel Login</button>
<button type="submit" class="btn"><?php echo get_phrase('login'); ?></button>
</div>
</div>
</div>
</div>
<div class="forgot-pass text-center">
<span><?php echo get_phrase('or'); ?></span>
<?php echo get_phrase('forgot_password'); ?>
</div>
<div class="account-have text-center">
<?php echo get_phrase('do_not_have_an_account'); ?>? <?php echo get_phrase('sign_up'); ?>
</div>
</form>
The button at the moment goes straight to this login function:
public function validate_login($from = "") {
$email = $this->input->post('email');
$password = $this->input->post('password');
$credential = array('email' => $email, 'password' => sha1($password), 'status' => 1);
// Checking login credential for admin
$query = $this->db->get_where('users', $credential);
if ($query->num_rows() > 0) {
$row = $query->row();
$this->session->set_userdata('user_id', $row->id);
$this->session->set_userdata('role_id', $row->role_id);
$this->session->set_userdata('role', get_user_role('user_role', $row->id));
$this->session->set_userdata('name', $row->first_name.' '.$row->last_name);
$this->delete_session_user_id();
$this->session->set_flashdata('flash_message', get_phrase('welcome').' '.$row->first_name.' '.$row->last_name);
if ($row->role_id == 1) {
$this->session->set_userdata('admin_login', '1');
redirect(site_url('admin/dashboard'), 'refresh');
}else if($row->role_id == 2){
$this->session->set_userdata('user_login', '1');
$this->set_session_user_id();
redirect(site_url('home/my_courses'), 'refresh');
}
}else {
$this->session->set_flashdata('error_message',get_phrase('invalid_login_credentials'));
redirect(site_url('home/login'), 'refresh');
}
}
I created this function to pull the user id from the emails:
public function get_user_id($user_email = "") {
$this->db->select('id');
$this->db->where('email', $user_email);
$user_id=$this->db->get('users');
return $user_id;
}
This function can get the user id based on the email supplied.
Then I use this function to check if there is a session and return false if there are 0 results and true if there is a session with that user id. So if its false they should be able to log in and the modal pop-up shouldnt open but if its true it should open.
public function user_has_session($user_id=''){
$this->db->where('user_id',$user_id);
$this->db->from('ci_sessions');
$total=$this->db->count_all_results();
if($total<0)
return false;
else
return true;
}
I think this is the best approach without having to redo the entire login flow. Perhaps someone can advise if this is the best approach or if in fact i should change the entire flow.
Thanks
Here is the previous problem I had which I have answered myself
https://stackoverflow.com/questions/62458226/codeigniter-3-stop-multiple-logins-using-ci-sessions-database
UPDATE Added to clarify my problem
Well the problem is when a login is attempted from another device it should logout the other active session. So if o logged in on my desktop in a new browser or even my phone with the same user ID the active session should end, at the moment it does so without warning the user. So I want to have a modal pop up warning the user that there is an active session currently running with this user id
You need to implement an Ajax call which will check whether the user is already logged in or not. If the user is not logged In than you can proceed to login otherwise trigger your popup open to display the message.
Here the user has choices to log in or not, If users choose to login then you can unbind the event on submit and let the user go ahead.
I have made some changes to your HTML file. Please check below -
Your HTML template
<form action="<?php echo site_url('login/validate_login/user'); ?>" id="login-form" onSubmit="return checkUserSession();" method="post">
<div class="content-box">
<div class="basic-group">
<div class="form-group">
<label for="login-email"><span class="input-field-icon"><i class="fas fa-envelope"></i></span> <?php echo get_phrase('email'); ?>:</label>
<input type="email" class="form-control" name = "email" id="login-email" placeholder="<?php echo get_phrase('email'); ?>" value="" required>
</div>
<div class="form-group">
<label for="login-password"><span class="input-field-icon"><i class="fas fa-lock"></i></span> <?php echo get_phrase('password'); ?>:</label>
<input type="password" class="form-control" name = "password" placeholder="<?php echo get_phrase('password'); ?>" value="" required>
</div>
</div>
</div>
<div class="content-update-box">
<button type="submit" class="btn"><?php echo get_phrase('login'); ?></button>
</div>
<!-- Modal -->
<div class="modal fade" id="login" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">You are already logged in</h4>
</div>
<div class="modal-body">
<p>You are currently logged in on a different session on the site. Please note that if you continue, the existing session will be terminated. Please change your password if you suspect that your account has been conpromised.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel Login</button>
<button type="button" id="modal-submit-button" class="btn"><?php echo get_phrase('login'); ?></button>
</div>
</div>
</div>
</div>
<div class="forgot-pass text-center">
<span><?php echo get_phrase('or'); ?></span>
<?php echo get_phrase('forgot_password'); ?>
</div>
<div class="account-have text-center">
<?php echo get_phrase('do_not_have_an_account'); ?>? <?php echo get_phrase('sign_up'); ?>
</div>
</form>
<script>
/** Trigger function on form submit whether to check user logged in */
function checkUserSession(){
var email = $("#login-email").val();
$.ajax({
url: "<?php echo site_url('login/checkUserSession'); ?>",
type: 'POST',
data: { 'email': email},
success: function(status){
if(status == true) { // User is already logged in somewhere, display the messege.
$("#login").modal();
return false;
} else { // User is not logged in, submit the form
return true;
}
}
});
}
/** Allow user to log in with exception */
$("#modal-submit-button").on("click", function(){
$("#login").modal('hide'); // hide the modal
$("#login-form").attr("onSubmit", ""); // unbind the function
$("#login-form").submit(); // submit login form
})
</script>
Controller -
<?php
/** Function to check user logged in or not */
public function checkUserSession() {
$user_email = $this->input->post('email');
$userId = $this->get_user_id($user_email);
$response = $this->user_has_session($userId);
echo $response;
}
public function get_user_id($user_email = "") {
$this->db->select('id');
$this->db->where('email', $user_email);
$user_id=$this->db->get('users');
return $user_id;
}
public function user_has_session($user_id=''){
$this->db->where('user_id',$user_id);
$this->db->from('ci_sessions');
$total=$this->db->count_all_results();
if($total<0)
return false;
else
return true;
}
?>

i have error An uncaught Exception was encountered Type: ArgumentCountError Message: Too few arguments to function

I am making a news editing feature using CodeIgniter 3, there is also an image edit here
But has errors like the following,
An uncaught Exception was encountered
Type: ArgumentCountError
Message: Too few arguments to function Operator::edit_berita(), 0 passed in D:\xampp\htdocs\ui-desa\system\core\CodeIgniter.php on line 532 and exactly 1 expected
Filename: D:\xampp\htdocs\ui-desa\application\controllers\Operator.php
Line Number: 164
Backtrace:
File: D:\xampp\htdocs\ui-desa\index.php
Line: 315
Function: require_once
Controller Operator.php
public function edit_berita($id_berita)
{
$data['title'] = 'Edit Berita';
$data['user'] = $this->db->get_where(
'user',
['id' => $this->session->userdata('id')],
['email' => $this->session->userdata('email')]
)->row_array();
$data['berita'] = $this->model_berita->getAllBeritaById($id_berita);
// $data['berita'] = $this->db->get('berita')->result_array();
// $data['berita'] = $this->model_berita->getNama();
$this->form_validation->set_rules('judul_berita', 'Judul Berita', 'required');
$this->form_validation->set_rules('isi_berita', 'Isi Berita', 'required');
if ($this->form_validation->run() == false) {
$this->load->view('templates/header', $data);
$this->load->view('templates/sidebar', $data);
$this->load->view('templates/topbar', $data);
$this->load->view('operator/editberita', $data);
$this->load->view('templates/footer');
} else {
$judul_berita = $this->input->post('judul_berita');
$slug_berita = url_title($this->input->post('judul_berita'), 'dash', 'TRUE');
$isi_berita = $this->input->post('isi_berita');
$tgl_berita = date('Y-m-d H:i:s');
$id = $this->session->userdata('id');
// Cek Jika Ada Gambar Yang DiUpload
$upload_image = $_FILES['gambar_berita'];
if ($upload_image) {
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['upload_path'] = './gambar_berita/';
$this->load->library('upload', $config);
if ($this->upload->do_upload('gambar_berita')) {
$old_image = $data['berita']['gambar_berita'];
if ($old_image != 'default.jpg') {
unlink(FCPATH . 'gambar_berita/' . $old_image);
}
$new_image = $this->upload->data('file_name');
$this->db->set('gambar_berita', $new_image);
} else {
echo $this->upload->display_errors();
}
}
$this->db->set('id_berita', $id_berita);
$data = array(
'judul_berita' => $judul_berita,
'isi_berita' => $isi_berita
);
$this->db->where($data);
$this->db->update('berita');
$this->session->set_flashdata('message', '<div class="alert alert-success" role ="alert"> Berita Berhasil di Reposting </div>');
redirect('operator/berita');
}
}
Model model_berita.php
public function getAllBeritaById($id_berita)
{
return $this->db->get_where('berita', ['id_berita' => $id_berita])->row_array();
}
View edit_berita.php
<!-- CK Editor 4 -->
<script src="<?= base_url('ckeditor/'); ?>ckeditor.js"></script>
<script src="<?= base_url('ckeditor/'); ?>samples/js/sample.js"></script>
<link href="<?= base_url('ckeditor/'); ?>samples/css/samples.css" rel="stylesheet">
<link href="<?= base_url('ckeditor/'); ?>samples/toolbarconfigurator/lib/codemirror/neo.css" rel="stylesheet">
<!-- Begin Page Content -->
<div class="container-fluid">
<!-- Page Heading -->
<h1 class="h3 mb-4 text-gray-800">
<?= $title; ?></h1>
<div class="row">
<div class="col-lg">
<?php if (validation_errors()) : ?>
<div class="alert alert-danger" role="alert">
<?= validation_errors(); ?>
</div>
<?php endif; ?>
<?= $this->session->flashdata('message'); ?>
<?= form_open_multipart('operator/edit_berita') ?>
<form action="" method="post">
<input type="hidden" name="id" value="<?= $berita['id_berita']; ?>">
<div class="modal-body">
<div class="form-group">
<small>Masukkan Judul Berita</small>
<input type="text" value="<?= $berita['judul_berita']; ?>" class="form-control" id="judul_berita" name="judul_berita" placeholder="Judul Berita..." required>
</div>
<div class="form-group">
<small>Masukkan Isi Berita</small>
<textarea class="form-control" name="isi_berita" id="editor" required><?= $berita['isi_berita']; ?></textarea>
</div>
<div class="form-group">
<label for="gambar_berita">Ganti Gambar Berita</label>
<div class="col-sm-12">
<div class="row">
<div class="col-sm-3">
<img src="<?= base_url('gambar_berita/') . $berita['gambar_berita']; ?>" class="img-thumbnail" alt="Gambar Berita">
</div>
<div class="col-sm-9">
<div class="custom-file">
<input type="file" class="custom-file-input" id="gambar_berita" name="gambar_berita">
<label class="custom-file-label" for="gambar_berita">Choose File</label>
</div>
</div>
</div>
</div>
<br>
<button type="reset" class="btn btn-danger" data-dismiss="modal">Reset</button>
<button type="submit" class="btn btn-primary">Add</button>
</div>
</form>
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- End of Main Content -->
<script>
initSample();
</script>
I've tried a number of ways, but it's still an error too. Please help so that my news update feature can work. Thanks.
Well your error clearly states that your method.
public function edit_berita($id_berita)
is expecting a parameter, which you have named $id_berita.
I cannot tell why you decided to have a parameter in this method, so I can only make some suggestions to help you solve your issue.
From what I can gather from your supplied code you could try the following options...
Option 1:
From what I can tell from your form, you are posting this as a hidden input, so you should be retrieving it from the Post Data.
<input type="hidden" name="id" value="<?= $berita['id_berita']; ?>">
So your method should become...
public function edit_berita()
{
$id_berita = $this->input->post('id'); // This needs to be validated
// The rest of your code below here...
}
But I would be validating that value to see if it exists before processing anything else.
Personally, I would be naming it as id_berita in your form to keep things matched up to avoid mistakes.
Option 2:
Another option would be to modify your form_open_mulitpart from
<?= form_open_multipart('operator/edit_berita') ?>
To include the id to pass in as a parameter
<?= form_open_multipart('operator/edit_berita/'.$berita['id_berita']) ?>
You will have to check that by inspecting your HTML Source using your Browsers "View Source" and inspect the HTML to see that it has ended up in the right place.
That will let you use your existing method
public function edit_berita($id_berita)
But again, you would need to validate that the passed in $id_berita is correct.
Which ever way you go, is your choice. You just need to read through your code and understand it a bit better.
I hope that gives you some guidance.

PHP form only returning empty array

I am working on a code segment that is a messaging function next to a list of names. On the page there is an envelope and when you click it a pop-up window appears with a text area inside of it. I would like to be able to fill that textarea with characters and send it as a message using my already functioning sendMessage function. I know my sendMessage function works and I've isolated the issue to the return given by the submit button. How do I get my submit button to POST the textarea to the page (keep in mind there are 2+ forms on this page). The code looks like this:
if (empty($_POST) === false) {
print_r($_POST);
$blah = $_POST;
echo "<script>window.top.location='asd.$blah'</script>";
}
if (empty($_POST['Send']) === false) {
echo "<script>window.top.location='../hidden/PNMasdd.php?gpa=$var1&year=$var2&major=$var3&sport=$var4'</script>";
echo 'YOU ARE IN THE SENDING MESSAGE FIELD';
if( empty ($_POST['message_full']))
{
$errors[] = 'All fields must be filled in!';
//print_r($errors);
}
if(empty($_POST) === false && empty($errors) === true)
{
$receiver_id = $temp_user['id'];
$sender_id = $user_data['id'];
$type = 0;
if(empty($is_Convo))
{
$is_Convo = 0;
}
$blahVar = $user_data['id'] . ',';
$message = str_replace("\r\n", "<br>", $_POST['message_full']);
$message_data = array(
'sender_id' => $user_data['id'],
'receiver_id' => $receiver_id,
'message_full' => $message,
'is_opened' => $blahVar,
'isConvo' => $is_Convo,
'type' => $type
);
//Put data limit code here
send_message($message_data);
$my_exten = "Messages/messages.php";
$noti_data = array(
'id_receive' => $receiver_id,
'id_sent' => $user_data['id'],
'type' => 1,
'exten' => $my_exten
);
notify($noti_data);
echo '<center>';
echo '<div class="alert alert-info">
<strong>It sent!</strong> You have sent a message successfully!
</div>
';
echo '</center>';
}
else{
echo output_errors($errors);
}
}
?>
<form id="wow" name="wow" action="" method="POST">
<div class="row">
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<center>
<textarea maxlength="100000" data-msg-required="Please enter your message." rows="10" cols="100%" class="form-control" name="message_full" id="message_full"></textarea>
</center>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<table width=90%><td width=45%><button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button></td><td width=45%>
<button type="submit" value="Send Message" name="Send" onclick="document.getElementById('wow').submit()" class="btn btn-primary">Send</button>
As pointed out in the comments, the buttons need to be inside your form.
Your setup is a bit odd for my taste, but try this out.. it will mostlikely need some tweaks and i'm not sure about the cancel button..
<form id="wow" name="wow" action="" method="post" >
<div class="form-group required">
<div class="col-md-12">
<textarea name="message_full" id="message_full" rows="10" cols="100%" class="form-control" maxlength="10000" placeholder="Please enter your message." autocomplete="off"></textarea>
</div>
</div>
<div class="buttons"> <!-- add css for this or change the class to yours.. -->
<input class="btn btn-default" data-dismiss="modal" value="Cancel" />
<input class="btn btn-primary" type="submit" value="Send Message" />
</div>
</form>
As #Barmar just said, you don't have any field named 'Send'. You should change:
if (empty($_POST['Send']) === false)
to:
if (empty($_POST['message_full']) === false)
(Edit)
Only the fields inside your form will get to $_POST superglobal, the submit button will not get there

Codeigniter multi forms on one page validation errors

I have two forms in my page. when I submit either of the form, both of them show the same validation error message. I used
if (isset ($_POST['click_cus_button']))
to check what button is licked in the from seems it does not work for me.
Can anyone tell me why this bit of code isn't working?
this is my controller (admin.php)
function add_users_data()
{
if (isset ($_POST['click_cus_button']))// click_cus_button is set load this validation errors
{
$this->form_validation->set_rules('cus_name', 'Name', 'trim|required|min_length[5]');
// validate name
$this->form_validation->set_rules('cus_email', 'Email', 'trim|required|valid_email');
// validate email
$this->form_validation->set_rules('cus_phone', 'Phone Number', 'trim|required|min_length[10]|max_length[10]');
// validate phone number
$this->form_validation->set_rules('cus_password', 'Password', 'required|matches[cus_passconf]');
// validate password
$this->form_validation->set_rules('cus_passconf', 'Retype Password', 'required');
// validate password
if ($this->form_validation->run() == FALSE)// if from validation failed load following pages from views
{
$this->load->view('admin/admin_add_users.php');
}
else // if from validation success load following pages from views
{
$data = array(
'cus_name' => $this->input->post('cus_name'),
'cus_email' => $this->input->post('cus_email'),
'cus_mobile' => $this->input->post('cus_mobile'),
'cus_phone' => $this->input->post('cus_phone'),
'cus_status' => $this->input->post('cus_status')
//'database_field' => $this->input->post('text_box_name')
);
$this->mod_users->add_record($data);//add a comment
$data['result']="Customer Added Successfully ";
$this->load->view('admin/admin_add_users.php',$data);
}
}
else if (isset ($_POST['click_oper_button']))// click_oper_button is set load this validation errors
{
$this->form_validation->set_rules('oper_firstname', 'Name', 'trim|required|min_length[5]');
// validate name
$this->form_validation->set_rules('oper_lastname', 'Name', 'trim|required|min_length[5]');
// validate name
if ($this->form_validation->run() == FALSE)// if from validation failed load following pages from views
{
$this->load->view('admin/admin_add_users.php');
}
else // if from validation success load following pages from views
{
$data['result']="Customer Added Successfully ";
$this->load->view('admin/admin_add_users.php',$data);
}
}
}
this is my View (admin_add_users.php)
I removed some fields to make the code shorter in the view
<?php echo form_open('admin/add_users_data') ?><!-- start of the form -->
<div class="span8">
<div style="padding-bottom:11px;">
<div class="div add_cus box"><!-- start of the add customer add form -->
<?php echo validation_errors('<div class="notice marker-on-bottom bg-darkRed fg-white" id="bottom_form_error">', '</div>'); ?>
<?php
if(isset($result))
{
echo '<div class="notice marker-on-bottom bg-green fg-white">'.$result.'</div>';
}
?>
<!-- Display Validation error massages -->
<feildset>
<legend><i class="icon-user-2 on-right on-left"></i><strong>Add Customer</strong></legend>
<input name='cus_status' type='hidden' value='disabled'/>
<input type="submit" value="Add Customer" class="info" name="click_cus_button">
</feildset>
</div><!-- END of the add customer add form -->
</div>
<div class="div add_oper box"><!-- start of the add Operator add form -->
<?php echo validation_errors('<div class="notice marker-on-bottom bg-darkRed fg-white" id="bottom_form_error">', '</div>'); ?>
<?php
if(isset($result))
{
echo '<div class="notice marker-on-bottom bg-green fg-white">'.$result.'</div>';
}
?>
<feildset>
<legend><i class="icon-user on-right on-left"></i><strong>Add Operator</strong></legend>
<h6>*All fields marked are required</h6>
<lable>Name*</lable><br>
<div class="input-control text size6" data-role="input-control">
<input type="text" name="oper_firstname" placeholder="First Name">
</div>
<div class="input-control text size6" data-role="input-control">
<input type="text" name="oper_lastname" placeholder="Last Name">
</div>
<br>
<div class="input-control radio default-style" data-role="input-control">
<label>
Gender*<br>
<input type="radio" checked="" name="oper_gender">
<span class="check"></span>
Male
</label>
</div>
<div class="input-control radio default-style" data-role="input-control">
<label>
<input type="radio" name="oper_gender">
<span class="check"></span>
Female
</label>
</div>
<br>
<lable>Date of Birth*</lable>
<div class="input-control text" data-role="input-control">
<input id="datepicker" name="oper_dob" type="text">
</div>
<lable>Address*</lable><br>
<div class="input-control text size1" data-role="input-control">
<input type="text" name="oper_add_no" placeholder="No">
</div>
<input type="submit" value="Add Operator" class="info" name="click_oper_button">
</feildset>
</div><!-- END of the add Operator add form -->
<div class="div add_admin">
RadioButton 3 Selected
</div>
</div>
<?php echo form_close(); ?><!-- END of the form -->

Categories