controller not displaying a message in phalcon php - php

I’m trying to make a simple example in Phalcon PHP framework, so i have a view which contain two fields name and email and a submit button. When i click in this button a function of a controller is called to store the name and the email in the DB. This action goes well the problem is I’m trying to display a message after the action ends but i still have the view that contain the form (name, email). Here's my code.
My checkout controller.
<?php
class CheckoutController extends \Phalcon\Mvc\Controller
{
public function indexAction()
{
}
public function registerAction()
{
$email = new Emails();
//Stocker l'email et vérifier les erreurs
$success = $email->save($this->request->getPost(), array('name', 'email'));
if ($success) {
echo "Thanks for shopping with us!";
} else {
echo "Sorry:";
foreach ($user->getMessages() as $message) {
echo $message->getMessage(), "<br/>";
}
}
}
}
The view
<!DOCTYPE html>
<html>
<head>
<title>Yuzu Test</title>
</head>
<body>
<?php use Phalcon\Tag; ?>
<h2>Checkout</h2>
<?php echo Tag::form("checkout/register"); ?>
<td>
<tr>
<label for="name">Name: </label>
<?php echo Tag::textField("name") ?>
</tr>
</td>
<td>
<tr>
<label for="name">E-Mail: </label>
<?php echo Tag::textField("email") ?>
</tr>
</td>
<td>
<tr>
<?php echo Tag::submitButton("Checkout") ?>
</tr>
</td>
</form>
</body>
</html>

You can use Flash Messages, so you don't have to break the application flow.
Regards

echo() during controller code won't (shouldn't) work unless you turn off your views, because its buffered and cleared after dispatching.
If you want to be sure it's happening this way, just add die() at the end of registerAction() method.
If you create separate view for registerAction(), you can use there variables you declare with $this->view->message = ... or $this->view->setVar('message', ...) in controller method. Than, in view file you can reuse them by <?php echo $this->view->message; ?> or <? echo $message; ?>.

I think you have to write following line in the end of your controller function registerAction
$this->view->disable();

Related

how does the view and my controller "connect"

I am following a mvc tutorial but i dont understand how and why my code does't work.
So i have a Controller like so.
public function indexAction()
{
$formSent = false;
if (isset($_POST['send'])){
$formSent = true;
}
$this->view->setVars([
'name' => 'Stefan',
'formSent' => $formSent
]);
}
and my form which is located in views/index
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Forumlar</title>
</head>
<body>
<form method="post" action="/index">
<table>
<tr>
<td>Vorname</td>
<td><input type="text" name="vorname"></td>
</tr>
<tr>
<td>Nachname</td>
<td><input type="text" name="nachname"></td>
</tr>
<tr>
<td>PLZ</td>
<td><input type="number" name="plz"></td>
</tr>
</table>
<button type="submit" name="send">Send</button>
</form>
</body>
<?php
echo $name;
if ($formSent){
echo "Form is Sent!";
}
?>
To make my question more simple to understand and where my problem is.
So echoing $name, does output in this case "Stefan".
While anything that is done with the form doesn not work.
For example dumping _POST will be empty and my if formSent statement does not work.
So how exacltey do i "connect" these two or how does it work ?
Thank you.
EDIT: Here its waht it says after i send the form
Object not found! The requested URL was not found on this server. The
link on the referring page seems to be wrong or outdated. Please
inform the author of that page about the error.
If you think this is a server error, please contact the webmaster.
Error 404
EDIT 2:
Yes i do thave this in my IndexController
protected $view;
public function setView(\Mvc\Library\View $view)
{
$this->view = $view;
}
To connect the view to controller, u need to load the view page in controller..
in controller,
public function index() {
$this->load->view('YOUR PAGE NAME IN VIEW FOLDER');
}

Why it directs me to a wrong url using codeigniter,

I have a two controller function, Index function and generateReport function, when I click the export button it should direct me to this URI index.php/schoolScoreCardReport/generateReport, the problem is it directs me to this URI index.php/generateReport, i dont know why, here is my code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class schoolScoreCardReport extends PG_Controller {
public function __construct(){
parent::__construct();
}
public function index(){
if(isset($_POST['Export'])){
$this->generateReport();
}
$this->layout->view('report/index');
}
public function generatePOMEDetailReport(){
$this->layout->view('test/index');
}
}
Pls help me thankyou here is my html view for index function
<table>
<?php echo form_open('schoolScoreCardReport'); ?>
<tr><td>
<label>Region:</label>
<select name="region_name" style="width:150px">
<option value = "1">a</option>
</select>
</td></tr>
<tr><td>
<input type="submit" id="btn_export" value="Export" name="Export">
</td></tr>
<?php echo form_close(); ?>
</table>
Unless you have some routes going on to affect the requests, I believe the parameter sent to form_open() should be as follows...
<?php echo form_open('schoolScoreCardReport/generatePOMEDetailReport'); ?>
As generatePOMEDetailReport is the only function shown in your controller code above other than the index function.
If you really want it to call the index function so the IF $post check is done, try 'schoolScoreCardReport/index' for the parameter.
I think there is a one-to-one relationship between a URL string and its corresponding controller class/method in codeigniter. The segments in a URI normally follow this
example.com/class/function/id/
or you see this link :https://www.codeigniter.com/userguide3/general/routing.html
in form_open u need to follow this
<?php echo form_open('ControllerName/FunctionName'); ?>
in your case it is
<table>
<?php echo form_open('schoolScoreCardReport/generatePOMEDetailReport'); ?>
<tr><td>
<label>Region:</label>
<select name="region_name" style="width:150px">
<option value = "1">a</option>
</select>
</td></tr>
<tr><td>
<input type="submit" id="btn_export" value="Export" name="Export">
</td></tr>
<?php echo form_close(); ?>
</table>

Phalcon does change views

<?php
class IndexController extends \Phalcon\Mvc\Controller {
public function indexAction(){
}
}
?>
<?php
class SignupController extends \Phalcon\Mvc\Controller {
public function indexAction(){
}
}
?>
<?php
echo "<h1>Hello!</h1>";
echo Phalcon\Tag::linkTo( "signup", "Sign Up Here!");
?>
<?php use Phalcon\Tag; ?>
<h2>Sign up using this form</h2>
<?php echo Tag::form( "signup/register" ); ?>
<p>
<label for="name">Name</label>
<?php echo Tag::textfield( "name" ); ?>
</p>
<p>
<label for="email">E-Mail</label>
<?php Tag::textfield( "email" ); ?>
</p>
<p>
<?php echo Tag::submitButton( "Register" ); ?>
</p>
</form>
I was following a tutorial on phalcon framework here but it can't get it to work. I have created the controllers for the index page and the signup page. I also created the views for the index controller and the view for the signup controller.
What happens is that when I click on the link to go to the signup page it shows the url correct which means we should be on the signup page but it shows the index view not the signup view. Basically when i click on the signup link the only thing that changes in the browser is the url but not the page.
anyone know what is going on here?
Ok,
I got this working in the end.
Make sure that (in my case Nginx) you confirm that it has been set up correctly.
The second thing to look at is the code to handle the request. I have used this:
$application = new \Phalcon\Mvc\Application();
$application->setDI($di);
if (!empty($_SERVER['REQUEST_URI'])) {
$pathInfo = $_SERVER['REQUEST_URI'];
} else {
$pathInfo = '/';
}
echo $application->handle($pathInfo)->getContent();
This isn't exactly what I wanted, but for some reason my PATH_INFO was coming out as empty, even when I have set cgi.fix_pathinfo to 1 in the php.ini
Hope this helps.

MySQL, PHP - Forms Issue

This is a little part of a college website project I have and I've ran into this issue. I hope you can help me with it.
I've made a small representation of this issue so it's easier to read.
What I'm trying to do here is:
doubt1.php = shows a form.
doubt2.php = shows form with the values from doubt1.php for confirmation.
doubt3.php = saves values to database.
class.php = library of clases(only name).
The problem is that it saves empty values at doubt3.php.
If I skip doubt2.php and redirect the form from doubt1.php to doubt3.php I have no problem at all, it saves successfully.
These are the codes:
doubt1.php
<html>
<body>
<form name=f action=doubt2.php method=post>
<input name=name value="Hello";>
<input type=submit>
</form>
</body>
</html>
doubt2.php
<html>
<head>
<?php
$y=$_REQUEST['name'];
?>
</head>
<body>
<form name=f action=doubt3.php method=post>
<input name=name value="<?php echo $y; ?>" disabled>
<input type=submit>
</form>
</body>
</html>
doubt3.php
<?php
$c=mysql_connect("localhost","root","root");
mysql_select_db("doubtdb");
if(!mysql_select_db("doubtdb")){
$q1="create database doubtdb";
$q2="use doubtdb";
$q3="create table data(name varchar(10))";
mysql_query($q1,$c);
mysql_query($q2,$c);
mysql_query($q3,$c);
mysql_select_db('doubtdb');
}
include "class.php";
$obj=new data($_REQUEST['name']);
$obj->save($c);
echo "Saved";
?>
class.php
<?php
class data{
private $name;
function __construct($name){
$this->name=$name;
}
function set_name($name){
$this->name=$name;
}
function get_name(){
return $this->name;
}
function save($c){
$q="insert into data values('$this->name')";
mysql_query($q,$c);
mysql_close($c);
}
}
?>
disabled input is not submited with the form.
try "readonly"
http://www.w3.org/TR/html401/interact/forms.html#h-17.12.1

loading a view twice in codeigniter

Hello I just started CodeIgniter. I am having problem in loading view.
My scenrio is I am creating a sample add form. After submit it goes to controller and insert entries in database. I want if the database operation is successfull it again comes to same view having again some values. And on the basis of those values I am showing some particular rows informing user about insertion operation. My function in controller looks like
public function add_user()
{
$this->load->view('add_user');
$post=$this->input->post();
if(isset($post['name']))
{
$data=array(
'name'=>$post['name'],
'designation'=>$post['designation']
);
if($this->db->insert('user',$data))
$result['update']=true;
else
$result['update']=false;
$this->load->view('add_user',$result);
}
}
And my view looks like
<h1 align="center">Add User</h1>
<table border="0" cellpadding="2" cellspacing="2" align="center">
<?php
if(isset($update))
{
if($update)
{
?>
<tr bgcolor="#00FF00">
<td>Record Added Successfully</td>
</tr>
<?php
}
else
{
?>
<tr bgcolor="#FF0000">
<td>Insertion Operation Failed</td>
</tr>
<?php
}
}
?>
<?php echo(form_open('first/add_user'));?>
<tr>
<td>Name</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Designation</td>
<td>
<select name="designation">
<option value="Junior PHP Developer">Junior PHP Developer</option>
<option value="Senior PHP Developer">Senior PHP Developer</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="Add User" />
</td>
</tr>
</form>
</table>
Now What I want that if insertion operation is successfull I am sending true value to view and if not I am sending false value. And on the basis of this value I am showing some rows. I am loading the view two times as per I understood the logic. Because first time it loads the form and second time It loads view with some value of true or false. But what happens that after it reloads there are two forms. I know this problem is due to double loading of my view. i want to ask if there is another way of sending values after database operation to view?
Simply load your view once:
public function add_user()
{
$post=$this->input->post();
$result = array();
if(isset($post['name']))
{
$data=array(
'name'=>$post['name'],
'designation'=>$post['designation']
);
if($this->db->insert('user',$data))
$result['update']=true;
else
$result['update']=false;
}
$this->load->view('add_user',$result);
}
By the way your code is a bit messy, work on it
// try something like this
//you may need to use form validation helper
//load add user form
public function add_user(){
//any data required for the form
$data['anything'] = '';
$this->load->view('add_user',$data);
}
//to process adding user action, form action will point to this
function adding_user(){
if($this->input->post('name')){
$data=array(
'name'=>$post['name'],
'designation'=>$post['designation'];
if($this->db->insert('user',$data)){
echo 'user added successfully!';
}else{
redirect(user/add_user);
}
);
}
}

Categories