i want captcha code security in my signup page but when i fill the form with wrong captcha code and submit form. problem is view two page in my signup page. and fill this form again with correct captcha but they same problem again and show incorrect captcha.
CONTROLLER
public function user_signup($data = NULL){
$this->load->library('form_validation');
$this->recaptcha->recaptcha_check_answer($_SERVER['REMOTE_ADDR'],$this->input->post('recaptcha_challenge_field'),$this->input->post('recaptcha_response_field'));
$this->form_validation->set_rules('Username', 'Username', 'required|trim|is_unique[users.Username]');
$this->form_validation->set_rules('First_name', 'First Name', 'required|trim');
$this->form_validation->set_rules('Last_name', 'Last Name', 'required|trim');
$this->form_validation->set_rules('Email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_rules('Password', 'Password', 'required|trim|md5');
$this->form_validation->set_rules('Conf_password', 'Confirm Password', 'required|trim|matches[Password]');
$this->form_validation->set_message('is_unique', 'Username already exists.');
if($this->form_validation->run() && $this->recaptcha->getIsValid()){
//Call to recaptcha to get the data validation set within the class.
$form_data = array();
$form_data['Username'] = $this->input->post('Username');
$form_data['First_name'] = $this->input->post('First_name');
$form_data['Last_name'] = $this->input->post('Last_name');
$form_data['Email'] = $this->input->post('Email');
$form_data['Password'] = $this->input->post('Password');
$form_data['Conf_password'] = $this->input->post('Conf_password');
$this->load->model('User');
$this->User->sign_up($form_data);
$data['msg']= "Account Created Sucessfuly";
}else{
if(!$this->recaptcha->getIsValid()){
$this->session->set_flashdata('error','incorrect captcha');
}
}
redirect("Users/signup", $data);
}
MODEL
public function sign_up($form_data){
$this->db->insert('users', $form_data);
}
VIEW
<?php
//$val = NULL;
if(isset($results->id)){
$val = "Update";
$action = "Users/user_update/".$results->id;
}else{
$val= "Register";
$action = "Users/user_signup";
}; ?>
<?php echo form_open($action); ?>
<?php echo form_input(array('name'=>'Username', 'class'=>'input-xlarge', 'type'=>'text', 'placeholder'=>'Username', 'required' => 'required', 'value'=>$u)); ?>
<?php echo form_error('Username', '<div class="alert alert-error">', '</div>'); ?>
<?php echo form_input(array('name'=>'First_name', 'class'=>'input-xlarge', 'type'=>'text', 'placeholder'=>'First Name', 'required' => 'required', 'value'=>$f)); ?>
<?php echo form_error('First_name', '<div class="alert alert-error">', '</div>'); ?>
<?php echo form_input(array('name'=>'Last_name', 'class'=>'input-xlarge', 'type'=>'text', 'placeholder'=>'Last Name', 'required' => 'required', 'value'=>$l)); ?>
<?php echo form_error('Last_name', '<div class="alert alert-error">', '</div>'); ?>
<?php echo form_input(array('name'=>'Email', 'class'=>'input-xlarge', 'type'=>'text', 'placeholder'=>'Email', 'required' => 'required', 'value'=>$e)); ?>
<?php echo form_error('Email', '<div class="alert alert-error">', '</div>'); ?>
<?php echo form_input(array('name'=>'Password', 'class'=>'input-xlarge', 'type'=>'password', 'placeholder'=>'Password')); ?>
<?php echo form_error('Password', '<div class="alert alert-error">', '</div>'); ?>
<?php echo form_input(array('name'=>'Conf_password', 'class'=>'input-xlarge', 'type'=>'password', 'placeholder'=>'Confirm Password')); ?>
<?php echo form_error('Conf_password', '<div class="alert alert-error">', '</div>'); ?>
<?php if(!isset($results->id)){echo $recaptcha_html;} ?>
<?php if ($this->session->flashdata('error') !== FALSE) { echo '<div class="alert alert-error">'.$this->session->flashdata('error').'</div>'; } ?>
<div class="login-actions">
<span><input type="checkbox"> <span class="remember">I have read & agree</span></span>
<span><?php echo form_submit(array('value'=>$val, 'class'=>'btn btn-large btn-warning pull-right', 'type'=>'submit')); ?></span>
</div>
<?php echo form_close(); ?>
Put this code after the if(!$this->recaptcha->getIsValid()){ and echo messages...
if($this->input->post('recaptcha_response_field') =="" ){
$this->session->set_flashdata('error','fill up this code');
}else{
$this->session->set_flashdata('error','incorrect captcha');
}
if your show the message "account create successfully". use the set flash message
read this link properly
http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
How to display error messages in CodeIgniter
First you create a signup function in Controller and and view default signup page. as like
$this->load->view('User/User_sign_up');
Related
I am trying to save a schedule of working days on a table multiple rows at once...
I am doing this wrong, still giving me errors.
I have seen tabular inputs from others, but can't get this right.
I really need some other eyes on it.
I have a similar problem as https://phppedia.com/en/knowledge-base/32481399/yii2-insert-multiple-records-of-a-same-table
model:
public
function rules()
{
return [
['store_id', 'string'],
['day', 'string'],
['start_hour', 'string'],
['end_hour', 'string'],
['holiday','boolean'],
];
}
controler:
public function actionCreate()
{
$count = count(Yii::$app->request->post('Openhour', []));
$model = [new Openhours()];
for ($i = 1; $i < $count; $i++) {
$model[] = new Openhours();
}
if ($model->loadMultiple($model, Yii::$app->request->post())) {
foreach ($model as $model) {
$model->save(false);
}
}
return $this->render('create', ['model' => $model,]);
}
_form
<?php $stores = Stores::getAll() ?>
<?php foreach ($stores as $store): ?>
<?php if ($store->id !== 0): ?>
<?php $listData[$store->id] = [$store->id => $store->title]; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php $form = ActiveForm::begin(['enableAjaxValidation' => true, 'options' => ['class' => 'model-form'],]); ?>
<?php foreach ($model as $index => $model): ?>
<div class="row row-cols-3">
<div class="col">
<h5>
<?php if ($store->title === Yii::$app->user->identity->username) : ?>
<?= $form->field($model, '[$index]store_id')->hiddenInput(['value' => $store->title])->label($store->title) ?>
<?php else: ?>
<?= $form->field($model, '[$index]store_id')->dropDownList($listData, ['prompt' => 'Select...']); ?>
<?php endif; ?>
</h5>
</div>
<div class="col">Opening</div>
<div class="col">Close</div>
</div>
<?php $days = Storedays::getAll() ?>
<?php foreach ($days as $day): ?>
<div class="row row-cols-3">
<div class="col">
<div class="row justify-content-between">
<div class="col">
<h6><?= $day->name ?></h6>
<?= $form->field($model, '[$index]day')->hiddenInput(['value' => $day->name, 'id' => 'day' . $day->id])->label(false) ?>
</div>
<div class="col">
<?= $form->field($model, '[$index]holiday')->checkbox(['selected' => $model->holiday, 'id' => 'holiday' . $day->id])->label(false) ?>
</div>
</div>
</div>
<div class="col">
<?= $form->field($model, '[$index]start_hour')->Input('text')->widget(TimePicker::class, ['options' => ['id' => 'start_hour' . $day->id], 'pluginOptions' => ['maxHours' => '8', 'template' => 'dropdown', 'showSeconds' => false, 'showMeridian' => false, 'minuteStep' => 15,]])->label(false) ?>
</div>
<div class="col">
<?= $form->field($model, '[$index]end_hour')->Input('text')->widget(TimePicker::class, ['options' => ['id' => 'end_hour' . $day->id], 'pluginOptions' => ['maxHours' => '8', 'template' => 'dropdown', 'showSeconds' => false, 'showMeridian' => false, 'minuteStep' => 15,]])->label(false) ?>
</div>
</div>
<?php endforeach; ?>
<?php if (IS_ROOT) : ?><?= $form->field($model, '[$index]slug') ?><?php endif; ?>
<?= Html::submitButton('save', ['class' => 'btn btn-primary']) ?>
<hr class="mb-3">
<?php endforeach; ?>
<?php ActiveForm::end(); ?>
First you have an array of objects and you must use plural name instead.
So model become models.
Second you must use functions for loading multiple models from request Model::loadMultiple($models) and for validate multiple models Model::validateMultiple($models).
Now in your controller:
public function actionCreate()
{
$count = count(Yii::$app->request->post('Openhour', []));
$models = [new Openhours()];
for ($i = 1; $i < $count; $i++) {
$models[] = new Openhours();
}
if (Model::loadMultiple($models, Yii::$app->request->post()) && Model::validateMultiple($models)) {
foreach ($models as $model) {
$model->save(false);
}
}
return $this->render('create', ['models' => $models]);
}
Now in your _form.php change the line:
<?php foreach ($model as $index => $model): ?>
To:
<?php foreach ($models as $index => $model): ?>
I've success create modal form, but I can't submit it with ajax. Everytime i click submit button it not stay in the index page but instead show echo 1 from my Controller.
My controller
public function actionCreate()
{
$model = new Testing();
if ($model->load(Yii::$app->request->post())) {
if($model->save()){
echo 1;
}else{
echo 0;
}
}else{
return $this->renderAjax('create', [
'model' => $model,
]);
}
}
My form
<div class="testing-form">
<?php $form = ActiveForm::begin(['id' => $model->formName()]); ?>
<?= $form->field($model, 'test')->textInput(['maxlength' => true]) ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
<?php
$this->registerJS("
$('form#{$model->formName()}'.on('beforeSubmit', function(e)
{
var \$form = $(this);
$.post(
\$form.attr('action'),
\$form.serialize()
)
.done(function(result) {
if(result == 1)
{
$(\$form).trigger('reset');
$.pjax.reload({container:'#issuehrGrid'});
}else
{
$('#message').html(result.message);
}
}).fail(function()
{
console.log('server error');
})
return false;
});"
);
?>
My view
<div class="report">
<h3>HR Issues</h3>
<?= Html::a('<span class="glyphicon glyphicon-plus"></span> Add', ['create'], ['class' => 'btn btn-success modalButton']) ?>
<?php Pjax::begin(['id'=>'issuehrGrid']); ?>
<table class="table table-bordered table-hover">
<tbody>
<?php foreach ($dataProvider->models as $hr) {
?>
<tr>
<td><?=$hr->test;?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php Pjax::end(); ?>
</div>
Please help me with this.
Thank you!
What I suspect is that it would be showing you the blank page with 1 displayed in it is because after the first submission your Pjax grid is reloaded via the statement
$.pjax.reload({container:'#issuehrGrid'});
and once reloaded your beforeSubmit event is not bound again to the form as the form Html is reloaded again and when you submit the form second time it just makes a simple form submit and displays the blank page with 1, you need to use the pjax:complete event for the pjax, so that one the grid is reloaded your script binds the beforeSubmit event to the form again.
Also, you have a syntax error in your script which should be fixed. Change your script to the following
<?php
$formName = $model->formName();
$js = <<<JS
$(document).on("ready pjax:complete",function(){
$('form#{$formName}').on('beforeSubmit', function(e){
var \$form = $(this);
$.post(
\$form.attr('action'),
\$form.serialize()
).done(function(result) {
if(result == 1)
{
$(\$form).trigger('reset');
$.pjax.reload({container:'#issuehrGrid'});
}else
{
$('#message').html(result.message);
}
}).fail(function()
{
console.log('server error');
})
return false;
});
});
JS;
$this->registerJS(
$js,
\yii\web\View::POS_READY
);
?>
UPDATE
and dont forget to change the controller action code and change the echo 1 to return 1 and echo 0 to return 0
how to check a submit button value in controller (Yii2). I am working with multiple submit button.
I tried simple php code. but it is not working.
if(isset($_POST['next']) && $_POST['next']=='gotocartfive')
code in view is :
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'status')->checkbox(); ?>
</div>
<div class="form-group">
<?php echo Html::submitButton('NEXT',array('value'=>'gotocartfive','name' => 'next','id'=>'next_summary','class'=>'btn btn-primary pull-right')); ?>
<?php echo Html::submitButton('PREVIOUS',array('value'=>'previous_four','name' => 'cartfour','class'=>'btn btn-primary pull-left')); ?>
</div>
<?php ActiveForm::end(); ?>
<?= Html::submitButton('Submit 1', ['name' => 'action', 'value' => 'submit_1']) ?>
<?= Html::submitButton('Submit 2', ['name' => 'action', 'value' => 'submit_2']) ?>
PHP
If (\Yii::$app->request->isPost) {
switch (\Yii::$app->request->post('action')) {
case 'submit_1':
case 'submit_2':
}
}
When you submit form by pressing enter (without click any submit button), submit_1 will be default value.
You can try following code.
Code in view file.
<?= Html::submitButton(Yii::t('app', '<i class="fa fa-times"></i> Remove'), ['class' => 'btn red', 'name' => 'submit', 'value' => '0']) ?>
<?= Html::submitButton(Yii::t('app', '<i class="fa fa-check"></i> Save'), ['class' => 'btn blue', 'name' => 'submit', 'value' => '1']) ?>
Code in controller action
if (Yii::$app->request->post()) {
if (Yii::$app->request->post('submit') == 0) {
//Code for value 0
}
if (Yii::$app->request->post('submit') == 1) {
//Code for value 1
}
}
Please let me know if you've any questions.
Try This :
View File
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'status')->checkbox(); ?>
<div class="form-group">
<?= Html::submitButton('NEXT',[ 'name'=>'submit', 'value' => 'next', 'class' => 'btn btn-primary pull-right']) ?>
<?= Html::submitButton('PREVIOUS',[ 'name'=>'submit', 'value' => 'previous', 'class' => 'btn btn-primary pull-right') ?>
</div>
<?php ActiveForm::end(); ?>
Controller File
public function actionYourControllerName()
{
if(isset($_POST['submit') && $_POST['submit']=='next')
{
// your code
}
else if(isset($_POST['submit']) && $_POST['submit']=='previous')
{
// your code
}
}
Also you can add this little js snippet to your project and bind it to beforeSubmit event in yii.activeForm.js like this:
(function ($) {
var formId = !!yiiconfig.viewPolicyParams && yiiconfig.viewPolicyParams.formId && yiiconfig.viewPolicyParams.formId,
$form = formId && $("#" + formId);
/**
* Updates hidden field that represents clicked submit button.
* #param event event object triggered
*/
function updateHiddenButton (event) {
var $buttons = $form.find(':submit');
$buttons.length && $buttons.each(function (i,b) {
var $hiddenButton = $('input[type="hidden"][name="' + $(b).attr('name') + '"]', $form);
$hiddenButton.length && $hiddenButton.remove();
});
};
$form && $form.bind('beforeSubmit.' + formId, updateHiddenButton);
} (jQuery));
This code removes all hidden inputs which are being created by yii.activeForm before submitting.
Then after this inputs will be recreated by yii.activeForm.
hope this helps
I have a list of bookings and I want to delete them using multiple checkboxes. Here is my code:
foreach ($bookings as $booking): ?>
<tr>
<td><?php echo h($booking['Booking']['first_name']); ?> </td>
<td><?php echo h($booking['Booking']['surname']); ?> </td>
<td><?php echo h($booking['Booking']['created']); ?> </td>
<td><?php echo $this->Form-> checkbox('Bookings.ID.['.$booking['Booking']['ID'].']',
array('value' => $booking['Booking']['ID']));?></td>
</tr>
<?php endforeach; ?>
and in my controller I use this function to delete the selected bookings:
public function deletebooking(){
$bookings = $this->Booking->find('all');
$this->set('bookings',$bookings);
if(!empty($this->data)){
foreach($this->data[Bookings] as $key => $value){
if($value != 0){
$this->Booking->delete($value);
$this->redirect(array('action' => 'index'));
}
}
}
}
Can anyone tell me why it is not working?
Change your input field to the following:
foreach ($bookings as $booking): ?>
<td>
<?php echo $this->Form->checkbox('bookingids',
array(
'value' => $booking['Booking']['ID'],
'name' => 'data[Booking][bookingids][]',
));?>
</td>
Notice the empty [] in the name value. This will create a new index of that array. In your controller you would access it like this:
if(!empty($this->data)) :
foreach($this->data['Bookings']['bookingids'] as $key => $value):
$data = array();
$data['Booking']['id'] = $value;
$this->Booking->delete($data);
endforeach;
$this->redirect(array('action' => 'index'));
endif;
I'm going nuts trying to figure out why I'm having such a difficult time getting WordPress to only show posts newer than 30-days and I could really use a second set of eyes. I'm using the following code but nothing is showing up on my site.
<?php
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$the_query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
?>
<?php if ($the_query->have_posts()) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2 class="title"><?php the_title(); ?></h2>
<div class="meta">
<span class="date">
<strong><?php the_time('d'); ?></strong>
<strong><?php the_time('M'); ?></strong>
</span>
</div>
<div class="entry">
<?php if ( function_exists( 'get_the_image' ) ) {
get_the_image( array( 'custom_key' => array( 'post_thumbnail' ), 'default_size' => 'full', 'image_class' => 'alignleft', 'width' => '170', 'height' => '155' ) ); }
?>
<?php the_content('Read More'); ?>
</div>
</div>
<?php endwhile; ?>
<div class="navigation">
<?php
include('includes/wp-pagenavi.php');
if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
?>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php get_search_form(); ?>
<?php endif; ?>
<?php
function filter_where($where = '') {
//posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>
This works
The way you're doing looks fine. Try putting posts_per_page => -1 as the argument.
The following works for me:
function filter_where( $where = '' ) {
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$args = array(
'posts_per_page' => -1,
);
$the_query = new WP_Query($args);
remove_filter( 'posts_where', 'filter_where' );
while ($the_query->have_posts()) {
$the_query->the_post();
// do stuff
}
Hope it helps.
I know this is super old but, you can do this with WP_Query now.
$args = array(
'post_type' => 'post', // or whatever post type you want
'posts_per_page' => -1, // get all posts that apply, i.e. no limit
'date_query' => array(
array(
'after' => '-30 days',
'column' => 'post_date',
),
),
);