I'm using Laravel and in my website I need to make a registration throw many steps. There is a first view where the user enter his information and a profile picture then a second view to choose the account type and a third view to pay the inscription fee with Paypal.
I want to get all the information in the final view by using Input::all(), I hope there is an easy way to do that
I solved this myself by creating a php class which stores all this data and then create an object of this class and store that in a session.
class data{
public $data1;
public $data2;
public $data3;
//must checks if the data is complete
function checkdata(){
if(condition){
return true;
}else{
return false;
}
}
}
//Store data in session or load existing data from session.
if(isset($_SESSION["data"]){
$data = $_SESSION["data"];
}else{
$data = new data();
$_SESSION["data"] = $data;
}
//do with the data whatever needs to be done. change the variables if needed etc.
$data->$data1 = $_POST["data1"];
//once all data is complete send and save it where it needs to be saved and unset the session
if($data->checkdata()){
saveorsenddata($data);
unset($_SESSION["data"]);
}//else do nothing
Related
I have a view with a form where the user can change his name. After submit the form, I need to know if the name has changed or not.
At this momment, the code result allways in false (no changes), but the database table is updated correctly.
My User model has:
protected static function hasChanged()
{
self::updating(function($user){
if($user->isDirty())
{
return true;
}else
{
return false;
}
});
}
My ProfileController received the data in form.
public function updateProfile(Request $request)
{
$usuario = new User;
User::find(Auth::user()->id)->update($request->all());
$message = User::hasChanged() ? "Data changed" : "No data changed";
return redirect('home')->with('success', $message);
}
In $message allways get "No data changed", in spite of user has changed the data in the form.
I don't know if this is the best way to do it.
Thanks for help me.
The way that you have the hasChanged() method set up, this is actually not doing what you want it to do. Currently in your code, the hasChanged() method is registering an event handler for when the object is updating every time it is called. That means that hasChanged() doesn't actually return anything at all but it is adding a lot of unneeded overhead to your code in the long run.
What you'll want to do is split your call up into a couple of parts. When you call:
->update($request->all());
That is putting the values from the request into the object and then saving it all in one go. What you want to do is put the values into the object, then check to see if anything changed and then save it to the DB.
That will look something more like this:
$user = User::find(Auth::user()->id);
$user->fill($request->all());
$message = $user->isDirty() ? "Data changed" : "No data changed";
$user->save();
return redirect('home')->with('success', $message);
I'm new in PHP MVC, I have a question about how javascript works with php mvc
If I have a page with a button, when user click the button
It will send to next page and update data in database
$(document).ready(function(){
$("#btn").click(function(){
$.load(){... }
or $.post(){.....}//post data to another page
});
});
//next page
if(isset($_POST[])){
//update data
}
My question is
Should I send this data to controller than pass to model and output in view(if we need respond something)
Button --javascript--> controller -> model(update data) --send data back--> view
or
I can just send data to page and update without mvc
Sorry, i can't just comment your question yet
Your first approach is correct. Is recommended that you update data in models. Meanwhile, all SQL statements or ORM handles should be on it.
In your case, you have two options to show the data in view: Return a JSON in your php handle it with javascript, and load your view directly after update data. It depends how all your project is builded.
I can write some exemples, but you will need to give some peace of code.
// In your controller
if(isset($_POST)){
$obj = new MyObject();
$obj->name = $_POST['name'];
$obj->date = date("Y-m-d");
$obj->validatePost();
$obj->update();
$result = $obj->getData();
return $result;
}
// Your model
class MyObject {
public $name;
public $date;
public function validatePost(){
if($this->name == null){
// print error
}
}
public function update(){
// database cheets
}
public function getData(){
return $json;
}
}
I wanted to create a dynamic signup.php. The algorithm is as follow:
Algorithm
when signup.php is requested by client, the code will attempt to check whether user send any data in $_POST.
if $_POST does not contains any data (means it's the first time user request for signup.php), a signup form will be return to the user, allowing user to enter all his/her details and again send back to signup.php through submit button.
if $_POST does contains data (means user has fill up the signup form and is now sending all the data back to signup.php), then the php code will attempt validate all those data and return result showing user has been successfully registered or error if failed to do so.
The problem I'm having right now is how am I going to check whether it's the first time user request for signup.php or not?
Use isset() to check if $_POST contains data.
http://php.net/isset
To answer your question, "how am I going to check whether it's the first time user request for signup.php or not?", honestly, probably for other users......
There are a few ways, cookies, storing request ips in a database, bleh, bleh, bleh. But...... None of them are guaranteed. The user can disable cookies, use a dynamic ip, etc. You could issue a unique hash and place it as a login.php?q=encValueForUniquePageRequest
but...... The architecture you laid out won't be practical.
Sorry :(
To check that request is POST:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
//process new user
}
?>
Example:
<?php
Class signup_controller extends controller{
private $data = array();
private $model = array();
function __construct(Core $core){
parent::__construct($core);
/* load models - assign to model */
$this->model['page'] = $this->core->model->load('page_model', $this->core);
$this->model['auth'] = $this->core->model->load('auth_model', $this->core);
/* check script is installed - redirect */
if(empty($this->core->settings->installed)){
exit(header('Location: '.SITE_URL.'/setup'));
}
}
function index(){
/* do signup - assign error */
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if($this->model['auth']->create_user(1)===false){
$this->data['error'] = $this->model['auth']->auth->error;
}
}
/* not logged in */
if(empty($_SESSION['logged_in'])){
/* assign form keys */
$_SESSION['csrf'] = sha1(uniqid().(microtime(true)+1));
$_SESSION['userParam'] = sha1(uniqid().(microtime(true)+2));
$_SESSION['passParam'] = sha1(uniqid().(microtime(true)+3));
$_SESSION['emailParam'] = sha1(uniqid().(microtime(true)+4));
/* get partial views - assign to data */
$this->data['content_main'] = $this->core->template->loadPartial('partials/signup', null, $this->data);
$this->data['content_side'] = $this->core->template->loadPartial('about/content_side', null, $this->data);
/* layout view - assign to template */
$this->core->template->loadView('layouts/2col', 'content', $this->data);
}
/* signed in - redirect */
else{
exit(header('Location: ./user'));
}
}
}
?>
It's a long story that I'm fighting with, so I'm not going to extend to much for now, but and if not possible I will detail the problem.
I'm using Laravel framework. From an ajax call I send data from an upload form (plupload) to a function inside a controller.
Let's say I have the following functions in my controller:
function action_tempupload()
{
$temp = array();
$temp[] = Input::all();
return true;
}
function action_upload($news_id)
{
global $temp;
$input = $temp;
echo "<pre>";
//print_r($news_id);
print_r($input);
echo "</pre>";
exit();
}
function action_save($parameters = array())
{
// create news record in database and
// have a variable containing the news id sent to:
$this->upload($mysql->news_id);
}
Is it possible to have an temporary array, that saves each POST of the form for the image upload and call the array later in another function?
If they're are separate requests, then you can just store the array in a $_SESSION and unset all the created sessions in the last action method.
Click on the link of you need to know more about $_SESSIONS. The usage is pretty straight forward.
Sometimes i need data like array and sometimes i need same data as json.
Where would you do the check if is a ajax call, in controller or model or... Which one is better?
Test if is ajax call in controller
function my_controller(){
//getdata from model
$data=$this->my_model();
if(THIS_IS_AJAX_CALL){
echo json_encode($data);
}else{
return $data;
}
}
function my_model(){
//get the data from db
return $data;
}
Pass type as argument to model:
function my_controller(){
if(THIS_IS_AJAX_CALL){
return $this->my_model('json');
}else{
return $this->my_model();
}
}
function my_model($type=''){
//get the data from db
if($type='json'){
return json_encode($data);
}else{
return $data;
}
}
The controller. The model does not care how the data needs to be represented to the user, only the data itself.
A quote from the Codeigniter tutorial explaining MVC:
The Model represents your data structures. Typically your model classes will contain functions that help you retrieve, insert, and update information in your database.
The View is the information that is being presented to a user. A View will normally be a web page, but in CodeIgniter, a view can also be a page fragment like a header or footer. It can also be an RSS page, or any other type of "page".
The Controller serves as an intermediary between the Model, the View, and any other resources needed to process the HTTP request and generate a web page.
I think you should check in the controller(this has nothing to do with datastructures) the header to see if it is an ajax call, because jquery sets headers. If it is an Ajax call you should do the desired json_encode transformattion. I think your code should look something along the lines of the code below:
function is_xhr() {
return # $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] === 'XMLHttpRequest';
}
$data = /* get from model */
if( is_xhr() ){
// Not explicitly needed, but we like being accurate, right?:
header('Content-type: application/json');
echo json_encode($data);
exit(); // We don't need to render anything else
} else {
echo $data;
}