I'm facing one problem in yii2 radio buttons while using with yii2 dynamic forms package. yii2 is not generating the radio button with id attribute. Due to missing of id attributes in radio buttons, yii2 dynamic forms radio buttons values are always setting as 1. So Please help me how to overcome this problem.
Edited
<?= $form->field($client_allow_acces, "[$i]access_type")->radioList([1 => 'Allow access', 2 => 'Can\'t allow access'], ['uncheckValue' => null, 'id'=>'custom_id_value']) ?>
I'm getting the html output below
<div class="form-group field-clientallowaccess-0-access_type required">
<label class="control-label" for="custom_id_value">Access Type</label>
<input type="hidden" name="ClientAllowAccess[0][access_type]" value="">
<div id="custom_id_value">
<label><input type="radio" name="ClientAllowAccess[0][access_type]" value="1"> Allow access</label>
<label><input type="radio" name="ClientAllowAccess[0][access_type]" value="2"> Can't allow access</label>
</div>
<div class="help-block"></div>
</div>
My custom id value is coming in div element. but I need it in radio button itself.
Thanks in advance.
You need to use following code because of your given id is assigned to div in your code. that is on <div id="custom_id_value">.
<?= $form->field($client_allow_acces, '[$i]access_type')->radioList([1 => 'Allow access', 2 => 'Can\'t allow access'],[ 'item' => function($index, $label, $name, $checked, $value) {
$return = '<label class="modal-radio">';
$return .= '<input type="radio" name="' . $name . '" value="' . $value . '" id="custom_id_value_'.$index.'" >';
$return .= '<span> ' . ucwords($label) . '</span>';
$return .= '</label>';
return $return;
}]); ?>
Related
In Cakephp3.2 I cant place spaces between radio buttons. I am sure there is an easy way.
Here is an example of what I am talking about from my test server, check the radio button area in Grammar section about 2/3rds down page.
http://andrewt.com.au/crm/students/studentassessment-eng7/35730
My code
<?php $opt1q1=' I wanted to watch TV, but the electricity went out unexpectedly.';
?>
<br>
<h3><?php echo 'Task 3: Grammar' ?> </h3>
<div class='alert alert-info'><?php echo '1 )'.$opt1q1; ?> </div>
<?php echo $this->Form->radio( 'a16', ['text'=>'a) noun', 'b) verb ','c) preposition ', 'd) pronoun ' ],
['default' =>$assessment['a16'] ,'style' => 'padding-left:5px;' ] );
The docs didnt help either and its maybe because I have an older version cakephp?
https://book.cakephp.org/3/en/views/helpers/form.html#creating-radio-buttons
Didnt work
space between the radio button and label Cakephp
i think what you are looking for are link breaks?
How to add a line break within echo in PHP?
https://www.w3schools.com/php/func_string_nl2br.asp
<input type="hidden" name="a16" value="" class="form-control"><label for="a16-0">
<input type="radio" name="a16" value="0" <?php echo $assessment['a16']==0 ?' checked=checked':'' ?> >a) noun</label><label for="a16-0">
<input type="radio" name="a16" value="1" <?php echo $assessment['a16']==1 ?' checked=checked':'' ?> >b) verb</label><label for="a16-1">
<input type="radio" name="a16" value="2" <?php echo $assessment['a16']==2 ?' checked=checked':'' ?> >c)preposition</label><label for="a16-2">
<input type="radio" name="a16" value="3" <?php echo $assessment['a16']==3 ?' checked=checked':'' ?> >d)pronoun </label><label for="a16-3">
<style>
label{
margin-left: 16px;
}
</style>
I have a form with multiple reference for a class(I mean table) in single form like add more details. Main goal of the task is a company have more than one users. each user have some access restrictions. so while adding company we will add multiple company users. company users form have that access radio button. here radio button validation is not working. same issue for checkbox and dropdown also.sample code given below. Please help.
AccessController.php
class AccessController extends Controller
{
public function actionCreate() {
$formDetails = Yii::$app->request->post('ClientAllowAccess', []);
if(!empty($formDetails)){
foreach ($formDetails as $i => $formDetail) {
$modelDetail = new ClientAllowAccess(['scenario' => ClientAllowAccess::SCENARIO_BATCH_UPDATE]);
$modelDetail->setAttributes($formDetail);
$modelClientAccess[] = $modelDetail;
}
} else {
$modelClientAccess[] = new ClientAllowAccess(['scenario' => ClientAllowAccess::SCENARIO_BATCH_UPDATE]);
}
return $this->render('create', [
'client_allow_access_multiple' => $modelClientAccess
]);
}
}
create.php
....
<?php foreach($client_allow_access_multiple as $i => $client_allow_acces): ?>
<?= $form->field($client_allow_access, '[$i]access_type')->radioList([1 => 'Allow access', 2 => 'Can\'t allow access'],[ 'item' => function($index, $label, $name, $checked, $value) {
$return = '<label class="modal-radio">';
$return .= '<input type="radio" name="' . $name . '" value="' . $value . '" id="custom_id_value_'.$index.'" >';
$return .= '<span> ' . ucwords($label) . '</span>';
$return .= '</label>';
return $return;
}]); ?>
<?php endforeach; ?>
....
rendered view
<div class="form-group field-clientcontactdetails-0-gender has-success">
<label class="control-label" for="clientcontactdetails-0-gender">Gender</label>
<select id="clientcontactdetails-0-gender" class="form-control" name="ClientContactDetails[0][gender]">
<option value="">---</option>
<option value="1">Male</option>
<option value="2">Female</option>
<option value="3">Others</option>
</select>
<div class="help-block"></div>
</div>
<div class="form-group field-clientallowaccess-0-access_type required">
<label class="control-label" for="clientallowaccess-0-access_type">Access Type</label>
<input type="hidden" name="ClientAllowAccess[0][access_type]" value=""><div id="clientallowaccess-0-access_type"><label><input type="radio" name="ClientAllowAccess[0][access_type]" value="1"> Allow access</label>
<label><input type="radio" name="ClientAllowAccess[0][access_type]" value="2"> Can't allow access</label></div>
<div class="help-block"></div>
</div>
I have two set of radio buttons in html form button and button1. I am using below code to
1.keep the default value checked (question1 for first set and answer2 for next set)
2.keep user radio button selection after the form submit
<div id="button_set1">
<input onClick="show_seq_lunid();" type="radio" name="button" value="Yes" <?php if(isset($_POST['button']) && $_POST['button'] == 'Yes') echo ' checked="checked"';?> checked /><label>question1</label>
<input onClick="show_list_lunid();" type="radio" name="button" value="No" <?php if(isset($_POST['button']) && $_POST['button'] == 'No') echo ' checked="checked"';?> /><label>answer1</label>
</div>
<div id="button_set2">
<input onClick="os_hpux();" type="radio" name="button1" value="Yes" <?php if(isset($_POST['button1']) && $_POST['button1'] == 'Yes') echo ' checked="checked"';?> /><label>question2</label>
<input onClick="os_others();" type="radio" name="button1" value="No" <?php if(isset($_POST['button1']) && $_POST['button1'] == 'No') echo ' checked="checked"';?> checked /><label>answer2</label>
</div>
Here if i use below code, the second radio button button1 is not sticking on to the user selection after form submit, it changing back to its default checked state.ie answer2. But the first set of radio buttons work fine.
If I remove the default checked option from the code, both radio buttons working fine after form submit. How can I keep the radio button checked after form submit while using checked default option for radios
So, the problem is you're setting the checked value twice upon form submission, resulting in selecting either the default value (from initial form state) or the value that has been submitted.
For this to work correctly, you'd need always use PHP to append the checked value to your radio elements, like this:
<div id="button_set1">
<input onClick="show_seq_lunid();" type="radio" name="button" value="Yes" <?php if(!isset($_POST['button']) || (isset($_POST['button']) && $_POST['button'] == 'Yes')) echo ' checked="checked"'?> /><label>question1</label>
<input onClick="show_list_lunid();" type="radio" name="button" value="No" <?php if(isset($_POST['button']) && $_POST['button'] == 'No') echo ' checked="checked"';?> /><label>answer1</label>
</div>
<div id="button_set2">
<input onClick="os_hpux();" type="radio" name="button1" value="Yes" <?php if(isset($_POST['button1']) && $_POST['button1'] == 'Yes') echo ' checked="checked"';?> /><label>question2</label>
<input onClick="os_others();" type="radio" name="button1" value="No" <?php if(!isset($_POST['button1']) || (isset($_POST['button1']) && $_POST['button1'] == 'No')) echo ' checked="checked"';?> /><label>answer2</label>
</div>
Here's a working preview: http://codepad.viper-7.com/rbInpX
Also, please note that you're using inline JavaScript notation which is normally discouraged to keep dynamic JS content separate and more manageable ;-)
I had a similar problem recently, but had a lot more radio buttons to deal with, so I thought I'd abstract away the value checking functionality into a method that also creates the radio button itself, therefore minimising repetitive value-checking code. I've reworked mine to suit your variable names:
function createRadioOption($name, $value, $onClickMethodName, $labelText) {
$checked = '';
if ((isset($_POST[$name]) && $_POST[$name] == $value)) {
$checked = ' checked="checked"';
}
echo('<input onClick="'. $onClickMethodName .'();" type="radio" name="'. $name .'" value="'. $value .'"'. $checked .' /><label>'. $labelText .'</label>');
}
<div id="button_set1">
<?php createRadioOption('button', 'Yes', 'show_seq_lunid', 'question1'); ?>
<?php createRadioOption('button', 'No', 'show_list_lunid', 'question1'); ?>
</div>
<div id="button_set2">
<?php createRadioOption('button1', 'Yes', 'os_hpux', 'question2'); ?>
<?php createRadioOption('button1', 'No', 'os_others', 'question2'); ?>
</div>
I'm trying to make a HTML form post the input values to a custom table in the WordPress database. I've managed to get something to show up in a new row, but almost all of my values return N; instead of the value from the form.
Here's the code I have in my page template:
<?php
global $wpdb;
global $current_user;
$userID = $current_user->ID;
$brand = serialize($_POST["brand"]);
$url = serialize($_POST["url"]);
$sector = serialize($_POST["sector"]);
$keywords = serialize($_POST["keywords"]);
if (
'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'updateSearch' ) {
$ufDataUpdate = $wpdb->insert( 'wp_wct3', array(
'date' => current_time('mysql'),
'userid' => $userID,
'brand' => $brand,
'url' => $url,
'sector' => $sector,
'keywords' => $keywords ) );
}
?>
<form method="post">
<label for="brand">Brand/Product Name: </label>
<input type="text" id="brand" placeholder="eg: Spidr" class="clearfix" required />
<label for="website">Website address: </label>
<input type="url" id="url" placeholder="eg: www.spidr.co.uk" class="clearfix" required />
<label for="sector">Market sector: </label>
<input type="text" id="sector" placeholder="eg: Internet Marketing Tools" class="clearfix" required />
<label for="keyword">Keywords/Phrases:<br><span class="orange">(comma separated)</span></label>
<textarea cols="0" rows="8" class="light" id="keywords" required></textarea>
<input type="submit" id="submit" name="submit" class="button-65 mobile-button" value="release the spiders!">
<?php wp_nonce_field( 'updateSearch' ); ?>
<input name="action" type="hidden" id="action" value="updateSearch" />
</form>
Where wp_wct3 is the database name and each item in the array is the name of each column in that table.
I'm not sure if my issue lies in this code, or in the set-up of the database itself. I've used the Wordpress custom tables plugin to make the new table. The brand, url and sector simply use the text definition, while the keywords use enum('0','1').
Anyone have any ideas why the values aren't returning and I'm just getting N; ?
this is my custom-form template.. it works for me
<?php
/**
Template Name: Custom-Form
* The template for displaying all pages.
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages
* and that other 'pages' on your WordPress site will use a
* different template.
*
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php
if (!empty($_POST)) {
global $wpdb;
$table = wp_achord;
$data = array(
'name' => $_POST['yourname'],
'chord' => $_POST['chord']
);
$format = array(
'%s',
'%s'
);
$success=$wpdb->insert( $table, $data, $format );
if($success){
echo 'data has been save' ;
}
}
else {
?>
<form method="post">
<input type="text" name="yourname">
<textarea name="chord"></textarea>
<input type="submit">
</form>
<?php } ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
Figured out the main issue. I'd missed out the name attribute from the form itself, so my PHP wasn't picking up the field values!
I have a client that needs a custom registration form.
I need to make a custom design on this page
I need to add custom fields like First Name, Company, Phone, etc.
Someone can help me with this?
A better place to ask WordPress questions is probably on WordPress Answers. Anyhoo, if you want to solve this without plugins, you need three things:
A custom WordPress theme
A Page Template
A WordPress Page that uses the Page Template
When you have these three parts in place, you can do the following in your Page Template:
<?php
/*
Template Name: Registration
*/
global $current_user;
wp_get_current_user();
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$company = $_POST['company'];
if (($firstname != '') && ($lastname != '') && ($company != '')) {
// TODO: Do more rigorous validation on the submitted data
// TODO: Generate a better login (or ask the user for it)
$login = $firstname . $lastname;
// TODO: Generate a better password (or ask the user for it)
$password = '123';
// TODO: Ask the user for an e-mail address
$email = 'test#example.com';
// Create the WordPress User object with the basic required information
$user_id = wp_create_user($login, $password, $email);
if (!$user_id || is_wp_error($user_id)) {
// TODO: Display an error message and don't proceed.
}
$userinfo = array(
'ID' => $user_id,
'first_name' => $firstname,
'last_name' => $lastname,
);
// Update the WordPress User object with first and last name.
wp_update_user($userinfo);
// Add the company as user metadata
update_usermeta($user_id, 'company', $company);
}
if (is_user_logged_in()) : ?>
<p>You're already logged in and have no need to create a user profile.</p>
<?php else : while (have_posts()) : the_post(); ?>
<div id="page-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="content">
<?php the_content() ?>
</div>
<form action="<?php echo $_SERVER['REQUEST_URI'] ?>" method="post">
<div class="firstname">
<label for="firstname">First name:</label>
<input name="firstname"
id="firstname"
value="<?php echo esc_attr($firstname) ?>">
</div>
<div class="lastname">
<label for="lastname">Last name:</label>
<input name="lastname"
id="lastname"
value="<?php echo esc_attr($lastname) ?>">
</div>
<div class="company">
<label for="company">Company:</label>
<input name="company"
id="company"
value="<?php echo esc_attr($company) ?>">
</div>
</form>
</div>
<?php endwhile; endif; ?>
Now, when you want to retrieve the stuff you've stored, you need to know whether the information is within the User object itself or in metadata. To retrieve the first and last name (of a logged-in user):
global $current_user;
$firstname = $current_user->first_name;
$lastname = $current_user->last_name;
To retrieve the company name (of a logged-in user):
global $current_user;
$company = get_usermeta($current_user->id, 'company');
That's the basic gist of it. There's still a lot of stuff missing here, like validation, error message output, the handling of errors occurring within the WordPress API, etc. There's also some important TODO's that you have to take care of before the code will even work. The code should probably also be split into several files, but I hope this is enough to get you started.
An advantage of using a custom registration form is that modifying the code according to the user's needs becomes easy. For a custom submit form you can make use of existing hooks in Wordpress like template_redirect and then map that hook to some function which will do the post-processing of the form, like validation and submitting data to the site's database. You can refer to an in-depth article here.
<div class="employee">
<input type="hidden" name="show_msg">
<form name="customer_details" method="POST" required="required" class="input-hidden">
Your Name: <input type="text" id="name" name="customer_name">
Your Email: <input type="text" id="email" name="customer_email">
Company: <input type="text" id="company" name="company">
Sex: <input type="radio" name="customer_sex" value="male">Male <input type="radio" name="customer_sex" value="female">Female
<textarea id="post" name="experience" placeholder="Write something.." style="height:400px;width:100%"></textarea>
<input type="submit" value="Submit">
<!--?php wp_nonce_field( 'wpshout-frontend-post','form-submit' ); ?-->
</form></div>
PHP function
function wpshout_frontend_post() {
wpshout_save_post_if_submitted();
}
add_action('template_redirect','wpshout_frontend_post', 2);
A custom WordPress registration form has two major advantages over the standard form.
The first is the integration with the overall look and feel of the website theme. Standard forms often don’t work well with custom themes and there is always a chance that the custom CSS files do not render well with the form. A custom form, on the other hand, can be easily set up to work with custom CSS.
The second and more popular reason of using a custom registration form is the option of custom fields that are not included on the standard form. A small custom registration form speeds up the process and collects all the necessary data from a neat interface.
function wordpress_custom_registration_form( $first_name, $last_name, $username, $password, $email) {
global $username, $password, $email, $first_name, $last_name;
echo '
<form action="' . $_SERVER['REQUEST_URI'] . '" method="post">
First Name :
<input type="text" name="fname" value="' . ( isset( $_POST['fname']) ? $first_name : null ) . '">
Last Name:
<input type="text" name="lname" value="' . ( isset( $_POST['lname']) ? $last_name : null ) . '">
Username <strong>*</strong>
<input type="text" name="username" value="' . ( isset( $_POST['username'] ) ? $username : null ) . '">
Password <strong>*</strong>
<input type="password" name="password" value="' . ( isset( $_POST['password'] ) ? $password : null ) . '">
Email: <strong>*</strong>
<input type="text" name="email" value="' . ( isset( $_POST['email']) ? $email : null ) . '">
<input type="submit" name="submit" value="Register"/>
</form>
';
}
This form can be inserted anywhere by using the shortcode [wp_registration_form]. Here is the code snippet for setting up the shortcode:
function wp_custom_shortcode_registration() {
ob_start();
wordpress_custom_registration_form_function();
return ob_get_clean();
}
I hope that by now you have a fair idea of creating a WordPress custom Registration form.Still any confusion kindly check Build Custom WordPress Registration Forms