EDIT - I tried storing data inside session values and pass it as parameters for the model to process, but that didn't work either.
Okay before you downvote this, hear me out. I have an HTML form to get user reviews. User can leave a review after logging into his Google Account. I am using the Google 0Auth 2.0 API for this. So when you click submit on the form, you're redirected to Google Authentication page where you can choose which account you'd like to use.
However if you click on "Add another account" and put in the credentials, the data that I got through my original User Review Form
which is title of review
and review content
is "unset". I know this because the final query for the database that inserts the data obtained from the Google login as well as the Review Form
outputs "review_title" value can't be null
. I know it's very confusing but trust me this is the best I can explain it.
Controller.php
defined('BASEPATH') OR exit('No direct script access allowed');
class Googlelogin extends CI_Controller
{
public function __construct()
{
parent::__construct();
require_once APPPATH . 'third_party/src/Google_Client.php';
require_once APPPATH . 'third_party/src/contrib/Google_Oauth2Service.php';
}
public function index()
{
// $this->load->view('pages/review');
}
public function login()
{
$clientId = 'dddd.googleusercontent.com'; //Google client ID
$clientSecret = 'dddd-ddd'; //Google client secret
$redirectURL = base_url() . 'googlelogin/login';
//https://curl.haxx.se/docs/caextract.html
//Call Google API
$gClient = new Google_Client();
$gClient->setApplicationName('Login');
$gClient->setClientId($clientId);
$gClient->setClientSecret($clientSecret);
$gClient->setRedirectUri($redirectURL);
$google_oauthV2 = new Google_Oauth2Service($gClient);
if (isset($_GET['code'])) {
$gClient->authenticate($_GET['code']);
$_SESSION['token'] = $gClient->getAccessToken();
header('Location: ' . filter_var($redirectURL, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$gClient->setAccessToken($_SESSION['token']);
}
if ($gClient->getAccessToken()) {
$userProfile = $google_oauthV2->userinfo->get();
// print_r($userProfile);
$name = $userProfile['name'];
$email = $userProfile['email'];
$picture = $userProfile['picture'];
$this->load->library('form_validation');
$this->form_validation->set_rules('title', 'title');
$this->form_validation->set_rules('message', 'message');
$title = $this->input->post('title');
$message = $this->input->post('message');
// MY SESSION SOLUTION
$this->session->set_userdata($array);
$session_title = $this->session->userdata('title');
$session_message = $this->session->userdata('message');
print_r($this->input->post());
if ($this->form_validation->run() === FALSE) {
echo "error in your form ";
$this->session->set_flashdata('review_error', 'Please fill all the fields!');
$this->load->view('pages/review');
// redirect(base_url().'review/');
} else {
echo $title, $message;
$this->session->set_flashdata('review_success', 'Thank you for you ');
$this->review_model->set_review($name, $email, $picture, $session_title , $session_message);
redirect(base_url() . 'review/');
}
// echo "just dies";
die;
} else {
$url = $gClient->createAuthUrl();
header("Location: $url");
exit;
}
}
}
I am getting error in the login
method. Inside the FormValidation
condition.
Model.php
class Review_model extends CI_Model
{
public function set_review($google_name, $google_email, $google_image, $title, $message)
{
$data = array('review_title' => $title,
'review_content' => $message,
'email' => $google_email,
'name' => $google_name,
'image_url' => $google_image);
return $this->db->insert('reviews', $data);
}
public function get_review(){
$query = $this->db->get('reviews');
return $query->result_array();
}
}
The models is giving the actual output i.e. "review_title" cannot be NULL
.
Here's the form if anyone is interested.
Form.php
<form action="<?php echo base_url(); ?>review/submit/" method="post">
<input type="hidden" name='<?php echo $this->security->get_csrf_token_name(); ?>'
value='<?php echo $this->security->get_csrf_hash(); ?>'>
<div class="form-group">
<label for="exampleInputEmail1">Title</label>
<!-- <input type="text" name="title">-->
<?php echo form_input(['name' => 'title', 'class' => 'form-control', 'placeholder' => 'Enter your review title']); ?>
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Review</label>
<!-- <textarea rows="10" cols="100" name="message"></textarea>-->
<?php echo form_textarea(['name' => 'message', 'class' => 'form-control', 'placeholder' => 'Enter your review title', 'rows' => '10', 'cols' => '100']); ?>
</div>
<?php echo form_submit(['name' => 'submit', 'value' => 'submit']) ?>
</form>