Create random string in PHP Smarty template - php

I have the following input field:
{assign var="name" value="test12345.example.com"}
<input name="domain" value="{$domain}" size="50"
data-hostname-check="{$options}"
placeholder="<?php echo substr(md5(mt_rand()), 0, 7); ?>"
onchange="if (
typeof simulateCart == 'function' && this.checkValidity())
simulateCart.call(this);"/>
What i need is to generate a random string for if the input field when its not filled. But I have no clue how to do this. This input field is in an if loop using smarty template.
The onchange is required at the moment so if I remove this you can leave the input field blank but there should be a label before we can insert it into the database
At the moment I have a var (static) whichh i can use for the placeholder text. The echo which i tried does not work. Maybe someone can help me fixing this. If i put this in the placeholder the whole text in the "" will be displayed.
Another issue is that the form has an input validation and you have to press on a button. I know there is an jQuery on.click how can i use this in my situation.

You can write smarty plugin wich will output random string.
See chapter "function plugin with output" here https://www.smarty.net/docs/en/plugins.functions.tpl
You need only create file with you functuion
function smarty_function_randomstring($params, Smarty_Internal_Template $template) {
return substr(md5(mt_rand()), 0, 7);
}
Ant put it in plugin directory https://www.smarty.net/docs/en/variable.plugins.dir.tpl
Then you can use it in smarty like this
<input placeholder="{randomstring}"

Related

show php string containing comma in tag-it input field

I am using https://github.com/aehlke/tag-it plugin in php page.
Now the problem is, i have a set of string like
$str1="AlaqahdariDeh-eShu, Afghanistan";
$str2="`Ohonua, Tonga";
$str3="India";
$str=$str1.",".$str2.",".$str3;
which i want to show in tag-it input field like this
<input type="text" id="branchText" name="branchText" value="<?php echo $str;?>" class="ui-widget" size="20" style="display: none;">
but tag-it also splitting the $str1 in two value like this
Can anyone suggest to show $str1 and $str2 as a two string instead four string in tag-it input field?
I suggest you change the delimiter when you call tagit.
$('#singleFieldTags').tagit({
singleFieldDelimiter: '*'
});
Then try:
$str=$str1."*".$str2."*".$str3;
Or try what #Devendra Bhandari said. Use htmlentities() php function:
$str1= htmlentities("AlaqahdariDeh-eShu, Afghanistan");
$str2= htmlentities("Ohonua, Tonga");
$str3= htmlentities("India");
$str=$str1.",".$str2.",".$str3;

One POST index shows up true, another undefined, yet both have been implemented (what seems to be) the same way

I have this problem I've been working on and can't seem to figure out. At the end of the day I would like to pass some values to a php script from a user form. I am working with files that were written a long time ago and have tried to treat the variable I am trying to pass the same way the original author tried to treat his own. Yet, despite trying to keep the approach the same, I am getting different results. Specifically, in the php script where I want to pass these variables, when I echo the value of $_POST['whole'] I get true or false, yet when I echo the value of $_POST['plotX'] I get a variable undefined error.
I apologize for the large amount of code, but I hope that if you search for terms queryDataCenterOfMass and plotX you will see what I am referring to.
The HTML portion comes out as follows. This is where the user selects true or false for 'whole' and true or false for 'plotX'. This is actually code from the webpage, php is used to generate this code, but I thought it would be simpler to paste the HTML.
<div id="wholeDiv" class="dynamicDiv">
<b>Whole</b>
<br>
<select id="whole" name="whole" class="atomTypeSelect">
<option value="">>>Select<<</option>
<option value="true">Yes</option>
<option value="false" selected="">No</option>
</select>
</div>
<div id="plotXDiv" class="dynamicDiv">
<b>Plot x dimension</b>
<br>
<select id="plotX" name="plotX" class="atomTypeSelect">
<option value="">>>Select<<</option>
<option value="true">Yes</option>
<option value="false" selected="">No</option>
</select>
</div>
The next part is the code that creates the submit button. The important part is, I belive, the onclick portion, which calls the function queryDataCenterofMass, which is given in a separate javascript file and I pasted below this.
<input type="hidden" name="table" value= <?php echo $_GET['schema'] ?>/>
<input type="hidden" name="queryName"
value= <?php echo $result[0]['query_id'] ?>/>
<input type='Submit' value=' Query Data' name='funcSearch'
onclick="queryDataCenterofMass(('<?php echo $result[0]['query_id'] ?>'),('<?php echo $_GET['schema'] ?>'),($('#firstFrame').val()) ,($('#lastFrame').val()) ,($('#skip').val()) , ($('#minX').val()) , ($('#minY').val()) ,
($('#minZ').val()), ($('#maxX').val()), ($('#maxY').val()), ($('#maxZ').val()), ($('#whole').val()), ($('#whole_pcb').val()), ($('#molName').val()),($('#atomTypeDll').val()), ($('#atomID').val()), ($('#molID').val()) ), ($('#plotX').val()), ($('#plotY').val()), ($('#plotZ').val()); false;"
class='dynamicParamButton'><br/><br/>
Here is the definition of queryDataCenterofMass. It seems to use a jQuery function to pass everything via post to comfunction.php.
function queryDataCenterofMass(queryId, schema, firstFrame, lastFrame, frameSkip, minX, minY, minZ, maxX, maxY, maxZ, whole, wholePcb, molName, atomType, atomID, molID, plotX, plotY, plotZ) {
$("#waiting").show(500);
$("#interface").hide(0);
// generate a random number to pass to function.php and append to the end of our gnuplot files to make them unique
var random_num = Math.floor((Math.random() * 1000000) + 1);
$.post('./function_files/comfunction.php',
{
random_num: random_num,
query_id: queryId,
simschema: schema,
firstFrame: firstFrame,
lastFrame: lastFrame,
frameSkip: frameSkip,
minX: minX,
minY: minY,
minZ: minZ,
maxX: maxX,
maxY: maxY,
maxZ: maxZ,
whole: whole,
whole_pcb: wholePcb,
molName: molName,
atomType: atomType,
atomID: atomID,
molID: molID,
plotX: plotX,
plotY: plotY,
plotZ: plotZ
},
function (data) {
$("#waiting").hide(0);
$("#results").show(500);
var img = document.getElementById("com_img");
img.src = "../queries/gnuplot_tmp_files/gnuplot_output" + random_num;
$('div#message').html(data).fadeIn({duration: 700});
$('html,body').animate({
scrollTop: $('div#message').offset().top
});
});
}
So that's it. I am not sure why in comfunction.php the variable $_POST['plotX'] is undefined yet other variables, including $_POST['whole'] are not. Solving this problem is very important to me... if there is some way I can better ask this question, or more information I can provide, please don't hesitate to let me know. Thanks.
The onclick line
($('#molID').val()) ),
Seems to have an extra bracket here. Which makes the onclick callback end early, before the plotX and other elements could be sent.
Protip/s:
- Bad idea to load mix such javascript eventhandlers inside HTML
- I simply loaded the html in a IDE which showed the syntax error (among others)
- use browser developer tools.

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.

Having problems theming the search box in Drupal

I'm not the world's most experienced Drupal and I'm having a nightmare trying to customise the search box. I'm using the example from the Drupal help site and it's simply not working.
I've copied search-theme-form.tpl.php from the search module into my theme and copied the example code, Any HTML in that code outside of the pre-process function shows fine but as far as I can tell, the pre-process function either isn't called or just isn't affecting my search box.
I'm almost certain that I'm missing something absolutely fundamentally basic about how Drupal works but I can't find any info at all.
Here's the code:
<?php
function danland_preprocess_search_theme_form(&$vars, $hook) {
// Remove the "Search this site" label from the form.
$vars['form']['search_theme_form']['#title'] = t('');
// Set a default value for text inside the search box field.
$vars['form']['search_theme_form']['#value'] = t('Search this Site');
// Add a custom class and placeholder text to the search box.
$vars['form']['search_theme_form']['#attributes'] = array('class' => 'NormalTextBox txtSearch', 'onblur' => "if (this.value == '') {this.value = '".$vars['form']['search_theme_form']['#value']."';} ;", 'onfocus' => "if (this.value == '".$vars['form']['search_theme_form']['#value']."') {this.value = '';} ;" );
// Change the text on the submit button
//$vars['form']['submit']['#value'] = t('Go');
// Rebuild the rendered version (search form only, rest remains unchanged)
unset($vars['form']['search_theme_form']['#printed']);
$vars['search']['search_theme_form'] = drupal_render($vars['form']['search_theme_form']);
$vars['form']['submit']['#type'] = 'image_button';
$vars['form']['submit']['#src'] = path_to_theme() . '/images/search.jpg';
// Rebuild the rendered version (submit button, rest remains unchanged)
unset($vars['form']['submit']['#printed']);
$vars['search']['submit'] = drupal_render($vars['form']['submit']);
// Collect all form elements to make it easier to print the whole form.
$vars['search_form'] = implode($vars['search']);
}
?>
<div id="search" class="container-inline">
<?php print $search_form; print $search_theme_form; ?>
</div>
Put the preprocess function in template.php in your theme directory, then clear the cache at "admin/settings/performance"
Here are some helpful links to learn about theming with templates/preprocess functions:
About overriding themable output
Setting up variables for use in a template (preprocess and process functions)

Drupal - Webform element theming

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

Categories