(A) //The sigup.inc.php page
//Instantiating signup object.;after successfully linking the signup controller
$newPerson = new Signup_Contr($firstName, $lastName);
//Calling the signupUser() from the controller
$result = $newPerson->SignupUser();
//Now if I want to send any json object to ajax; for example:
$message = ['status' => 'error', 'message' => "Username already existed"];
echo json_encode($message);
//(B): The problem
whenever I include the calling MVC method in the sigup.inc.php:
$result = $newPerson->SignupUser();
The jQuery's
success property
do not output. And when I debug it in Chrome the watch window say:
response: not available
However, if I do not include this method my jQuery output works perfectly...
HELP will be much appreciated !
Related
Here's my setup for an email contact form:
www.example.com/includes/contact_form.php
www.example.com/includes/contact_submit.php
www.example.com/contact/
/contact/ includes contact_form.php, and the form points to contact_submit.php to run.
When contact_submit.php successfully sends the mail, it does a redirect back to /contact/ but includes a $_GET variable.
header('Location: /contact/index.php?success=yup');
Then in contact_form.php I have:
if (isset($_GET['success'])) { echo 'Your message has been received etc'; exit(); }
Everything works fine. I made it this way so that the form couldn't be F5/refresh resubmitted, and it is successful in that.
However, anyone can access the success page at any time by manually entering the url, even if they don't submit the form. Is there any way around that?
Ofcourse.
Use sessions for that:
class ResponseLog {
private function __construct(){}
public static function hasMessages(){
return (isset($_SESSION['response']['messages']) && !empty($_SESSION['response']['messages'])) ? true : false;
}
public static function setResponse(array $response){
$_SESSION['response'] = $response;
}
public static function getLastResponse(){
$response = isset($_SESSION['response'])) ? $_SESSION['response'] : null;
#unset($_SESSION['response']);
return $response;
}
}
And use it like this:
if(isset($_POST['form'])){
//validation and all to proccess request goes here
if(!$valid){
$response = array('request' => $_POST,'messages' => array('Incorrect email','Please enter forename'),'url' => '/my/form/where/it/happen/');
}
else {
$response = array('messages' => array('Success'),'url' => '/my/form/where/it/happen/');
}
ResponseLog::setResponse($response);
//redirect to contact form
}
In success page or fail:
if(ResponseLog::hasMessages()){
$response = ResponseLog::getLastResponse();
foreach($response['messages'] as $message){
echo $message;
}
}
With this you can store everything user does and can work with data as you need.
I hope this help :)
Warn: I wrote it from mind, so it's untested code and it can be implemented better it's just for view how to work with user session and responses.
PS: But is a lot of ways how to do it, for example see flash messages in some framework and you will be see how it works with sessions etc.
You can use the referrer page if you want, and throw an error or redirect if it is not the form page, but it is not so good. You can also check whether the fields and form name have been posted.
$_SERVER['HTTP_REFERER']
I have created a CakePHP app and after already set the routes.php files,i can send JSON requests,in order to use my app as an API. For testing,i have created a function which goes like this:
public function api() {
if($this->request->is('post')) {
$data1 = (string)$this->request->data['Model']['data1'];
$data2 = (string)$this->request->data['Model']['data2'];
//logic goes here,it does stuff and $result is the variable where the result of the login is saved
//$data1 and $data2 are used in the logic
$result = 'result';
$this->set('results',$result);
$this->set('_serialize',array('results'));
}
}
I have also created the exact same function with another name,which is meant to be used via a web form and that works correctly. BUT,this code,at this function here,when i POST data (i use Dev HTTP Client chrome extension),it returns the $results variable empty,like it does not receive what i send :/
I send the data as follows via the chrome extension i use:
data1='stuff1'&data2='stuff2'
and it returns me just
{
"results":""
}
(while the same code works perfectly when used without json).
Did i miss something?Does it seem to do something wrong? Please help me a bit around here..
ps:if you need more info,just tell me and i'll post it.
Thank you in advance!
The correct way to access that post would be
$this->request->data['data1'];
to verify what data is being sent do this:
public function api() {
//if($this->request->is('post')) {
$data1 = (string)$this->request->data['Model']['data1'];
$data2 = (string)$this->request->data['Model']['data2'];
//logic goes here,it does stuff and $result is the variable where the result of the login is saved
//$data1 and $data2 are used in the logic
$result = 'result';
//$this->set('results',$result);
$this->set('results',array('root'=>$this->request);
$this->set('_serialize',array('results'));
//}
}
hello everyone i start to create a small application using codeigniter, and i want to submit my form using json i have a problem when i add json_encode inside my function, redirect() doesn't work i don't know why
this is my code
public function login_validate() {
if ($_POST) {
$login = $this->users->access(array(
'email' => $_POST['email'],
'password' => md5($_POST['password'])
));
if (!$login) {
redirect('/home');
}
echo json_encode($this->data);
}
}
how can i fix this problem
You should really be returning a flag in your JSON response, and then if flag is set, use window.location to got to the desired page.
What you are doing is redirecting the AJAX call to a new page, not your browser.
I am using this extension of the CI_Session Class. To create flash data and style them using TW Bootstrap. However when I go to pass the "Success" message to the view it's just not loading in at all.
I have had a good luck but no joy with it.
My Controller looks like this..
// Set form Validation
$this->form_validation->set_rules('title', 'Title', 'required|trim|is_unique[films.title]');
if($this->form_validation->run() == FALSE)
{
$data['page_title'] = 'Add New DVD';
$this->load->view('_partials/_header', $data);
$this->load->view('_partials/_menu');
$data['genres'] = $this->genre_model->get_genres();
$data['classification'] = $this->classification_model->get_classifications();
$this->load->view('create', $data);
$this->load->view('_partials/_footer');
}
else
{
// Insert into DB
$film_data = array
(
'title' => $this->input->post('title'),
'genre_id' => $this->input->post('genre'),
'classification_id' => $this->input->post('classification')
);
$this->film_model->create($film_data);
$this->session->set_success_flashdata('feedback', 'Success message for client to see');
redirect('dvd');
and my View is....
<?php $this->session->flashdata('feedback'); ?>
So when I insert a new item to the DB, The Model runs, The redirect runs but the "set_success_flashdata" doesnt.
The MY_session library is set to load automatically as per CI's default config.
How do I get this damn Flash Message to show ::( ?
It is not set_success_flashdata it is just set_flashdata.
Change in Controller :
$this->session->set_flashdata('feedback', 'Success message for client to see');
View is just as it is :
echo $this->session->flashdata('feedback');
Hope this helps you. Thanks!!
Although I fail to see the point of this particular extension, the answer is simple enough.
You are not echoing the data from the Session Library.
The correct way to echo flashdata is like so:
<?php echo $this->session->flashdata('feedback'); ?>
I'm using Cake 2.1, and with it comes the new JsonView. What I'd like to do is POST to a method in my controller and render an html fragment so that I can return it as a value in json.
Previously I'd do something like this:
public function ajaxSubmit() {
if (!$this->request->is('ajax')) {
$this->redirect('/');
} else {
$this->autoRender = $this->layout = false;
$message = 'Please enter a message';
$this->set('message');
$errorFragment = $this->render('/Elements/errors/flash_error');
$toReturn = array('errorFragment' => $errorFragment);
return json_encode($toReturn);
}
}
Which only sends back the html fragment of that particular flash_error element such that I can't have multiple key => values being sent back in a standard json object. I want to be able to send both html fragments and just plain text as json.
So my question really is, how can I render an HTML element and set it with a (key=>value pair) to be sent back as json from my controller using the JsonView that Cake 2.1 provides? I already have set in my routes file Router::parseExtensions('json'); and I'm including the RequestHandler component inside of my AppController.
You shouldn't need a separate action for AJAX when using data views. Use can use the same action as your non AJAX submit.
However assuming that you wish to use a different action for AJAX because I don't know what your other action looks like, you can write something like this in app/View/ControllerName/json/ajaxSubmit.ctp.
<?php
$errorFragment = $this->element('errors/flash_error');
$toReturn = array('errorFragment' => $errorFragment);
echo json_encode($toReturn);
Then change your action to this
public function ajaxSubmit() {
if (!$this->request->is('ajax')) {
$this->redirect('/');
} else {
$message = 'Please enter a message';
$this->set('message');
}
}
See "Using a data view with view files" in the documentation.