Post by AdieB » Thu Feb 07, 2019 10:27 pm

I have a simple HTML form that passes three inputs to the controller via POST. The only thing is that when entering only one input filed, it returns a result for that field only and the other two are empty.
So, if I enter A into Username and leave the other two entries blank, the result is:-
array(3) { ["guest_name"]=> string(1) "A" ["guest_message"]=> string(0) "" ["guest_length"]=> string(0) "" }
array(4) { ["route"]=> string(15) "guestbook/entry" ["guest_name"]=> string(1) "A" ["guest_message"]=> string(0) "" ["guest_length"]=> string(0) "" }

But, if I submit all 3 form inputs, I get zero result, I get the following result.
array(0) { }
array(1) { ["route"]=> string(15) "guestbook/entry" }


The Template code is:-
<?php echo $header; ?>
<div class="container">
<ul class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<li><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a></li>
<?php } ?>
</ul>
<?php if ($success) { ?>
<div class="alert alert-success"><i class="fa fa-check-circle"></i> <?php echo $success; ?></div>
<?php } ?>
<div class="row"><?php echo $column_left; ?>
<?php if ($column_left && $column_right) { ?>
<?php $class = 'col-sm-6'; ?>
<?php } elseif ($column_left || $column_right) { ?>
<?php $class = 'col-sm-9'; ?>
<?php } else { ?>
<?php $class = 'col-sm-12'; ?>
<?php } ?>
<div id="content" class="<?php echo $class; ?>"><?php echo $content_top; ?>
<h1><?php echo $heading_title; ?></h1>
<form name="put" action="<?php echo $action; ?>" method="post">

Username: <input type="text" name="guest_name" id="guest_name" />
password: <input type="text" name="guest_message" id="guest_message" />
length: <input type="text" name="guest_length" id="length" />
<input type="submit" value="Submit" />
</form>
<?php echo $test;
echo "</br>";
echo $action;
echo "</br>";
echo $guest_name;
echo "</br>";
echo $guest_message;
echo "</br>";
echo $guest_length;
?>

<?php echo $content_bottom; ?></div>
<?php echo $column_right; ?></div>
</div>
<?php echo $footer; ?>

The controller code is:-
<?php

class ControllerGuestbookEntry extends Controller {
private $error = array();

public function index() {
$this->load->language('guestbook/guestbook');

$this->document->setTitle($this->language->get('heading_title'));

if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
$this->load->model('guestbook/guestbook');
$data['subject'] = sprintf('New guestbook entry submitted by %s', $this->request->post['guest_name']);
$data['message'] = $this->request->post['guest_message'];
$this->model_guestbook_guestbook->processGuestbookEntry($data);
$this->session->data['success'] = $this->language->get('text_success');
$this->response->redirect($this->url->link('guestbook/entry', '', true));


}


$data['success'] = '';
if (isset($this->session->data['success'])) {
$data['success'] = $this->session->data['success'];
unset($this->session->data['success']);
}

$data['breadcrumbs'] = array();

$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/home')
);

$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('guestbook/entry', '', true)
);

$data['heading_title'] = $this->language->get('heading_title');
$data['entry_guest_name'] = $this->language->get('entry_guest_name');
$data['entry_guest_message'] = $this->language->get('entry_guest_message');
$data['entry_submit'] = $this->language->get('entry_submit');


if (isset($this->error['guest_name'])) {
$data['error_guest_name'] = $this->error['guest_name'];
} else {
$data['error_guest_name'] = '';
}

if (isset($this->error['guest_message'])) {
$data['error_guest_message'] = $this->error['guest_message'];
} else {
$data['error_guest_message'] = '';
}

$data['action'] = $this->url->link('guestbook/entry', '', true);


if (isset($this->request->post['guest_name'])) {
$data['guest_name'] = $this->request->post['guest_name'];
} else {
$data['guest_name'] = 'Guest Name not set';
}

if (isset($this->request->post['guest_length'])) {
$data['guest_length'] = $this->request->post['guest_length'];
} else {
$data['guest_length'] = 'Guest length not set';
}


if (isset($this->request->post['guest_message'])) {
$data['guest_message'] = $this->request->post['guest_message'];
} else {
$data['guest_message'] = 'Guest Message not set';
}



$data['column_left'] = $this->load->controller('common/column_left');
$data['column_right'] = $this->load->controller('common/column_right');
$data['content_top'] = $this->load->controller('common/content_top');
$data['content_bottom'] = $this->load->controller('common/content_bottom');
$data['footer'] = $this->load->controller('common/footer');
$data['header'] = $this->load->controller('common/header');
$data['test'] = 10 *1.3;
var_dump($_POST);
echo "</br>";
var_dump($_REQUEST);
echo "</br>";
//var_dump($GLOBALS);
$this->response->setOutput($this->load->view('guestbook/entry', $data));
}

protected function validate() {
if (utf8_strlen(trim($this->request->post['guest_name'])) < 1) {
$this->error['guest_name'] = $this->language->get('error_guest_name');
}

if (utf8_strlen(trim($this->request->post['guest_message'])) < 1) {
$this->error['guest_message'] = $this->language->get('error_guest_message');
}

return !$this->error;
}
}

Newbie

Posts

Joined
Mon May 19, 2014 9:01 pm

Post by AdieB » Fri Feb 08, 2019 12:34 am

I found that commenting out the following code from the controller, the error disappears. Not sure why, but I don't need that bit of code so it doesn't affect me further.

if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
$this->load->model('guestbook/guestbook');
$data['subject'] = sprintf('New guestbook entry submitted by %s', $this->request->post['guest_name']);
$data['message'] = $this->request->post['guest_message'];
$this->model_guestbook_guestbook->processGuestbookEntry($data);
$this->session->data['success'] = $this->language->get('text_success');
$this->response->redirect($this->url->link('guestbook/entry', '', true));


}

Newbie

Posts

Joined
Mon May 19, 2014 9:01 pm
Who is online

Users browsing this forum: No registered users and 41 guests