I am working on CI 3 when I post form data and insert into database then form a value that has been successfully save and I am also getting the last insert through $insert_id = $this->db->insert_id() following example as mention below:
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'password' => sha1($this->input->post('cpassword')),
'confirm_id' => sha1($this->input->post('email')),
'created_at' => date('Y-m-d H:i:s')
);
$success = $this->db->insert('instructor', $data);
$insert_id = $this->db->insert_id($success);
if($success == true)
{
$instructorData = $this->instructor_m->get_instructorData($insert_id);
$this->session->set_userdata('InstloginuserId',$instructorData->instructorID);
redirect(base_url('board'));
}
else
{
$this->session->set_flashdata('message', $this->lang->line('fail'));
redirect(base_url('register'));
}
If success condition is true then it redirects to board page but it throws status code: 303 I don't know why? Please help me to solve this.
The Session Library has been completely re-written in CodeIgniter 3 and now comes with a bunch of new features, but that also means that there are changes that you should make.
Read the Step 6: Update your Session library usage from CodeIgniter | Upgrading from 2.2.x to 3.0.x.
Related
I'm working with Laravel 5.8 and I wanted to make sure that users can submit a form only every 2 hours (as long as a session is alive).
So I tried this at the Controller:
if(Session::has('request_has_been_sent')){
return redirect()->back()->with('error', 'You just submitted a request and cannot submit a new one');
}else{
$withDraw = WithdrawWallet::create([
'balance_value' => $request->balance_wallet,
'can_draw' => $request->can_draw,
'shaba_number' => $request->shaba_number,
'first_name' => $request->first_name,
'last_name' => $request->last_name,
'description' => $request->desc,
'status' => 'pending',
'user_id' => auth()->user()->usr_id,
]);
Session::put('request_has_been_sent');
}
return redirect()->back()->with('success','Your request was sent');
So by this, everytime a user submits a new request, session request_has_been_sent must be set.
Therefore at the next time user submits the form, if the session was still alive, the message You just submitted a request and cannot submit a new one appears.
But now the problem is it does not work. In fact user can still submit another request, right after submitting a request.
This means that the session is not set somehow.
So what's going wrong here? How can I properly do this?
You can set session using set and put method
Session::put('name','value');
Retrieve it using get
Session::get('name')
So in your case you need to set your session use
Session::put('request_has_been_sent','yes');
And to check if it is set or not use
if(Session::get('request_has_been_sent')){
Docs: https://laravel.com/docs/5.0/session
I think your only problem here is not setting the session value to anything you need to set it to something or else laravel will ignore it.
Session::put('request_has_been_sent', true);
// Check if it exists, this will check if it exists without getting it
Session::has('request_has_been_sent')
Once the user is logged out al the user sesion wil be flushd out. Which means user and create and WithdrawWallet and logout and then login again and then user can create another WithdrawWallet so to fix that you can use cache
$cacheKey = WithdrawWallet::class.'.'.auth()->user()->usr_id;
if (Cache::has($cacheKey)) {
return redirect()->back()->with('error', 'You just submitted a request and cannot submit a new one');
} else {
$withDraw = Cache::remember($cacheKey, now()->addHours(2), function () use ($request){
return WithdrawWallet::create([
'balance_value' => $request->balance_wallet,
'can_draw' => $request->can_draw,
'shaba_number' => $request->shaba_number,
'first_name' => $request->first_name,
'last_name' => $request->last_name,
'description' => $request->desc,
'status' => 'pending',
'user_id' => auth()->user()->usr_id,
]);
});
}
return redirect()->back()->with('success', 'Your request was sent');
I am working in multistep form to submit data to database as well image,But i am stuck ,
View:
<?php
echo $this->Form->file('File',array(
'placeholder'=>'Upload Profile',
'accept'=>''
))
?>
Here is Controller code after user completed All step:
<?php
if (!empty($currentSessionData['Post']['File']) &&
is_uploaded_file($currentSessionData['Post']['File']['tmp_name'])) {
$fileData = fread(fopen($currentSessionData['Post']['File']['tmp_name'], "r"),
$currentSessionData['Post']['File']['size']);
$this->request->data[$table_name]['image_name'] = $currentSessionData['Post']['File']['name'];
$this->request->data[$table_name]['image_type'] = $currentSessionData['Post']['File']['type'];
$this->request->data[$table_name]['image_size'] = $currentSessionData['Post']['File']['size'];
$this->request->data[$table_name]['image_data'] = $fileData;
}
i am getting input using Post Model and saving data to other table which i have loaded using loadModel() method in cakephp.
Now here $currentSessionData is Session data where i am saving user's data of all step once user completed all steps then it comes to the save part in controller.
And $tablename is new table in phpmyadmin where i want to save image_name,type,size and data.
Now proble is is_uploaded_file() method is returning false ?
$currentSessionData returns :
'File' => array(
'name' => 'anonymous.png',
'type' => 'image/png',
'tmp_name' => 'C:\wamp\tmp\php2D7E.tmp',
'error' => (int) 0,
'size' => (int) 983
),
anonymous.png is selected file.
Any Help please I'm Stuck? Thanks in advance.
Im new to coding and web development as it is and diving into the deep end with API's is a thing i wish i never had done! However being said i have progressed further than expected. I am now having problems when trying to add custom fields to the add contact feature. Im trying to get the code to add the hidden form input fields when the user hits my thankyou page. I dont want to use Getresponses own Form builder for my main page so it was better to use the API. I have the code running perfectly when it comes to just adding the contact however when i add the set_contact_customs the code does not execute and fails with the following error: (Request have return error: Array) So i understand its to do with the set_contact_customs array however im clueless as to what it is i have done wrong.. Any advice and help is greatly appreciated as i am still learning the basics so picking up on what you experts say is a great learning curve. Thanks.
--- Below is the working version without the set_contact_customs ----
<?php
// Add contact to selected campaign id
try{
$result_contact = $client->add_contact(
$api_key,
array (
'campaign' => 'My-Camp-ID',
'name' => $fullname,
'email' => $emailaddress
)
);
echo "<p style='color: blue; font-size:24px;'>No Errors, Contact and Custom Fields have been added...</p>";
}
catch (Exception $e) {
echo $e->getMessage();
}
?>
--- Here is the code that causes the problems (with set_contact_customs) ----
<?php
// Add contact to selected campaign id
try{
$result_contact = $client->add_contact(
$api_key,
array (
'campaign' => 'My-Camp-ID',
'name' => $fullname,
'email' => $emailaddress
)
);
$result_contact = $client->set_contact_customs(
$api_key,
array(
'Survey Type' => $surveytype,
'Survey Cost' => $surveycost
)
);
echo "<p style='color: blue; font-size:24px;'> Contact Added </p>";
}
catch (Exception $e) {
echo $e->getMessage();
}
?>
API 2 doesn't really exist: in GetResponse they say version "1.5.0 - this is last JSON/RPC version of our API", especially if you were speaking 10 months ago. Now they are preparing to beta-test v3. So I will assume you were speaking about 1.5 and answer about it (I'm not familiar with v3, maybe there it's different).
You must send contact id with set_contact_customs, and you didn't.
When it says, "request error: array", it doesn't relate to your array (even though the problem is in your array, because you don't send in it contact id), they are sending an array as a response with error messages.
I'd love to tell you, where to get the contact id in order to send it, but I'm looking for it myself now. :)
UPDATE:
Ok, I combined it from pieces all over the internet, and now here's the working format.
You don't need to add_contact and then update it, you can do it in one go, adding the 'customs' parameter to the add_contact call (GR say, that we shouldn't expect for the contact to be added immediately, so you might not even get whom to update, if you call that function right away).
The fields for add_contact are described here.
The 'customs' parameter should look differently. Instead of:
array(
'Survey Type' => $surveytype,
'Survey Cost' => $surveycost
)
it should be:
array(
array( 'name' => 'Survey Type', 'content' => $surveytype ),
array( 'name' => 'Survey Cost', 'content' => $surveycost )
)
By the way, from what I tested, - blessedly, you don't need to define in GR UI those custom fields first, whatever you send, will be added or updated (in their limits for the custom field names and values).
I got error, when tried to send one custom field with empty content, when calling add_contact. When I sent it with set_contact_customs, I didn't get any error; I wanted to see, if it would delete the field or field value - it didn't do a thing.
If you still wish to update the existing contact, here's how to send the contact id with the update call:
$result = $client->set_contact_customs(
$api_key, array(
'contact' => $contact_id,
'customs' => $custom_fields_array
)
);
To first find contact id, you should call get_contacts. And since it's been said (I haven't tested it), that in different campaigns contacts with the same email address have different contact id, you should pass both the campaign, and the email with it.
As you can see, campaign can be sent in 'campaigns' parameter (then campaign id, that you got for add_contact, should be used), or in 'get_campaigns' (then the campaign name or even prefix can be used).
Here's the call with campaign id, for your code:
$result = $client->get_contacts(
$api_key, array(
'campaigns' => array( 'My-Camp-ID' ),
'email' => array( 'EQUALS' => $emailaddress )
)
);
To retrieve contact id from get_contacts, do the same as recommended for retrieving campaign id:
$contact_id = array_pop( array_keys( $result ) );
if ( empty( $contact_id ) ) {
//still not ok
}
else {
//you can call set_contact_customs
}
In order for that error message to be more descriptive, instead of just 'Request have return error: Array', open your jsonRPCClient.php, which you most surely include in your file with these GR function calls, and look for the following line:
!is_null($response['error']) => 'Request have return error: ' . $response['error'],
and replace it with the following, at least:
!is_null($response['error']) => 'Request have returned error: ' . var_export($response['error'], true),
Now your code will use the beloved var_export function and if you make a mistake, you will see in your error log something like:
Request have returned error: array (
'message' => 'Invalid params',
'code' => -32602,
)
I dedicate this thorough answer to all those, who helped me endlessly here on StackOverflow, just giving their answers to someone else's questions, sometimes years ago. Thank you! Hopefully my answer will save someone time, efforts, and mood, too. :)
I'm trying to add a User Note while programmatically adding a user. I've been working quite successfully from Joomla 3.0 Register user with php script, but am unable to figure out how J32 adds a note - in model notes.php, the save function is commented out, so it's a deadend for me while I figure out all this marvelous new version of the best CMS out there.
Is there some method to do this - an equivalent to registering like $model->addnote,
i couldn't find a method but here is the script i have used within my own plugin. All seems to work correctly
protected function _addNote($vars) {
$now = new JDate();
$now = $now->toSql();
// Create and save the user note
$userNote = (object) array(
'user_id' => $vars['id'],
'catid' => 0,
'subject' => "User Information Update By Proworx",
'body' => "<p>User ".$vars['name']." has been updated to:</p><p>Name: ".$vars['name']."</p><p>Email: ".$vars['email']."</p><p>Password: Not Telling... check proworx</p>",
'state' => 1,
'created_user_id' => 332, //DONT FORGET TO CHANGE THIS VALUE TO YOUR SITES ADMIN
'created_time' => $now
);
$db = JFactory::getDbo();
$db->insertObject('#__user_notes', $userNote, 'id');
}
i am exploring codeigniter for quite some time and most of the problem i faced while exploring it already solved by googling it but the problem i am facing right now is kind of weird. So here is the situation. I have a controller which will validate the form and pass all input from the form to the model using array if there is no error after the validation.
I have another function in the same controller which will send out email upon called. So what i wanted to do is validate the form, sent all input using array to model and insert it into the database then send email to the user's email address.
Below is part of the code from signup controller :
if($this->form_validation->run() == FALSE){
//load custom config
$this->config->load('site_config', TRUE);
$this->load->library('session');
$this->load->helper('form');
//retrieve data
$data['site_title'] = $this->config->item('site_title', 'site_config');
$data['site_login'] = $this->config->item('site_login', 'site_config');
$data['site_url'] = $this->config->item('site_url', 'site_config');
$data['img_folder_path'] = $this->config->item('img_folder_path', 'site_config');
$this->load->view('view_header', $data);
$this->load->view('view_signup',$data);
$this->load->view('view_footer');
}else{
$this->load->model('Adduserinfointoactivationtable');
$user_infos = array('first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email' => $this->input->post('email'),
'tel' => $this->input->post('tel'),
'fax' => $this->input->post('fax'),
'add_1' => $this->input->post('add_1'),
'add_2' => $this->input->post('add_2'),
'city' => $this->input->post('city'),
'post_code' => $this->input->post('post_code'),
'state' => $this->input->post('state'),
'password' => sha1($this->input->post('pass_2')),
'activation_code' => sha1(sha1($this->input->post('email').$this->input->post('pass_2'))),
);
$this->Adduserinfointoactivationtable->addUserInfo($user_infos);
$this->send_mail();
}
Here is the model :
public function addUserInfo ($user_infos){
$sql = "INSERT INTO tbl_user_Account (email_add, activation_code, fname, lname, tel, fax, add1, add2, city, post_code, state, password) VALUES (?,?,?,?,?,?,?,?,?,?,?,?);";
$query = $this->db->query($sql, array($user_infos['email'], $user_infos['activation_code'], $user_infos['first_name'], $user_infos['last_name'], $user_infos['tel'], $user_infos['fax'], $user_infos['add_1'], $user_infos['add_2'], $user_infos['city'], $user_infos['post_code'], $user_infos['state'], $user_infos['password']));
$query->result_array();
}
when i place the $this->send_mail(); right after the else, it works. I did some testing and the email function is working fine. but when i call it this way which is after $this->Adduserinfointoactivationtable->addUserInfo($user_infos);, it doesn't send out email. I also noticed no matter what i do after $this->Adduserinfointoactivationtable->addUserInfo($user_infos);, for example load a view or echo out something, it doesn't work.
Can someone advice me on this?
I think your issue might be the fact that, according to the CodeIgniter documentation, $this->db->query() returns a boolean value if an "insert" type query is performed.
This is happening in your case. Because of this, you're probably creating a fatal error by executing $query->result_array().
As a simple fix, you can probably try the following:
if (is_object($query)) {
return $query->result_array();
} else {
return $query;
}
You are not loading any view in the else part, are you? Do a redirect to the same function or to a success function page.