I am a newbie in jQuery. I am trying to use the auto complete jQuery pluggn in my ASP.NET page .I downloaded the sample from the site and trying to rewrite it from PHP to ASP.NET
Can any one help me to rewrite it ?
<script type="text/javascript">
$().ready(function() {
function findValueCallback(event, data, formatted) {
$("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
}
function formatItem(row) {
return row[0] + " (<strong>id: " + row[1] + "</strong>)";
}
function formatResult(row) {
return row[0].replace(/(<.+?>)/gi, '');
}
$("#imageSearch").autocomplete("images.php", {
width: 320,
max: 4,
highlight: false,
scroll: true,
scrollHeight: 300,
formatItem: function(data, i, n, value) {
return "<img src='images/" + value + "'/> " + value.split(".")[0];
},
formatResult: function(data, value) {
return value.split(".")[0];
}
});
and page contains
<input type="text" id='imageSearch' />
<input type="button" value="Get Value" />
Content of images.php
$term = $_REQUEST['q'];
$images = array_slice(scandir("images"), 2);
foreach($images as $value) {
if( strpos(strtolower($value), $term) === 0 ) {
echo $value . "\n";
}
}
I have some question on this
1 . I understand the images.php will return the list of images.Can any one provide me the format with an example ?Will that be a collection of list items,or divs or a single string seperated by some characters
2 . How the javscript is formating the output received.In my case i want to return a list of items(image names) and a Text (caption) with an id for it.So that users will see the image and the text in the option list and when he chooses,i want to show another page there ( i want to use the anchor tag there to navigate to another page with the id of option which user selected.
Kindly guide me Thanks in advance
1 . I understand the images.php will
return the list of images.Can any one
provide me the format with an example
?Will that be a collection of list
items,or divs or a single string
seperated by some characters
Not having run the code, it will be a list of strings separated by newline ("\n") chars; e.g.,:
"image1.jpg\nimage2.jpg\nimage3.jpg"
2 . How the javscript is formating the output received.In my case i want to return a list of items(image names) and a Text (caption) with an id for it.So that users will see the image and the text in the option list and when he chooses,i want to show another page there ( i want to use the anchor tag there to navigate to another page with the id of option which user selected.
You might want to have a look at JSON if you want to return structured data from the server.
Related
How to create custom mobile number validation in Wordpress contact form 7 plugin is it possible please help me.
Actually i want to only valid mobile number is allowed this field not taken any other character for ex- 9708644571
To use the phone number validation , you need to put the number field as per example given below.
[number* your-number min:9 max:10 step:3 class:required "40"]
Reference link : CF7
If you want to use custom validation for mobile number, then you can use jQuery also.
Just like this:
$('.wpcf7-form .custom-field-class input').on('keypress', function(e) {
//if the letter is not digit then display error and don't type anything
if (e.which != 8 && e.which != 0 && (e.which < 43 || e.which > 57)) {
return false;
}
});
Place this file in your page template where form exists or in the footer.php or in your custom js file.
Please replace the custom-field-class with your input field wrapper class or you can modify your classes & inputs name according to your requirement.
Hope, this may be helpful to you.
// Phone number validation : format and 10 digits
// Add code in your functions.php file
function custom_contact_validation_filter( $result, $tag ) {
// 'contact': is name of tag in contact form
if ( 'contact' == $tag->name ) {
$re_format = '/^[0-9]{10}+$/'; //9425786311
$your_contact = $_POST['contact'];
if (!preg_match($re_format, $your_contact , $matches)) {
$result->invalidate($tag, "Enter 10 digits only" );
}
}
return $result;
}
add_filter( 'wpcf7_validate_text*', 'custom_contact_validation_filter', 10, 2 );
add_filter( 'wpcf7_validate_text', 'custom_contact_validation_filter', 10, 2 );
I'm using 2 custom fields... left_column & right_column which are WYSIWYG..so can add imagery... I want to runa check that "IF theres imagery, then add the class 'image_box'...
The thing is, I can get to identify images...but applying the logic to both columns...pulls in 2 sets of the images...regardless of what column (left or right) they were added to.
My code:
if(get_sub_field('extra_left_column')){
if(get_sub_field('extra_left_column', $attachemnt['ID'])) {
$extra_column_right.= '<div class="col6 right_column image_box">'.wpautop(apply_filters('the_content',get_sub_field('extra_right_column'))).'</div>';
} else {
$extra_column_right.= '<div class="col6 right_column">'.wpautop(apply_filters('the_content',get_sub_field('extra_right_column'))).'</div>';
}
}
if(get_sub_field('extra_right_column')){
if(get_sub_field('extra_right_column', $attachemnt['ID'])) {
$extra_column_right.= '<div class="col6 margin_left right_column image_box">'.wpautop(apply_filters('the_content',get_sub_field('extra_right_column'))).'</div>';
} else {
$extra_column_right.= '<div class="col6 margin_left right_column">'.wpautop(apply_filters('the_content',get_sub_field('extra_right_column'))).'</div>';
}
}
I thought my code would be 'column specific' but it's not....if I add images only to my 'Righ tColumn' fileds within a post...it also pulls the same imagery into the 'left column' on the live site... so I've got duplicates... not good.
First of all dump the values of both the subfields to see what data is being returned. If its just an empty field then, you can check for !empty() or !== ''.
If you are using v3.3.4 or later you can alos use a function called has_sub_field, to check if the field was set.
I'm trying to hide the display of custom fields if they have no values. If I display them with the_meta, then the headers show, whether or not there are any values in the Custom Fields.
It generates this html:
<ul class="post-meta">
<li>
<span class="post-meta-key">My Custom Field Title</span>
</li>
<li>
</ul>
I really don't want titles on the page if there's no content. So I tried:
<?php
$ck = get_post_custom_keys($post_id); //Array
foreach ($ck as $k) {
$cv = get_post_custom_values($k, $post_id ); //Array
foreach ($cv as $c) {
echo (' - ');
echo ($c);
}
}
?>
Echo for $c (the custom values applied to the post) looks like this:
With no values:
1 - 1343633746:1 - - field_5014a45c9a2df - field_5014a48c38f9d - - field_5014a48c2cc82
With values:
1 - 1343603999:1 - 3 cups of eggs - field_5014a45c9a2df - field_5014a48c38f9d - 2 cups of flour - field_5014a48c2cc82
This is probably the long way to go about it. It's showing values for things like "last edit" too, not just the two fields that I put in to test.
How I can show the Custom Fields only if they have values? I'm writing a theme, so I do not know the names of the fields, or how many, beforehand.
Thanks!
You can check for empty values with empty(), you can check if the field starts with underscore (internal values like _edit_last) and skip those.
<?php
$ck = get_post_custom_keys($post_id); //Array
foreach ($ck as $k) {
if (substr ($k, 0, 1) == '_')
{ // skip keys starting with '_'
continue;
}
$cv = get_post_custom_values($k, $post_id ); //Array
foreach ($cv as $c) {
if (empty ($c))
{ // skip empty value
continue;
}
echo ($k . ': ' . $c . '<br/>');
}
}
?>
Is your theme only displaying certain custom fields? Instead of testing against everything, why not just test against whether those specific custom fields are empty?
E.g.
$item = get_post_meta($post_id, "custom_field_1", true);
if (!empty($item)) { // if that field has something
[...] // display what you need to
}
Given a dynamically loaded heading (using PHP/WordPress) is there a pure CSS method of styling the final word? For instance:
<h1 class='featured'> This is my page title</h1>
Becomes
<h1 class='featured'> This is my page <span id="finalWord">title</span></h1>
If using CSS is not viable, is exploding the content of the <h1> tag the suggested way to do this?
AFAIK there is not a :last-word pseudo class in CSS (although that would be cool). You could use JavaScript for something like this, but if you have access to the <h1> server-side I'd do it with PHP. It's going to be part of the source code and probably easier.
A simple algorithm would be to find the last space in the string with strrpos() and use substr() to piece it back together wrapped in <span>.
I guess a really crude way would be to create a way to all the words and put it in a PHP array. Then echo all the values, and if it's the last one then put the <span id="finalWord"> before and the </span> after.
EX:
Step 1: Create the array
$your_text = "Your text goes here";
$pieces = explode(" ", $your_text);
Now you have all your data in a array. Each word is in it's object.
echo "<h1 class='featured'>";
$the_count = count($pieces);
foreach ($your_text as $i => $value) {
$i = $i + 1;
if ($i == $the_count) { echo "<span id='finalWord'>"; }
echo $value;
if ($i == $the_count) { echo "</span>"; }
}
echo "</h1>";
So basically what this code does is count how many objects are in your array and will check if the object being displayed is the last one. If it is, it will put the correct ID on it.
I just typed this code out real quick, so there could be some errors.
Coulton
is there a pure CSS method of styling
the final word
No such method in principle. CSS cannot modify the DOM.
Take text of dom element, find last word using your own criteria for the "word", wrap it into span and set innerHTML by the result.
Sorry for the follow up on an ancient post, this is the one that came up in my google searches for "wrap the last word in a string with a span tag using php" so I figured this is where I would put the solution I came up with.
Using the above as a starting point, I created this function to accomplish what I needed:
function wrap_last($string, $wrap_start = '<span class="font-bold">', $wrap_finish = '</span>') {
$string_array = explode(' ', $string);
$count = count($string_array);
$new_array = array();
foreach ($string_array as $i => $value) {
$i = $i + 1;
$array_part = '';
if ($i == $count) {
$array_part .= $wrap_start;
}
$array_part .= $value;
if ($i == $count) {
$array_part .= $wrap_finish;
}
$new_array[] = $array_part;
}
$new_string = implode(' ', $new_array);
return $new_string;
}
I'm trying to generate unique id's on the 'a' tag of the menu item so that I can implement Popups API.
This is what my function in template.php looks like:
function phptemplate_menu_item_link($link) {
if (empty($link['options'])) {
$link['options'] = array();
}
// If an item is a LOCAL TASK, render it as a tab
if ($link['type'] & MENU_IS_LOCAL_TASK) {
$link['title'] = '<span class="tab">'. check_plain($link['title']) .'</span>';
$link['options']['html'] = TRUE;
}
if (empty($link['type'])) {
$true = TRUE;
}
//get unique id from menu item title
$css_id = phptemplate_id_safe(str_replace(' ', '_', strip_tags($link['title'])));
//set unique id for link
if ($link['menu_name'] == 'primary-links') {
$link['options']['attributes']['id'] = 'id-' . $css_id;
}
return l($link['title'], $link['href'], $link['options']);
}
I'm debugging w/ Zend and the conditional statement works. I've cleared the cache, my browser cache, and rebuilt the menu multiple times, but can't seem to get it to work.
Just as a reference, phptemplate_id_safe is custom (obviously) and works fine.
You might want to have a look at menu_attributes or this forum post.