How to call add_action after another add_action perform - ajax

I need to run hooks one after another I had tried the code where first hook working but another hook never called . I am making ajax request ajax request success but section action function (test) not calling.
PHP / Wordpress
<?php
add_action( 'wp_ajax_my_action', 'my_action' );
add_action('wp_ajax_my_action' , 'test');
//add_action('my_action' , 'test');
function my_action() {
//global $wpdb; // this is how you get access to the database
$one = $_POST['valone'];
$two = $_POST['valtwo'];
echo $sum = $one+$two;
//test();
wp_die(); // this is required to terminate immediately and return a proper response
}
function test(){
echo "Hello World";
}

You are terminating all code when you use wp_die in the first hook. This will terminate PHP execution and stop any future code from running. Essentially, it will stop what it was trying to do and return what it has to the AJAX call.

You can try calling the same action from your first action like below,
add_action( 'wp_ajax_my_action', 'my_action' );
function my_action() {//global $wpdb; // this is how you get access to the database
$one = $_POST['valone'];
$two = $_POST['valtwo'];
echo $sum = $one+$two;
//test();
add_action('wp_ajax_my_action' , 'test');
wp_die(); // this is required to terminate immediately and return a proper response
}
function test(){
echo "Hello World";
}
Hope this helps

Related

My laravel (version 7) session variables are not accessible outside declared function on server

This is my Controller
Session variable generated in set_value function are not accessible inside
function get_value. This code works fine on my localhost, but not
working on server. All session related files are same as laravel
documentation. Laravel version is 7.
public function set_value(Request $request){
session_start();
Session::put('name', 'Hello World');
}
public function get_value(){
$data = Session::get('name');
echo '<pre>';
print_r($data);
}
You need to call save() and remove session_start();
Session::put('name', 'Hello World');
Session::save();
To store data in session, you can use:
public function set_value(Request $request){
$request->session()->put('name', 'Hello World');
}
There is also another way, through the global helper:
session(['name' => 'Hello World']);
To get the session value, you'd use:
Session::get('name');

Missing argument 1 for Teepluss\Theme\Theme::Teepluss\Theme\{closure}()

i am using a theme manager for la-ravel Teepluss but when i bind any things it gives this error :
Missing argument 1 for Teepluss\Theme\Theme::Teepluss\Theme{closure}()
here is the screenshot of error
http://i.imgur.com/elQ5qLY.png
just adding this line $this->theme->bind('active', 'home'); to the below code gives error
public function showWelcome()
{
$this->theme->layout('default');
$this->theme->setTitle($this->config['SEO']['home']['title']);
$this->theme->setMeta_desc($this->config['SEO']['home']['meta_description']);
$this->theme->setMeta_keywords($this->config['SEO']['home']['meta_keywords']);
$this->theme->bind('active', 'home');
return $this->theme->scope('home.index')->render();
}
i think the package theme binding has some issues
Finally i found the issue. There is bug in this package. There shouldn't be any argument in the clouser and the code should be something like this :
public function bind($variable, $callback = null)
{
$name = 'bind.'.$variable;
// If callback pass, so put in a queue.
if ( ! empty($callback))
{
// Preparing callback in to queues.
$this->events->listen($name, function() use ($callback, $variable)
{
return ($callback instanceof Closure) ? $callback() : $callback;
});
}
// Passing variable to closure.
$_events =& $this->events;
$_bindings =& $this->bindings;
// Buffer processes to save request.
return array_get($this->bindings, $name, function() use (&$_events, &$_bindings, $name)
{
$response = current($_events->fire($name));
array_set($_bindings, $name, $response);
return $response;
});
}

not able get flashdata in CodeIgniter

Here is my controller code
function index() {
echo $this->session->flashdata('message');
$this->load->view('categoryView');
}
function delete() {
$products = $this->item_model->get_category_id($category_id);
if (count($products)) {
$message = 'Category Name is used by the product. Please change them to another category!';
}
else {
$category_id = $this->product_category_model->delete($category_id);
$message = ($category_id) ? 'Category Deleted Successfully' : 'Failed to Delete Category';
}
$this->session->set_flashdata('message', $message);
redirect('category', 'refresh');
}
After calling the delete function the flashdata has to be set and retrieve that value in index() function of the same controller but I can't.
Am also tried the $this->session->keep_flashdata('message'); before redirect to index function. But still am not get any value in index function.
Also changed $config['sess_expire_on_close'] = FALSE; to $config['sess_expire_on_close'] = TRUE; Still am not get the result.
I am wasted more time(nearly half day). Please anybody help to retrieve the flas data in codeigniter.
There are several probabilities:
1- Check your browser cookie. See if it allows cookies or if any other extension is intervening.
2- Refresh might not send the browser a new request (which thus mean a new URL). Try it for another controller see if it make any change
3- hard code the set_flashdata() with hard-coded value than a variable. set_flashdata("message", "Thank you");
In codeingitor you cannot echo anything in controller.
You have to set in $this->session->flashdata('message'); in view file not in index() function of controller
Try again by putting $this->session->flashdata('message'); in "categoryView" file where you want to display flash message.
You could try:
function set_flashdata_notification($notify_type, $msg, $error_code = null)
{
$ci =& get_instance();
$flashdata_data = set_notification_array($notify_type, $msg, $error_code);
$ci->session->set_flashdata($flashdata_data);
}
function delete() {
$products = $this->item_model->get_category_id($category_id);
if (count($products)) {
set_flashdata_notification('Category Name is used by the product.','Please change them to another category!');
redirect('path_to_view/view','refresh');
}

Login Throttling Function with Active Record

I have a function that I found that was wrote to use PDO, I modified it to use codeigniters active record db class. Everything work EXCEPT when I place the code within a function like so:
function login_attempt_count() {
$seconds = 10;
// First we delete old attempts from the table
$oldest1 = strtotime(date('Y-m-d H:i:s').' - '.$seconds.' seconds');
$oldest2 = date('Y-m-d H:i:s',$oldest1);
$del_data = $oldest2;
$this->db->where('when <', $del_data);
$this->db->delete('Login_Attempts');
// Next we insert this attempt into the table
$data = array(
'ip' => $_SERVER['REMOTE_ADDR'],
'when' => date("Y-m-d H:i:s"));
$this->db->insert('Login_Attempts', $data);
// Finally we count the number of recent attempts from this ip address
$count = 'SELECT count(*) as number FROM Login_Attempts WHERE ip = ?';
$num = $this->db->query($count, $_SERVER['REMOTE_ADDR']);
if ($num->num_rows() > 0){
foreach($num->result() as $attempt){
$attempts = $attempt->number;
return $attempts;
}
}
}
using it like this:
$max_attempts = 3;
if(login_attempt_count() <= $max_attempts) {
echo 'login';
}else{
echo 'To many attempts';
}
or this:
$a = login_attempt_count();
Causes the rest of the page to not load. So this indicates an error.
Again if I use the code within the function, outside the function, it works as expected.
Or if there is a completely better and more secure method that I should be using I am open for suggestions. Thanks!
Ok here I am answering my own question. ;) The function works fine! However, testing it in a view causes an error? After placing the function within my model and calling the function within my controller works great. Using it like so:
class Auth extends CI_Controller{
function login(){
$max_attempts = 3;
if($this->auth_model->login_attempt_count() <= $max_attempts) {
//Do something
}else{
//Something else
}
}//End Function
}//End Class

Codeigniter passing 2 arguments to callback

After posting a form having two fields named 'id' and 'url' I have the following code:
$this->load->library('form_validation');
$this->form_validation->set_rules('id', 'id', 'trim|xss_clean');
$this->form_validation->set_rules('url', 'url|id', 'trim|xss_clean|callback_url_check');
A db query needs both fields.
The function url_check($str, $id) is called but in this case 'id' always has the value 0.
If I just do :
$this->form_validation->set_rules('url', 'url', 'trim|xss_clean|callback_url_check');
And call url_check($str) everything's working as it's is supposed to do.
The question is how do I pass two values to the url_check($str, $id)?
You can use $this->input->post directly:
function check_url() {
$url = $this->input->post('url');
$id = $this->input->post('id');
// do some database things you need to do e.g.
if ($url_check = $this->user_model->check_url($url, $id) {
return TRUE;
}
$this->form_validation->set_message('Url check is invalid');
return FALSE;
}
Just do it the right way (at least for CI 2.1+) as described in the docs:
$this->form_validation->set_rules('uri', 'URI', 'callback_check_uri['.$this->input->post('id').']');
// Later:
function check_uri($field, $id){
// your callback code here
}
This seems to work also.
$id = 1;
$this->form_validation->set_rules('username', 'Human Username', 'callback_username_check['.$id.']');
function username_check($str, $id) {
echo $id;
if ($str == 'test') {
$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
return FALSE;
}
else {
return TRUE;
}
}
If I understand form_validation correctly, each rule (set_rules) is for one field of the form and your callback will only check the one field. In your case it would seem that 'id' is out of scope. Instead, one can pass an array to the set_rules function and do the callback. I have not tried this yet. http://codeigniter.com/user_guide/libraries/form_validation.html#validationrulesasarray
Just a note on using the callback parameters as suggested in the other answers. If you are using app/config/form_validation.php to create your validation rules, the $this->input->post('parameter') syntax will not work since that object is not available in the CI Loader at the point in the execution where it reads the contents of that file. You would have to do the call in your callback routine e.g. :
public function _validate_user_signup($username, $password) {
$var = $this->input->post('password');
In this case the second parameter passed to the method would not contain the password, but $var would after the call.
I hope that's clear.
Matt
It's better to use Form Validation library to get the data that is being validated.
Not always your data will be in $_GET or $_POST (see https://www.codeigniter.com/userguide3/libraries/form_validation.html#validating-an-array-other-than-post).
The best way you can access your data inside a validation callback is this:
$this->form_validation->validation_data
"validation_data" is a public property in the CI_Form_validation class.

Resources