...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
March 18, 2013

jQuery remove email in textarea

Question by JR Galia

I have HTML textarea.

<textarea> ... </textarea>

I don’t want users to enter any email address in the textarea. What is the best approach on this using jQuery? How to remove email entered before submitting the form?

I only need hint and references. Thanks.

Answer by nbrooks

jsFiddle Demo

Use a regular expression to match the email addresses, and then replace them with blank strings.

$(function() {
    $("textarea").change(function() {
        $(this).val( function(idx, val) {
            return val.replace(/b(w)+@(w)+.(w)+b/g, "");
        });
    });
});

Answer by Starx

Although a genuine concern, the use of JavaScript here will not give you the result you are expecting. The problem is that you want to remove the email addresses automatically before you are submitting the form. This is not a user friendly approach.

You have to alert the user that he cannot enter a email address in the box and ask him to correct the mistake.

Removing the email address part is MUCH SAFER DONE IN SERVER Level. Like from PHP, ASP, JSP etc.

Alert the user of email address like this.

$("textarea").change(function() {
    if(/^w+@[a-zA-Z_]+?.[a-zA-Z]{2,3}$/.test($(this).val()) {
         alert('you have entered email address, which is not allowed');
    }
});

Then remove the email for your Server Application.

Read more
March 16, 2013

Fatal error: Allowed memory size, when using ip2long function

Question by user1941709

I am using the ip2Long function below,

function ip_range($start, $end) {
    $start = ip2long($start);
    $end = ip2long($end);
    return array_map('long2ip', range($start, $end) );
}

$range_one = "86.188.249.48 ";
$range_two = "86.188.249.55";
print_r( ip_range($range_one, $range_two) );

But I get the following error:

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to
allocate 32 bytes) in /home/site/public_html/path/checkrange.php on
line 6

Answer by Samuel Cook

$range_one is not considered a valid IP. If you remove the space off the end then this works for me:

$range_one = "86.188.249.48";

To avoid this in the future, you should trim your values:

$start = ip2long(trim($start));
$end = ip2long(trim($end));

Answer by Starx

The is a space at the end of the variable.

$range_one = "86.188.249.48 ";
                        // ^ Remove this
Read more

OnClick fopen fread fwrite fclose

Question by artur99

I have a image:

 <img src="imagini/floo.gif" onclick="score()">

and i want “on click” to be opened a file “c.txt”.
That file’s content is:
0

And i want on every click, to be added 100 in c.txt.

I want something like this:

 <img src="imagini/floo.gif" onclick="score()">
<script> function score(){ (..do file add.php ...) }</script>

And add.php:

<?php
$c = "c.txt";
$fh = fopen($c, "r");
$current = fread($fh, filesize($c));
fclose($fh);
$current++;
$fh = fopen($c, "w");
fwrite($fh,$current);
fclose($fh);
?>

What to put in place: “(..do file add.php …)” for the code to work?

Answer by Starx

You need to send an AJAX request to process the file. Here is a jQuery version of the code:

function score() {
    $.post("add.php", function(data) {
       //This callback executes after the request was successfull
       // echo something back to read from here as `data`
       console.log(data);
    });
}
Read more

how to handle two function

Question by Rohit Khurana

I have two funtions searching and searchhistory. Currently these two function are running in my code but the value of nbofimages is not updated.

Also it’s showing only one alert message after searchhistory function is completed.

$.when(searching(searchkwd, check_st, check_sf, '31', '1', filter, ori, colorcheck))
 .then(searchhistory(user_search,search_pars,search_username,nbofimages));

var nbofimages;

function searching(searchkwd, check_st, check_sf, '31', '1', filter, ori, colorcheck)
{
    nbofimages = "3";
    alert(nbofimages);
}

function searchhistory(user_search,search_pars,search_username,nbofimages)
{
    alert(nbofimages);
}

Answer by Starx

Function passed to $.when requires a promise to be returned.

function searching(searchkwd, check_st, check_sf, '31', '1', filter, ori, colorcheck)
{
    nbofimages = "3";
    alert(nbofimages);
    return true;
}
Read more

Link not working in LI

Question by Pjack

OMG I’m pulling my hair out trying to figure out why some links don’t work in the UL. However if your right click open in new tab that works. I don’t understand why some are not clickable. The first two links work but they are events used by jQuery. 3rd and 4th links are actual pages and those don’t work and never does the last javascript/jquery link. Just the first two using jQuery. The others do not. I’ve reformatted my CSS several times and no difference. The unusual tags is because I use Smarty. BTW using Chrome.

This is the HTML

Edit: rendered markup

<div id="phomenu" class="photoMenu">
    <ul>
    <li><a id="avatar_13885_10028" class="set_avatar" href="#13885">Use This Photo As Avatar</a></li>
    <li><a id="cover_13885_10028" class="set_cover" href="#13885">Use This Photo As Album Cover</a></li>
    <li><a href="/page-13885-k4cjGSDSG4K.html">Page Photo</a></li>
    <li><a href="/?page=photo&amp;section=desc&amp;pho_id=13885">Edit Photo Information</a></li>
    <li><a id="remove_k4cjGSDSG4K_13885" class="remove_photo" href="javascript:void(0)">Delete this photo</a></li>
    <li><a id="feature_13885" class="feature_photo" href="javascript:void(0)">Feature Photo</a></li>
    </ul>
</div>

And this is the CSS

.photoMenu ul{
        list-style-type: none;
        list-style-position: outside;
        margin: 0;
        padding: 0;
        font-size:9pt;
        border-top: 2px solid #ffa449;
    }
    .photoMenu li {
        margin: 0;
    }
    .photoMenu li a {
        color: #FFFFFF;
        border-bottom: 1px solid #e6e6e6;
        display:block;
        min-height:25px;
        padding-top:10px;
        background-color:#C6711B;
        text-decoration: none;

    }
    .photoMenu li a:hover {
        color: #FFF;
        background-color: #f2a83a;
    }

Answer by Starx

The code you have shown so far does not have any problem. The problem is somewhere else, may be your script is interfering with the link.

Proof

If the browser is just made at you, it can get angry with the javascript:void(0) part

Although unnecessary it may require a ; at the last

<li><a id="remove_k4cjGSDSG4K_13885" class="remove_photo" href="javascript:void(0);">Delete this photo</a></li>
<li><a id="feature_13885" class="feature_photo" href="javascript:void(0);">Feature Photo</a></li>
Read more
March 13, 2013

Why use media queries ? The different to the %

Question by Ruwantha

I know that media queries are use to create responsive layouts. But My question is we can do the same thing using ” % “. So my question is then why media queries?

Answer by Starx

Defining layout using % help create a fluid layout, NOT RESPONSIVE layouts.

Media queries help you to define different style sets for different screen sizes.

Also with Media Queries, you don’t have to be limited to just heights and weights, you can control more than sizes.

Example below creates different background for different screen sizes:

@media only screen and (min-width: 320px) {
   body { 
      background: red;
   }
}

@media only screen and (min-width: 780px) {
   body { 
      background: blue;
   }
}

Can % do that?
No

Read more
March 12, 2013

Concatenating lang CONST and STR CONST in PHP

Question by Jorge

Trying to concatenate:

<?php
class MD_ImpFormularios extends CI_Model {
  private $dir_forms = __DIR__ . 'Hola';

Gives:

PHP Parse error:  syntax error, unexpected '.', expecting ',' or ';' in md_impformularios.php on line 3

But I don’t see anything wrong here, it isn’t a CONST or Static, it’s a simple variable.

Thanks

Answer by Starx

Do not do concatenation while declaring class variables.

private $dir_forms = __DIR__ . 'Hola';
                          // ^ This is NOT allowed during declaration

You can use your constructor function to set such variables.

private $dir_forms;
public function __construct() {
    $this -> dir_forms = __DIR__ . 'Hola';
}
Read more
March 11, 2013

How To Write A Simple Click Opacity Fade?

Question by user2122160

Okay, so I’m simply trying to make the opacity fade from 0 to 1 and then 1 to 0 on click. I’m assuming that I need to write an if statement. Here is the code as it is right now.

$(document).ready(function() {
     $('#soilink').click(function() {$('#soi').animate({opacity:1}, 400 );}

    );

So right now, if I click on the link on my website, the div area called #soi fades in. However, the second part is that I need to be able to click the link once again so it fades back to 0. Haven’t quite figured that part out.

EDIT

I want this to toggle, however using the toggle function will result in the div completely becoming absent from the html, causing other divs to shift.

Answer by Starx

There is a fadeTo() function for that

$('#soilink').click(function() {
   $('#soi').fadeTo(400, 1);
});

If you are trying create a toggle effect, there is also a fadeToggle() function.

$('#soilink').click(function() {
   $('#soi').fadeToggle(400);
});

Update:

Here is how to do that.

$('#soilink').click(function() {
   $('#soi').stop().fadeTo(400, $('#soi').css('opacity') == 0 ? 1 : 0); 
});
Read more

Efficiently Merge values from table2 into table1

Question by Jakobud

Look at the following SQLFiddle for reference: http://sqlfiddle.com/#!2/f02ca/1

I have two nearly identical tables. I want to determine the difference between the table data, update one table with the different data from the other table.

table1
|| *id* || *some_id* || *timestamp* ||
||    1 ||         A ||       01:00 ||
||    2 ||         B ||       02:00 ||
||    3 ||         B ||       01:00 ||
||    4 ||         A ||       02:00 ||
||    5 ||         A ||       03:00 ||

table2
|| *id* || *some_id* ||
||    1 ||         B ||  <-- some_id is different than table1
||    2 ||         B ||
||    3 ||         B ||
||    4 ||         A ||
||    5 ||         B ||  <-- some_id is different than table1

So I want to update table1, with new some_id‘s from table2. I can easily find the differences between the two tables:

SELECT t2.id
FROM table2 t2
LEFT JOIN table1 t1 ON t1.id = t2.id
WHERE t2.some_id != t1.some_id

Result:

1, 5 (the id's of the table2 rows that have a different some_id).

I thought I could do this type of query:

UPDATE table1
SET some_id = 
    (SELECT some_id FROM table2 WHERE id IN 
        (SELECT t2.id 
        FROM table2 t2
        LEFT JOIN table1 t1 ON t1.id = t2.id 
        WHERE t2.some_id != t1.some_id ))

I thought that query would update table1 with new some_id‘s from table2 but only for rows where the some_ids where different between them. But I get the following error:

SQL Error (1093): You can't specify target table 'table1' for update in FROM clause

Am I on the right track here? If so, how do I get around this problem? Is there a better or more efficient way up accomplishing this?

A couple notes:

  1. It’s important that I do not just update or insert everything from table2 into table1, because of the timestamp column. That column will update itself when the row is updated. The only rows I want updated in table1 are the rows who’s some_id is different in table2. So essentially, the timestamp column is there to show when that row was last changed.
  2. It’s important that this is done in as few queries as possible and that they are done efficiently. In my simple example here, there are only 5 rows per table, but in reality my tables have many thousands of rows.

Answer by Starx

I am afraid not. In MySQL, it is not possible to modify the same table from which the SELECT is done.

Check these: http://dev.mysql.com/doc/refman/5.6/en/update.html

Currently, you cannot update a table and select from the same table in a subquery.

Read more
...

Please fill the form - I will response as fast as I can!