CodeIgniter not reacting to post - php

When I submit a form for the url example.com/index.php/topic/1/test-topic-test CodeIgniter does not recognize that a post form is submitted.
Routes:
$route["topic/(:num)/([a-z]+)"]["post"] = "forums/topic_post_reply/$1/$2";
Forums.php controller:
public function topic_post_reply($id, $name)
{
$message = $this->input->post("topic_reply_content");
if(!empty($message) && !empty($this->session->userdata('id')))
{
$data = [
"content" => $message,
"author" => $this->session->userdata('id'),
"reply_date" => time(),
"parent" => $id
];
$this->db->insert("forum_topics_replies", $data);
}
else
{
die("Something went wrong");
}
}
Form:
<form class="uk-form-stacked" action="<?php echo base_url(); ?>index.php/topic/<?php echo $this->uri->segment(2); ?>/<?php echo $this->forums_model->slug($this->uri->segment(3)); ?>" method="post">
<div class="uk-form-inline">
<textarea class="uk-textarea" name="topic_reply_content" rows="4" placeholder="Write a lovely reply..."></textarea>
</div>
<div class="laevis-reply-hidden">
<div class="uk-margin-small" style="margin-bottom:0">
<input type="submit" class="uk-button uk-button-primary uk-width-1-1" value="Post">
</div>
</div>
Why isn't this working?

I had to have the post route above all other routes for the same url or it would not work. I also had to change it to $route["topic/(:num)/:any"]["post"].

Related

Why does the validation for my codeigniter website fail everytime?

I have a controller code like so:
$validation=service('validation');
$validation->reset();
$data = [
'validation' => $validation,
];
if ($this->request->getMethod() === 'post'){
$validation->setRules( [ 'comment' => 'required',] );
if($validation->run()) {
$CommentsModel->save(['Comment' => $this->request->getPost('comment'),]);
return redirect()->to('/someplace...');
}
}
}
return view('templates/header', $data)
. view('templates/menu')
. view('addComment', $data)
. view('templates/footer');
}
Everytime my form is posted, the validation fails... even when I have something in comment! Any idea why this would be?
This is the form:
<form action="/Add/<?= esc($book)?>/<?= esc($chapter) ?>" method="post">
<?= csrf_field() ?>
<label for="comment">Comment</label>
<textarea name="comment" id="comment" ><?= set_value("comment")?></textarea><br />
<?php if($validation->hasError('comment')) echo '<p class="validationError">' . $validation->getError('comment') . '</p>'?>
<input type="submit" name="submit" value="Add Comment" />
</form>

Codeigniter 3 blog application: redirecting to the updated post fails

I am working on a basic (just 2 tables: authors and posts) blog application in Codeigniter 3.1.8.
I have an edit post functionality, using an update form:
<?php echo form_open("posts/update"); ?>
<input type="hidden" name="id" id="pid" value="<?php echo $post->id; ?>">
<div class="form-group <?php if(form_error('title')) echo 'has-error';?>">
<input type="text" name="title" id="title" class="form-control" placeholder="Title" value="<?php echo $post->title; ?>">
<?php if(form_error('title')) echo form_error('title'); ?>
</div>
<div class="form-group <?php if(form_error('desc')) echo 'has-error';?>">
<input type="text" name="desc" id="desc" class="form-control" placeholder="Short decription" value="<?php echo $post->description; ?>">
<?php if(form_error('desc')) echo form_error('desc'); ?>
</div>
<div class="form-group <?php if(form_error('body')) echo 'has-error';?>">
<textarea name="body" id="body" cols="30" rows="5" class="form-control" placeholder="Add post body"><?php echo $post->content; ?></textarea>
<?php if(form_error('body')) echo form_error('body'); ?>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-block btn-md btn-success">
</div>
<?php echo form_close(); ?>
In the Posts_model model I have the method responsible with updating the post:
public function update_post() {
$data = [
'title' => $this->input->post('title'),
'description' => $this->input->post('desc'),
'content' => $this->input->post('body')
];
$this->db->where('id', $this->input->post('id'));
return $this->db->update('posts', $data);
}
In the Posts controller, I have 2 methods, for editing and updating the post:
public function edit($id) {
$data = $this->Static_model->get_static_data();
$data['post'] = $this->Posts_model->get_post($id);
$data['tagline'] = 'Edit the post "' . $data['post']->title . '"';
$this->load->view('partials/header', $data);
$this->load->view('edit');
$this->load->view('partials/footer');
}
public function update() {
$this->Posts_model->update_post();
// Redirect to the updated post
}
In my update() method, I was unable to redirect to the updated post. The redirect($this->agent->referrer()); line only brings me back to the update form. Where I want to redirect to is the just updated post. How do I do that?
Post vars should be handled in your controller:
public function update() {
$id = $this->input->post('id');
$data = [
'title' => $this->input->post('title'),
'description' => $this->input->post('desc'),
'content' => $this->input->post('body')
];
// Update post
$this->Posts_model->update_post($id, $data);
// Redirect to the updated post
redirect('posts/post/' . $id);
}
Model:
public function update_post($id, $data) {
$this->db->where('id', $id);
return $this->db->update('posts', $data);
}
Note: if you want to do it your way, then simply return the id in the model function and redirect based on the return of the function in update()

How to avoid duplication when submitting data to json file via php?

I have a code that submits data into a cake json 'database', but when I submit using PHP. When I reload the page, the file repeats the code of the last object in the JSON file when I get it. How do I avoid this?
This is my PHP
if(isset($_POST["submit"]))
{
if(empty($_POST["name"]))
{
$error = "<label class='text-danger'>Enter Name</label>";
}
else if(empty($_POST["type"]))
{
$error = "<label class='text-danger'>Enter Type</label>";
}
else if(empty($_POST["diff"]))
{
$error = "<label class='text-danger'>Enter Difficulty</label>";
}
else
{
if(file_exists('../../databases/cakes.json'))
{
$current_data = file_get_contents('../../databases/cakes.json');
$array_data = json_decode($current_data, true);
$extra = array(
'person' => array(
'name' => $_POST['name'],
'difficulty' => $_POST["diff"],
'type' => $_POST["type"],
'isNew' => 'true',
'isVeg' => 'false',
)
);
$array_data[] = $extra;
$final_data = json_encode($array_data);
if(file_put_contents('../../databases/cakes.json', $final_data))
{
//.-.
}
}
else
{
$error = 'JSON File not exits';
}
}
}
?>
<body>
<div id="layout"></div>
<div id="content">
<div id="add">
<div class="form-title"><h1>Add Cake</h1></div>
<form method="post">
<?php
if(isset($error))
{
echo $error;
}
?>
<br />
<div class="input-field">
<label for="name">Cake Name</label>
<input type="text" name="name"/>
</div>
<br />
<div class="input-field">
<label for="diff">Difficulty</label>
<div class="select">
<select name="diff" id="slct">
<option>Choose an option</option>
<option value="male">EZ</option>
<option value="female">Meh</option>
<option value="matthew">Mildy Hard</option>
</select>
</div>
</div>
<br />
<div class="input-field">
<label for="type">Type</label>
<input type="text" name="type"/><br />
Need Suggestions?<br>
</div>
<input class="addCake" type="submit" name="submit" value="Add Cake!"/><br />
See some other cakes
<?php
if(isset($message))
{
echo $message;
}
?>
</form>
Submission Works.
Result:
[[{"cake":{"name":"tes1","diff":"EZ","type":"Deli","isNew":"true","isVeg":"false"}}]]
But when I reload the page I see two of this things...
Result:
[[{"cake":{"name":"tes1","diff":"EZ","type":"Deli","isNew":"true","isVeg":"false"},{"name":"tes1","diff":"EZ","type":"Deli","isNew":"true","isVeg":"false"}}]]
Use ($_SERVER['REQUEST_METHOD'] == 'POST') instead of ($_POST["submit"])
$array_data = array_merge($array_data, $extra); instead of $array_data[] = $extra;

i am using two submit button one form in codeigniter it was worked well in chrome but iam using the firebox both button going to the else part

<body>
<div id='container'>
<pre>
<fieldset>
<legend>login</legend>
<?php echo validation_errors(); ?>
<form method="post" id="login" name="login">
username :<input type="text" name="uname" id="uname" value="<?php echo $this->session->flashdata('uname') ?>" ><br>
password :<input type="password" name="upass" id="upass">
<input type="checkbox" name="remember">Remember me
<input type="submit" value="login" class="submit" name="login"> <input type="submit" value="signup" name="signup">
</form>
</fieldset>
</pre>
<div><?php echo $this->session->flashdata('error'); ?></div>
</div>
<div id="error"></div>
</body>
controller:
public function view()
{
if($this->input->post("login"))
{
if($this->input->post('remember')=='on')
{
$uname=$this->input->post("uname");
$this->session->set_flashdata('uname',$uname);
}
$this->form_validation->set_rules('uname','username','trim|required');
$this->form_validation->set_rules('upass','password','required');
$data['title']='login page';
if ($this->form_validation->run() === FALSE)
{
$this->session->set_flashdata('error','enter username and password');
$this->load->view('stud_det/login',$data);
}
else
{
$data['result']=$this->stud_model->login();
if(!empty($data['result']))
{
$session=array(
'uname'=>$this->input->post('uname'),
'upass'=>$this->input->post('upass')
);
$this->session->set_flashdata($session);
$this->session->set_userdata($session);
$this->load->view('stud_det/view',$data);
}
else{
$this->session->set_flashdata('error','username or password is incorrect');
$this->load->view('stud_det/login',$data);
}
}
}
else{
$this->load->view('stud_det/index',array('title'=>''));
}
In your first if condition, you are loking for 'login' POST variable, however 'login' is the name of the submit input type. You have to test the post value 'uname' and 'upass'. Something like this:
if($this->input->post("uname") && $this->input->post("upass")) {
// Your code...
}
Other thing, you do not need define name to form tag. The name 'login' it is duplicated in form and input tags.

Codeigniter - Displaying page

I'm having a problem displaying my index.php in codeigniter. Can someone help me with this?
Here's my index.php:
<form id="myForm" action="<?php echo baseurl('controller/CreateUser'); ?>" method="post">
Name: <input type="text" name="name"/><br/>
Username: <input type="text" name="username"/><br/>
Password: <input type="password" name="password"/><br/>
<button id="sub">Save</button>
</form>
<span id="result"></span>
And here's my controller:
public function index(){
$this->load->view( 'layouts/header', [ 'title' => 'Never Stop Learning!' ] );
$this->load->view( 'NeverStopLearning/index');
$this->load->view( 'layouts/footer' );
}
public function CreateUser(){
$data = array(
"name" => $this->input->post("name"),
"username" => $this->input->post("username"),
"password" => $this->input->post("password")
);
if($this->model->Insert('table_users', $data)){
echo "Successfully Inserted";
}else{
echo "Insertion Failed";
}
}

Categories