Post not working from text input, with CI - php

I'm trying to post some information from a from do so some calculations, however for some reason that data isn't posting. I do a var_dump after I get the info, and it still is blank. Any help would be great.
My Controller:
<?php
class Timevalueshow extends Controller{
function index(){
$this->load->view('Timevalueshow_view');
}
function submit(){
$years = $this->input->post('years');
$rate = $this->input->post('rate');
$principle = $this->input->post('principle');
$periods = $this->input->post('periods');
$isCont = $this->input->post('continuous');
var_dump($years);
$params = array(
'years' => $years,
'rate' => $rate,
'principle' => $principle,
'periods' => $periods,
'isCont' => $isCont
);
var_dump($params);
$this->load->library('timevalue',$params);
$this->timevalue->leprint();
}
}
?>
And the view file.
<head>
<title>Time Value of Money</title>
</head>
<body>
<div id="container">
<form method="POST" action="http://localhost:8888/CodeIgniter_1.7.2/index.php/timevalueshow/submit">
<p>Years: </p> <input id="years" type="text" />
<p>Rate: </p> <input type="text" id="rate"/>
<p>Principle: </p> <input type="text" id="principle"/>
<p>Periods: </p> <input type="text" id="periods"/>
<p>Continuous?: </p> <input type="checkbox" id="continuous"/>
<input type="submit" value="Submit"/>
</form>
</div>
</body>

You didn't submitted the form elements' name's only their id's. Edit your form so the elements contain name attributes too.

Related

How to add correctly 2 items in this JSON? - Laravel

I must save x2 the same data fields (nif and name) at the same time in the DB.
With 1 field (nif) it works perfectly and save x2 rows in the DB with different info, so the JSON works, but adding the 2th field (name) it just save the nif value in all the fields.
I don't understand so much the JSON and his syntax logic, but I think the problem is how I wrote it in the controller.
P.D. No, I can't put EnviarCurriculumPreguntas::create x2 in a row because that's not the purpose of this code, so I'm using a JSON instead.
EnviarCurriculum.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class EnviarCurriculum extends Model
{
protected $table = 'table';
protected $primaryKey = 'ID_table';
protected $fillable = [
'nif',
'name'
];
public function getRepeatedFields()
{
return json_decode($this->nif);
return json_decode($this->name);
}
}
EnviarCurriculumController.php
namespace App\Http\Controllers\enviarCurriculum;
use App\EnviarCurriculum;
use App\Configuracion;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class EnviarCurriculumController extends Controller
{
public function index()
{
return view('enviar_curriculum', ['EnviarCurriculum' => new EnviarCurriculum()]);
}
public function create()
{
return view('enviar_curriculum', ['EnviarCurriculum' => new EnviarCurriculum()]);
}
public function store(Request $request)
{
foreach (request('nif', 'name') as $val) {
EnviarCurriculum::create(['nif' => $val, 'name' => $val]);
}
}
}
enviar_curriculum.blade.php
<!DOCTYPE html>
<html lang="es">
<head>
...
</head>
<body>
<form action="{{ route("store") }}" method="POST">
#csrf
<div>
<input type="text" name="nif[]" id="nif">
<input type="text" name="name[]" id="name">
</div>
<br>
<div>
<input type="text" name="nif[]" id="nif">
<input type="text" name="name[]" id="name">
</div>
<input type="submit" class="btn btn-primary" value="Enviar">
</form>
</body>
</html>
you can't have input having the same name without being an array
<!DOCTYPE html>
<html lang="es">
<head>
...
</head>
<body>
<form action="{{ route("store") }}" method="POST">
#csrf
<div>
<input type="text" name="nif[]" id="nif">
<input type="text" name="name[]" id="name">
</div>
<br>
<div>
<input type="text" name="nif[]" id="nif">
<input type="text" name="name[]" id="name">
</div>
<input type="submit" class="btn btn-primary" value="Enviar">
</form>
</body>
</html>
Then in your store method loop the array inputs
public function store(Request $request)
{
$nifs = $request->input('nif', []);
$names = $request->input('name', []);
foreach ($nifs as $key => $nif) {
EnviarCurriculum::create(['nif' => $nif, 'name' => $names[$key]??'']);
}
}
Then fix your getRepeatedFields() method to return both decoded fields.
public function getRepeatedFields()
{
return [
'nifs' => json_decode($this->nif),
'names' => json_decode($this->name),
];
}
<input type="text" name="nif[]" id="nif">
<input type="text" name="name[]" id="name">
in name , nif only store the last inserted item please change it as array

What to do when a declare array variable is undefined at view but working with controller laravel 5.7

Here is the code:
public function chat($id=1){
Route::view('/chat', 'chat');
$id = View::make('chat.blade', ['reviewer_id' => Reviewer::findOrFail($id)]);
$audiences = DB::table('audience')->get();
$data = [
'id'=>$id,
'audiences'=>$audiences,
'audience_id'=> 2
];
return View::make('chat.blade', ['data'=>$data]);
}
As the code is simple I route to blade view, get data from database, get audience data, initialize data array return data to chat.blade simple code but in view
Undefined variable: data (View:
/Users/userinfo/Sites/chat/resources/views/chat.blade.php)
View code:
<div>
#foreach($data->audiences as $info->audience)
{{$info->audience->id}};
#endforeach
</div>
<div>
<form action="/" method="post">
<input type="hidden" value={{$reviewer_id}} name="id">
<input type="hidden" value={{$audience_id}} name="id">
<input type="text" name="message">
<input type="submit" value="submit">
</form>
</div>
<?php $__currentLoopData = $data->audiences; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $info->audience): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
<?php echo e($info->audience->id); ?>;
<?php endforeach; $__env->popLoop(); $loop =
$__env->getLastLoop();
?>
> undefined variable $data
change $data->audiences to $data['audiences'] in your view. $data is an array not an object
If i do it then i do it like this
public function chat($id = 1){
Route::view('/chat', 'chat'); // I don't know what that is
$reviewer_id = Reviewer::findOrFail($id); // or Reviewer::find($id);
//I Update this little bit : $audiences = DB::table('audience')->get();
$audiences = Audience::all();
$audience_id = 2 ;
return view('chat.blade', compact(['id','audiences','audience_id','reviewer_id']));
}
Now you can access all the variables passed in compact in your blade file like this
<div>
// Depends on what is in the $audiences could be with "$key => $value"
#foreach($audiences as $key)
{{$key->id}};
#endforeach
</div>
<div>
<form action="/" method="post">
<input type="hidden" value={{$reviewer_id}} name="id">
<input type="hidden" value={{$audience_id}} name="id">
<input type="text" name="message">
<input type="submit" value="submit">
</form>
</div>

Codeigniter not updating my data

So I'm new to codeigniter and developing a viewing catalogue website. And so far my update function isn't working and I've been at this for 3 hours
so here's my view:
<?php echo validation_errors(); ?>
<?php echo form_open('catalogPages/updateEvent'); ?>
<input type="hidden" name="id" value="<?php echo $events['event_id']; ?>">
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control" name="title" placeholder="Add
Title" value= "<?php echo $events['event_name']; ?>">
</div>
<div class="form-group">
<label>Body</label>
<textarea id="editor1" class="form-control" name="body" placeholder="Add
Body" value = "<?php echo $events['event_desc']; ?>"></textarea>
</div>
<button type="submit" class="btn btn-default">Submit</button>
My Model:
public function editEvent()
{
$data = array(
'event_name' => $this->input->post('event_name'),
'event_desc' => $this->input->post('event_desc'),
'event_id' => $this->input->post('event_id')
);
$this->db->where('event_id', $this->input->post('event_id'));
return $this->db->update('events', $data);
}
Controller:
public function editEvent($id = NULL)
{
$this->load->model('event_model');
$data['events'] = $this->event_model->get_event($id);
$this->load->view('adminEventUpdate', $data);
}
public function updateEvent()
{
$this->load->model('event_model');
//$id = $this->input->posts('event_id');
$this->event_model->editEvent();
}
It works but so far when I try to update the data doesn't seem to be updated but rather doesn't change at all.
When this Html form is submitted, the $_POST array has the keys:
'id', 'title', and 'body'.
There are no inputs named 'event_id', 'event_name', 'event_desc'.
The Html input properties 'name' need to change.

How to use post method via MVC

This is my view file called application.html.
I just started learning the Zend framework.
I have existing database table (mytable.sql), with three columns:
ID
APP_ID
APP_NAME
So now my goal is, I need create new app using post method and that new app name must be save in to database.
<form method="post" action="/admin/appmgt/createAction">
<p>
<label class="field" for="APP_ID"><span>*</span>APP_ID</label>
<input type="text" name="APP_ID" class="textbox-300" />
</p>
<p>
<label class="field" for="APP_NAME"><span>*</span>APP_NAME</label>
<input type="text" name="APP_NAME" class="textbox-300" />
</p>
<input type='submit' name='submit' value='Submit Form' />
</form>
This my controller file:
public function createAction() {
$this->view->form = $this->getForm();
if ($this->getRequest()->isPost()) {
if (!$this->view->form->isValid($_POST)) {
return $this->render('form');
}
}
}
This is my Model file:
public function createApp($APP_ID, $APP_NAME) {
$data = array(
'APP_ID' => $APP_ID,
'APP_NAME' => $$APP_NAME
);
$this->insert($data);
}
Could you please help me ?

CodeIgniter : How to set a value on the edit page

I want to make an edit form , but I have a problem when displaying the data . This is a warning in my code ..
A PHP Error was encountered
Severity: Notice
Message: Undefined index: pakar_username
Filename: m_pakar/edit_pakar.php
Line Number: 20
This is my view:
<form method="post" role="form" action="<?=base_url()?>admin/m_pakar/edit_pakar?>">
<label>Username</label>
<input class="form-control" type="username" name="username" value="<?php echo $coba['pakar_username']?>" ><br>
<label>Password</label>
<input class="form-control" type="text" name="password" value="<?php echo $coba['pakar_password']?>"><br>
<label>Email</label>
<input class="form-control" type="email" name="email" value="<?php echo $coba['pakar_email']?>" ><br>
<button type="submit" class="btn btn-success">Update</button>
</form>
My Controller:
public function edit_pakar($id){
$this->general->set_table('data_pakar');
$this->general->order('pakar_id', 'asc');
$datasend['coba'] = $this->general->get_result_array();
$datasave = array(
'pakar_username' => $this->input->post('username', TRUE),
'pakar_password' => md5($this->input->post('password', TRUE)),
'pakar_email' => $this->input->post('email', TRUE),
);
$this->general->set_table('data_pakar');
$this->general->where($datasave);
$this->general->update($datasave);
$dataview['content'] = $this->load->view('admin/m_pakar/edit_pakar', $datasend, TRUE);
$this->load->view($this->template, $dataview);
}
Is there something wrong with my code ? not only pakar_username , but all existing data on the edit form doesn't show.
Why have you got all this bloated code in your controller? Move the Database stuff to a model...
HTML Form Fix:
<form method="post" role="form" action="<?=base_url()?>admin/m_pakar/edit_pakar">
Secondly, you're not pushing the details in "datasave" to the view, you're only pushing "datasend", so I adjusted for "coba2" incase you need coba...
$datasend['coba'] = $this->general->get_result_array();
$datasave = array(
'pakar_username' => $this->input->post('username', TRUE),
'pakar_password' => md5($this->input->post('password', TRUE)),
'pakar_email' => $this->input->post('email', TRUE),
);
$datasend['coba2'] = $datasave;
Now edit input fields as so:
value="<?php echo $coba2['pakar_username']?>"

Categories