Drupal - Webform element theming - php

Another question about Drupal webforms --
The form itself is built in by /includes/form.inc's
function theme_form_element($element, $value)
and adds a <label> element to the $output. I want to remove that label only for one webform, so I have to override the function.
How can I override it for only one webform, while leaving it the same in all others?
E.g.
if ($block == 'contact'):
// only output <input> form element stored in $value
function mytheme_html_form_element($element, $value) {
$t = get_t();
$output .= " $value\n";
return $output;
}
endif;
Is this possible, and what goes in the if condition?

If you're just looking to remove the label, you can also use hook_form_alter(), and check that $form_id is equal to the webform in question. The id will be of the form: webform_client_form_N where N is the node ID of the webform.
Once you're operating on the proper form, you can unset the label using, for example, code like this:
unset($form['submitted']['first_name']['#title']);
Which would unset the label for a field called first_name.

i did have to do a hook_form_alter, but the label itself was in the ['submitted'] element.
here is the code
if($form_id == 'webform_client_form_18') {
$form['submitted']['#children'] = '
<input
type="text"
maxlength="128"
name="submitted[email]"
id="edit-submitted-email"
value="' . $form['submitted']['email']['#default_value']. '"
class="form-text required"
/>
';
}
in a different form, removing the #title worked (+1 for you!), but this was a different case.

I wouldn't unset form element titles. You could get unexpected results when your form gets rendered by the theme engine.
You can do it several ways:
Theme each element or the whole form with with '#theme' => 'my_callback'.
You can also create your own form element using hook_elements that uses a corresponding theme hook.
See:
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html
http://api.drupal.org/api/function/hook_elements/6

Related

How to add static html markup to a CiviCRM form

I have CiviCRM 4.4.6 + Drupal 7 and i alter one of CiviCRM's forms.
Inside hook_civicrm_buildForm(), i try to:
form->addElement('html', 'statichtml', '<div>aa</div>');
$template =& CRM_Core_Smarty::singleton();
$bhfe = $template->get_template_vars('beginHookFormElements');
if (!$bhfe) {
$bhfe = array();
}
$bhfe[] = 'statichtml';
$form->assign('beginHookFormElements', $bhfe);
If i use it with 'text' element type, it works correctly. This way nothing is rendered, but an empty additional tr is added.
How to use this type of element correctly?
http://pear.php.net/manual/hu/package.html.html-quickform.intro-elements.php
Here is the explanation.
The element type should be static, not html and the above code starts to work.

Expand all input fields to size of value or remove all input fields with javascript.

I have an extremely large form with around 200+ input fields, once the form has been completed you can view it in the admin section (copied code from main section).
I really don't want to have manually go through and delete every input field. Is there a way to expand all input fields to the size of the value or input after page load or remove then input tags all together leaving just the value?
If you just want to change all input fields to native text:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function()
{
$("input").each( function(index)
{
$(this).replaceWith("<p>"+$(this).val()+"</p>");
}
);
});
</script>
This will then remove all input tags all together leaving just the value (with a < p> tag around it )
fiddle:
http://jsfiddle.net/wKvPg/
if you just pasted the code it means that your code is mostly html so in that case i would use a text editor like notepad++ it has a find and replace function which supports regex you can use that
Download Notepad++
On the future i recommend you avoid copy/pasting code and implement them as functions.
You can have hundred or more inputs with same name and different values:
<input type="text" name="values[]" value="val1" />
<input type="text" name="values[]" value="val2" />
Just loop it all:
foreach ($_POST['values'] as $key => $val)
{
print "Key = " . $key . " Value = " . $val . "<br />";
// another actions
}
Or access directly:
$val1 = $_POST['values'][0]; // 'val1'
$val2 = $_POST['values'][1]; // 'val2'
Hope that helps.

Dynamic forms and PHP

Ive started working on a dynamic form script that allows a user to add form elements via Jquery, which is then in turn submitted to a PHP script.
I'm just after some feedback on ways to achieve this. At the moment I have the following:
When a user adds a form element the element is added with the following name array:
<textarea name="element[text][123]">
<input type="text" name="element[input][456]" />
As I need to know the type of form element that was submitted I am using a multidimensional array called 'element[][]' where the first level of the array is the type of element and the second element of the array is a unique ID and the value.
When I var_dump() This after submission PHP outputs:
array
text => array
123 => string 'The textarea value'
input => array
456 => string 'The input field value'
Im working on the PHP side of the script now and just wondering if there is a better way to do this.
Any thoughts?
UPDATE
I have to change the way that Im doing this as the array keynames are not unique.
If the user adds two textareas
<textarea name="element[text][123]">
<textarea name="element[text][456]">
When the user adds a form element, the element can be dragged so the positioning can be changed after the element was created. This allows a user to add an element but then move it to where they want it to appear.
PHP handles this ordering fine and accepts the array in the order that the form is submitted, however as mentioned above if the key names are the same then the order will be broken.
On the PHP side I need to know
the type of form field
the value of the form field
the unique ID, which is just a timestamp, of the form field
I think I might need to do what Cole mentioned, assigning the names as:
element[text_123]
I can then explode the keyname on '_' to determine the type and the identifier.
UPDATE
I took the script Jack posted and slightly modified it
$vars = $_POST['element'];
foreach ($vars as $id => $vals)
{
// $vars[id] outputs the ID number
// $vars[vals] is the array containing the type and value
echo "This fields ID is $id. ";
foreach($vals as $key => $value)
{
echo "Type was: $key and the value was: $value <br />";
}
}
A quick test of this outputted
This fields ID is 1338261825063. Type was: heading and the value was: xzczxczxczxczxczxc
This fields ID is 1338261822312. Type was: heading and the value was: asdasdasdasdad
From this I know the identifier and the array that it belongs to, the type and the value, but I also know the order that the data was submitted.
From that I can wrap my data in markup, perform any additional operations and then insert the data into the database.
Looks okay; you could also consider something like this (it introduces more fields though, so you must really think the benefit is worth it):
<input type="hidden" name="element[123][type]" value="text" />
<input type="hidden" name="element[456][type]" value="input" />
<textarea name="element[123][value]">
<input type="text" name="element[456][value]" />
Then you can do this:
foreach ($_POST['element'] as $name => $info) {
// $info['type'] is 'text' or 'input'
// $info['value'] is the user input
}

Extract dynamically created form data

I've just started using jQuery. One thing I've been using it for is adding rows to a table that is part of a form.
When I add a new row, I give all the form elements names like 'name_' + rowNumber. I increment rowNumber each time I add a row.
I also usually have a Remove Row Button. Even when a row is removed, the rowNumber count stays the same to keep from repeating element names.
When the form is submitted, I set a hidden element to equal the rowNumber value from jQuery. Then in PHP, I count from 1 to the rowNumber value. Then for each value, I perform an isset($_REQUEST['name'_ . index]). This is how I extract the form elements that remained after deleting rows in jQuery.
Does anyone here have a better technique for accounting for deleted rows?
For some of our simpler tables, we use a field name such as 'name[]', though for JavaScript they would need a usable id.
It does add some complexity in that 'name[0]' has to assume 'detail[0]' is the correct element.
PHP will create an array and append elements if the field name ends with [] similar to
<input name="field[]" value="first value" />
<input name="field[]" value="second value" />
// is roughly the same as
$_POST['field'][] = 'first value';
$_POST['field'][] = 'second value';
Use arrays to hold you values in your submission. So bin the row count at the client side, and name your new elements like name[]. This means that $_POST['name'] will be an array.
That way at the server side you can easily get the row count (if you need it) with:
$rowcount = count($_POST['name']);
...and you can loop through the rows at the server side like this:
for ($i = 0; isset($_POST['name'][$i]; $i++) {}
You could extract all the rows by doing a foreach($_POST as $key => $value).
When adding a dynamic form element use the array naming method. for example
<input type="text" name="textfield[]" />
When the form is posted the textfield[] will be a PHP array, you can use it easily then.
When you remove an element make sure its removed from the HTML DOM.
Like blejzz suggests, I think if you use $_GET, then you can just cycle through all of the inputs that were sent, ignoring the deleted rows.
foreach ($_GET as $k=>$v) {
echo "KEY: ".$k."; VALUE: ".$v."<BR>";
}
I notice that you mention "accounting for deleted rows"; you could include a hidden input, and add a unique value to it each time someone deletes a row. For example, the input could hold comma-separated values of the row numbers:
<input type="hidden" value="3,5,8" id="deletions" />
and include in your jQuery script:
$('.delete').click(function(){
var num = //whatever your method for getting the row number
var v = $('#deletions').val();
v = v.split(',');
v.push(num);
v = v.join(',');
$('#deletions').val(v);
});
Then you should be able to know which rows were deleted (if that is what you were looking for).
you can use POST or GET
After submit you can use all of your form element with this automaticly. You dont need to reorganise your form element names. Even you dont need to know form elements names.
<form method="POST" id="fr" name="fr">.....</form>
<?php
if(isset($_POST['fr'])){
foreach($_POST as $data){
echo $data;
}
}
?>
Also you should look this
grafanimasyon.blogspot.com.tr/2015/02/veritabanndan-php-form-olusturucu.html
This is a automated form creator calcutating your database tables. You can see how to give name to form elements and use them.

PHP: Parsing of a given form input variable

How would I go about parsing incoming form data where the name changes based on section of site like:
<input type="radio" name="Motorola section" value="Ask a question">
where 'Motorola section may be that, or Verizon section, or Blackberry section, etc.
I do not have any control over changing the existing forms unfortunately, so must find a way to work with what is there.
Basically, I need to be able to grab both the name="" data as well as its coresponding value="" data to be able to populate the email that gets sent properly.
Well, you don't receive a HTML form, but just field names and values in $_POST. So you have to look what to make out of that.
Get the known and fixed fields from $_POST and unset() those you've got [to simplify]. Then iterate over the rest. If " section" is the only constant, then watch out for that:
foreach ($_POST as $key=>$value) {
if (stristr($key, "section")) {
$section = $value;
$section_name = $key;
}
}
If there are multiple sections (you didn't say), then build an section=>value array instead.
<form action="formpage.php" method="post">
<input type="radio" name="Motorola_section" value="Ask a question">
</form>
$motorola = $_POST['Motorola_section'];
if ($motorola =='Ask a question')
{
form submit code if motorola is selected
}
Well, first off, you shouldn't have spaces in the name field (even though it should work with them).
Assuming it's a form, you can get the value through the $_POST (for the POST method) and $_GET (for the GET method) variables.
<?php
if ( isset( $_POST['Motorola section'] ) ) // checks if it's set
{
$motoSec = $_POST['Motorola section']; // grab the variable
echo $motoSec; // display it
}
?>
You can also check the variables using print_r( $_GET ) or print_r( $_POST ).

Categories