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');
}
Related
I have an html form and a connection to database, and when I hit submit button the information goes to my db. But if I refresh my page after, without pushing any button, the information goes again and again and again. Please help me to fix this.
I also would appreciate help of php form CRUD, from UI. Can you help me with code, let's say there are buttons, which show my database tables, where I can make changes from UI.
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<meta charset="utf-8">
<title></title>
</head>
<body>
<form action="form.php" method="post">
<table>
<tr>
<td>Name:</td>
<td>
<input type="text" name="name" placeholder="name" required> <br>
</td>
</tr>
<tr>
<td>
Surname:
</td>
<td>
<input type="text" name="surname" placeholder="surname" required>
</td>
</tr>
</table>
<input type="submit" name="submit">
</form>
</form>
</body>
</html>
form.php
<?php
//Create connection
$servername='localhost';
$username = 'root';
$password = '';
$dbname='testdb';
//create connection
$conn = new mysqli($servername,$username,$password,$dbname);
if($conn->connect_error){
echo $conn->connect_error;
}else {
echo "connected successfully<br/>";
}
//post method
if(isset($_POST['submit'])){
$name = $_POST['name'];
$surname = $_POST['surname'];
}
#insert data
$result = "INSERT INTO users(name,surname) VALUES('$name','$surname')";
$show = "SELECT * FROM users";
if($conn->query($result)){
echo "data inserted successfully";
}else {
$conn->connect_error;
}
$conn->close();
?>
There are several solutions for your issue.
The PHP approach: Redirect after the successful storage in your database. You can use the PHP header function for redirecting to another page with a success message.
The JavaScript approach: This approach is a little more cumbersome as it requires the data to be submitted using an ansychronous request with JavaScript. After the successful response of your script, you can then hide the form with JavaScript and display a success message for the user.
Help for your CRUD system
Since you have not come up with your own approach, you can not expect that you will get the exact help you need here. There are tons of examples, how you can achieve a CRUD system.
Here 's the basic approach of CRUD.
class Person
{
public function create($name, $surname) : int
{
// insert data into database and return the auto_increament value
return $id;
}
public function read(int $id) : array
{
// read out the data by the given id and return the data
return $data;
}
public function update($id, $data) : bool
{
// update the database entry with the given id and the given data and return the response of the update statement (true or false)
return $result;
}
public function delete($id) : bool
{
// delete the database entry with the given id and return the response
return $result;
}
}
This is how you should beginn. Then you have to deal with the data you need in your view. For reading out a specific person you need an id. For updating a specific person, you need an id. Same for deleting a specific person. Think about how to get the id. You can solve this with an hidden input element in your form or a GET parameter in the URL of the form.
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();
i am tring to set cookie in codeigniter but i cannt make it happen i dont know what is wrong.
there is no error on the error log page...
this is my view page...
<!DOCTYPE html>
<html>
<head>
<title>
EKART
</title>
</head>
<body>
<h1>SIGN IN</h1>
<form id="admin" action="/do/ekart/adminlogin/login/" method="POST">
Name :<br/>
<input type="text" name="name" id="name"/><div id="name_display"></div><br/>
Password :<br/>
<input type="password" name="password" id="pasword"/><div id="password_display"></div><br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
and here is my controller..
adminlogin.php
<?php
class adminlogin extends Controller{
public function __construct(){
parent::Controller();
$this->load->library('mylib');
}
public function index(){
$this->load->view("ekart/adminlogin");
}
public function login(){
$name=$_POST["name"];
$password=$_POST["password"];
$data=array("username"=>$name,"password"=>$password);
$result=$this->mylib->logincheck($data);
if($result){
echo "every thing is fine";
setcookie("myCookie",$name,time()+3600,"/");
$this->load->view("ekart/home",array("message"=>""));
}
}
}
the control is going inside if and it also prints "every thing is fine" the home page is also get loaded but i dont know why the cooke is still not set..
mylib is my library to check login validation....
There is a codeigniter way of doing this.
Make sure to have $this->load->helper('cookie'); in your controller, or autoload it.
$cookie = array(
"username" => $name,
"time" => time()+3600
);
$this->input->set_cookie($cookie);
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
<?php
class SignupController extends Zend_Controller_Action
{
function indexAction()
{
if ($this->_request->isPost())
{
echo "Your email address is: " . $this->_request->getPost('email');
}
$this->render("signup");
}
}
This is my controller
this is my view
<html>
<body>
<form action="/signup" method="post">
Email: <input name="email" type="text" />
<input name="btnSubmit" value="Click me" type="submit" />
</form>
</body>
</html>
when I tried to run the view
I am getting result
blank/signup
why this happens
please rectify that error, and tell me the reason for that error
If you are using signup view then replace your render code from this :-
$this->_helper->viewRenderer('signup');
Try it with else:
if ($this->_request->isPost()) {
echo "Your email address is: " . $this->_request->getPost('email');
} else {
$this->render("signup");
}