PHP - Get value from saved dropdown - php

I have a plugin in WordPress in which I can make custom field like dropdown etc. Now I need to get the saved value from the dropdown.
I have this:
$vt_city = get_post_meta($cs_job_id, 'cs_post_loc_city', true);
But that displays it meta instead of the value like this: not_filled
And what I need it to show is: Not filled
How can I achieve such thing?

I can't see nothing about it in the doc, but you can use a regex at least
$clean_meta = preg_replace("/^(\w{1})([a-z]*)_([a-z]*)$/","$1$2 $3",$vt_city);

Related

Gravity forms $field_values not working

I am running the most curent version of WP and Gravity forms. I'm trying to call the form through a template file and pass through a few things like so:
gravity_form(2, $display_title=false, $display_description=true,
$field_values, $ajax=true);
I'm calling the $field_values like so:
$field_values = array("my" => "FOO", "your" => "BAR",);
They are simple not populating into gravity forms. Any ideas?
Make sure to click on the field and then click on the advanced tab and check mark the prepopulate field checkbox. This will drop down a text box in which you can specify a parameter name.
Ok, lets say that your form ID is 7, let me give you a step-by-step answer:
Create your form
Go to advanced
Check "Allow field to be populated dynamically"
Give it a name, eg.: my_name
gravity_form(7, false, false, false, array('my_name'=>'whatever you want to autofill'), false);
Now if you go to that form you will see that the field which had the name my_name has the value whatever you want to autofill.
Hope this helped, bye bye.
This documentation gives some helpful advice on populating fields:
gravityhelp.com/documentation/page/Allow_field_to_be_populated_dynamically
and here is an example of how I populate fields using a hook:
add_filter("gform_field_value_yourparametername", "populate_yourparametername");
function populate_yourparametername($value){
global $post;
$value = get_post_meta( $post->ID,'metakeyname',true);
return $value;
}
In order for this to work, you must check the box in the advanced tab of the form item in your Gravity Form that allows the field to be populated dynamically. Then you need to fill in the input below with 'youparametername'.

Set a checkbox based on its value

I am currently working on a javascript and php project. I have a checkbox array that have the same id chkCategory and the name chkCategory[]. I then have PHP return some json that contains the various categories and I want to set each checkbox checked attribute for each category that was in the json. The only thing that identifies each checkbox is the value. I.e. the json may have a value MyCategory so the checkbox that has the value MyCategory should be set.
I know you can use $("#myCheckBoxId").attr("checked", true) for an individual checkbox but not sure how to do this for a specific checkbox with a certain value.
Thanks for any help you can provide.
You can use standard CSS attribute selectors in JQuery. For example:
$("input[value=MyCategory]")
will select the input tag with attribute value=MyCategory. If there are different types of inputs with that value, you could also select on type=checkbox:
$("input[type=checkbox][value=MyCategory]")
If you know the index, you could do...
$("input[name='MyCategory']").eq(n).prop("checked", true);
If you don't have jQuery...
document.getElementsByName("MyCategory")[n].checked = true;
In plain javascript it would look like this:
​document.querySelector('input[value=checkboxValue]').checked = true;​​​​​​​​​​​​​​​​​​​​​​​​​​​​
where checkboxValue is the value of your checkbox

Altering the text after a checkbox using the Drupal Form Api

I got the following issue. A clients wants that the text after checkboxes are links to other pages and thus between ...
I have the following code:
$form['boxes_brands'] = array(
'#type'=>'checkboxes',
'#title'=>'<div id="title-container">Merken</div>',
'#options'=>$brandArr,
'#default_value'=>$_SESSION['filter_brands_cat'],
);
=> $brandArr is an array of brands.
I looked in the Form Api of Drupal but I did not find an option to do this. I could alter the values in $brandArr but of course that changes the value of the value attribuut of the input object too.
Using the prefix and suffix options won't do it either because I don't want the checkboxes in the tags too.
Is there a clean way to do this?
Thanks!
If you created the form with the UI, then you should be able to specify something like this in as the options and links would be rendered as links:
google|This is a link to google
yahoo|Yahoo
bing|Bing!
See example:
Otherwise, you should be able to modify the $brandArr accordingly to create links in the label. Doing this should NOT change the value of the attribute as it should be a $value->$label associative array. You just need to change the $label not the $value.

How to control the content (text) to display in a $form->input?

folks.
I'm starting with CakePHP and after reviewing this tutorial (
http://book.cakephp.org/view/1543/Simple-Acl-controlled-Application
) and also after having used the "cake bake" command to generate my
models,
controllers and views , everything is fine, but when I visit the
Post's add view (views \ posts
\ add.php), I find that instead of showing a input text for the
username, it shows a select with all the usernames.
this is the line in the Post's add view that show the select.
echo $this->Form->input('user_id');
PostsController :
function add() {
// Some other code
$users = $this->Post->User->find('list');
$this->set(compact('users'));
}
Although I know how to display only the username of the currently
logged-in user, I don't know how to control the content to show in
$this->Form->input() because if I use a variable that is not part of
the "Post" model , it's shown , but as the label for the input.
Have you any idea how to solve this??
P.S. I've been trying to find this information on the cookbook , but I haven't been able to find anything specific to my situation :(
If you want to add a string in as the value of the input, try this :
echo $form->input('my label text',array('value'=>'the value string','type'=>'hidden'));
That would make the input hidden, and set the value.
Or remove the type=>hidden and it will have a value, you can also add things like disabled and such in there, or to modify the div it creates, use div'=>array() and put your div options in that array... hope this helps.
i would like to help you on this thing.
first of all when you want a text box then you must define the type of the input field.
echo $form->input('user_id', array('type'=>'text', 'label'=false));
i hope this will help you.

jQuery: Using autocomplete to add values to two fields instead of one

I want to use the autocomplete plugin for jQuery to populate not one, but two fields when selecting one of the autocomplete values - the name of a band is entered in the #band input field, but the band url (if exists) should also automatically be added to the #url input field when selecting the band name.
Right now I simply have an un-pretty list in an external php file from which the autocompleter takes it's values:
$bands_sql = "SELECT bands.name, bands.url
FROM bands
ORDER BY name";
$bands_result = mysql_query($bands_sql) or print (mysql_error());
while ($bands_row = mysql_fetch_array($bands_result)) {
$band_name = $bands_row['name'];
$band_url = $bands_row['url'];
echo $band_name."\n"; #needs to be replaced with an array that holds name and url
}
My autocomplete function looks very basic atm, but as I'm an absolute beginner when it comes to jQuery (and also clueless when it comes to PHP arrays), I have no idea how to tell it to populate two fields and not one.
$(document).ready(function() {
$("#band").autocomplete('/autocomplete-bands.php');
});
Is that even possible?!
sure check use result hadler so you can then do what you want once a choice has been made
I don't know about the particular plug-in you are using, but I would use the autocomplete widget for jQuery UI instead of a third party plug-in.
Here is an example of what you are looking for:
$("#band").autocomplete('/autocomplete-bands.php').result(function(event, data, formatted) {
if (data)
$('#url').(data['url']);
else {
// no data returned from autocomplete URL
}
});
I don't know much about php, but whatever the format of your data that is returned should be put where the data['url'] is currently in order to populate the #url input.

Categories