...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
April 16, 2012

Jquery DateTimePicker not visible

Question by patxy

I’m trying to implement this http://trentrichardson.com/examples/timepicker/ to my project.

The problem is that nothing happens.

So, here is what I’ve done and not removed :

-I’ve downloaded the .js file and put it inside the “Scripts” file.
-I added a reference to this script that way :

<script src="@Url.Content("~/Scripts/jquery-ui-timepicker-addon.js")" type="text/javascript"></script>

I’ve added this at the beginning of the page :

 <script type="text/javascript">
$(function () {
    $('.datetime').datetimepicker();
});
 </script>

I’ve added this at the end of the css file :

 /* css for timepicker */
 .ui-timepicker-div .ui-widget-header { margin-bottom: 8px; }
 .ui-timepicker-div dl { text-align: left; }
 .ui-timepicker-div dl dt { height: 25px; margin-bottom: -25px; }
 .ui-timepicker-div dl dd { margin: 0 10px 10px 65px; }
 .ui-timepicker-div td { font-size: 90%; }
 .ui-tpicker-grid-label { background: none; border: none; margin: 0; padding: 0; }

And I’ve also this :

 <input type="text" class="datetime" id="datetime1" value="" />

The problem is that there is only the textbox that is displayed. The timepicker isn’t. What haven’t I well understand?

Answer by Tats_innit

Here you go demo for you as well : http://jsfiddle.net/atP2P/

HTML

class was missing

<script src="http://trentrichardson.com/examples/timepicker/js/jquery-ui-timepicker-addon.js" type="text/javascript"></script>

<input type="text" name="datetime" id="datetime1" class="datetime" value="" />
​

Answer by Starx

Your are using class .datetime while initializing the plugin. So your HTML should have class in them

 <input type="text" name="datetime" id="datetime1" class="datetime" value="" />

Demo

Or, you can initialize your plugin to select the id instead of the class like:

$(function () {
    $('#datetime1').datetimepicker();
});

Either way, it will work.

Read more

Is there a shortcut way to refer to elements in my DIV?

Question by Marie J

I have the following:

<div class="modal hide fade" id="adminDialog" style="display: none;">
    <div class="modal-header">
        <a data-dismiss="modal" class="close">×</a>
         <h3 id='dialog-heading'></h3>
    </div>
</div>

I created this variable:

dialogDiv = $('#adminDialog');

I would like to change the text of the heading. If I want to do this do I need to
use $('#dialog-heading').text() or is there a shortcut way that is based on the dialogDiv variable?

Answer by Starx

If it has to be based on the dialogDiv then this is the best one I know

$("h3", $("#adminDialog")).text('new text');

Or

$("h3", dialogDiv).text('new text');

This selector tell you to find a h3 within the scope of #adminDialog.


But you are using an id for the div, so no selector can top the direct selection. Like

$('#dialog-heading').text('new text');
Read more

jquery change display doesnt work

Question by Dmitry Makovetskiyd

I want a div to become visible, when a “button” is pressed.
Here is the html that represents the button:

<span id='dl' class='btn' data-app-id=''>DOWNLOAD<br/>FREE</span></div>

Here is the html of the div in its initial stage before the button is pressed.

<div class='box' id='dlbox' style='display:none'>
    <div class='title'>
        <span class='text'>Confirm install</span>
        <span id='x1' class='x'></span>
    </div>
    <p>Click to install </p>
    <p><?php echo "<span style='font-weight:bold'>$app->appName</span>"; ?></p>
</div>​

The jquery is this:

$("#dl").live("click", function() {
    $("#dlblank").toggle();
    $("#dlbox").toggle();
});​

The css of the div when “toggled”:

.box {
    position: absolute;
    background: #4d0404;
    opacity: none;
    z-index: 9001;
    -webkit-box-shadow: 0 5px 7px black;
    text-align: center;
    color: white;
    top: 50px;
}

Answer by matpol

I personally prefer to use show and hide instead toggle as it is a bit clearer what is going on. I would also put the disply none in the jquery too.

$('#dlbox').css('display':'none');   
$(document).ready(function() {   
       $("#dl").live("click", function(){
            $("#dlbox").show()
        });
    });

Answer by Starx

You have an markup error. A closing </div> without opening. On the first line

Next, you might want to stop using .bind() it is deprecated. You should use .on() instead.

$("#dl").on("click", function(){
    $("#dlblank").toggle();
$("#dlbox").toggle();});

Demo without the div

Read more

Multidimensional array with many sub arrays and mysql – how to deal with it?

Question by Delicja

I have very long multidimensional array that have many sub arrays. I would like to insert some value from them into mysql. I would be gratefull for some tip how to deal with it. I would like to use a loop that put only some value to one or different table in database. How I can get value from for example [Things][Thing][k][value]? Thanks for any advice.

Array
(
[Data] => Array
    (
        [A] => Array
            (
                [B] => Array
                    (
                        [0] => Array
                            (
                                [C] => Array
                                    (
                                        [value] => some value1
                                    )
                                [D] => Array
                                    (
                                        [value] => some value2
                                    )
                                [E] => some value3
                            )
                        [1] => Array
                            (
                                [C] => Array
                                    (
                                        [value] => some value4
                                    )
                                [D] => Array
                                    (
                                        [value] => some value5
                                    )
                                [E] => 5
                            )
                    )
                [value] => 
            )
[Things] => Array
            (
                [Thing] => Array
                    (
                        [F] => Array
                            (
                                [value] => some value6
                            )
                        [G] => Array
                            (
                                [H] => Array
                                    (
                                        [0] => Array
                                            (
                                                [i] => Array
                                                    (
                                                        [value] => some value7
                                                    )
                                                [j] => Array
                                                    (
                                                        [value] => some value8
                                                        [value] => some value8
                                                    )
                                                [k] => Array
                                                    (
                                                        [value] => some value9
                                                    )
                                                [l] => Array
                                                    (
                                                        [value] => some value10
                                                    )
                                                [m] => some value11
                                                [n] => 
                                            )
                                        [1] => Array
                                            (
                                                [o] => Array
                                                    (
                                                        [value] => some value12
                                                    )
                                                [p] => Array
                                                    (
                                                        [value] => some value13
                                                    )
                                                [r] => Array
                                                    (
                                                        [value] => some value14
                                                    )
                                                [d] => Array
                                                    (
                                                        [value] => some value15
                                                    )
                                                [t] => some value16
                                                [u] => 
                                            )
                                    )
                                [value] => 
                            )
                            )
                            )           
                    [Thing2] => Array       
                    (

                            (...)

                            and so on...

In my database I have table for these things from array above. There is id in it of course and some other fields that are connected with other tables.
As you can see below I would like to get some values from array, insert it into Table Thing, and some of them put into
Table for field 4 and Table for field 5 and get id of them and put it into Table Thing. I’ll use stored procedure.
I would like to call it from php (I hope that this conception is good?). I have problem to get values that I want to get from this array so David Chan I’m looking for help with array and loop. I tried as Starx has written but I got nothing (white screen). I’m still learning so I know that I have to do something wrong.

............          .................       ..................
Table Thing           Table for field 4       Table for field 5

id int                id int                  id int
field 1 varchar       value varchar           value varchar
field 2 varchar
field 3 varchar       ................        ................
field 4 int
field 5 int
(...)
...........

Answer by Starx

Whenever you are trying to access multidimensional array, you have to use indices to point a particular field.

For example:

$data['A']['B'][0]['C']['VALUE'] would give you the value some value1

Use a similar way to get the values and insert into the database.

Read more

regex to allow decimals, but no commas

Question by Rookieatthis

I need a regex expression that allows decimals but no commas. Such as:
1200.00
or
50.00
or
40
or
1
The expression is used in a validation to make sure people put in a price as a number, but no commas. I currently have a regex that allows for commas, numbers, and decimals, but I cannot seem to get the commas out.

This is the current regex that I have which doesnt allow commas or decimals, but I want decimals in there

/^[0-9 ]+$/,

Thanks for your help.

Answer by npinti

Something like this should work: ^d+(.d{2})?$

This basically states the following:

  • ^d+: Start from the beginning of the string (^) and match one or more digits (d+).
  • (.d{2})?: Match a period character (.) followed by exactly 2 digits (d{2}). The question mark denotes that the value within the brackets can either exist or not, meaning that it will match either 1 or 0 instances of the pattern.
  • $: The string must end here.

Answer by Starx

To match the currency you can use this regex

^(?:[1-9]d+|d)(?:.dd)?$
Read more

How do I make an image have padding while keeping the webkit-box-shadow?

Question by cjmabry

I want to style all of the images on my website to have box-shadows and padding of 10px, so that text isn’t smooshed up against them. However, when I assign any padding to “img” with CSS, the box-shadow is drawn at the edge of the padding, leaving a 10px blank space around the image.

#content img {
    -moz-box-shadow: 0 0 5px 2px #282a2d;
    -webkit-box-shadow: 0 0 5px 2px #282a2d;
    padding:10px
}

This particular image is floated left within the paragraph. here is an example of my problem –

http://imgur.com/kKY0A (I can’t post pictures yet)

Any ideas?

EDIT: I do not want the padding. I just want the box-shadow, and then space, so that text doesn’t mash up right next to the box-shadow. Turns out what I wanted was margin, not padding. Silly mistake.

Answer by Starx

Give the background of the same color

#content img {
    -moz-box-shadow: 0 0 5px 2px #282a2d;
    -webkit-box-shadow: 0 0 5px 2px #282a2d;
    background: #282a2d;
    padding:10px;
}

Demo


UPDATE:

As mentioned in comment, OP seems to be OK without padding too. So, I will just complete my answer.

#content img {
    -moz-box-shadow: 0 0 5px 2px #282a2d;
    -webkit-box-shadow: 0 0 5px 2px #282a2d;
    margin:10px
}
Read more

Adding two click events to the same button only works once

Question by bob_cobb

Basically I’m trying to make a button be able to handle editing of an element. I want it so that when I click on the Edit button, it changes the text to Save Changes and adds a class which will then bind the button to another click event so that when they click Save Changes, it’ll alert Saved and change the text back to Edit. It does this perfectly once. If you continue to try to do it, it simply won’t add the class or change the text anymore.

Here is a demo on jsfiddle

The code:

$(function() {
$button = $('button[name="edit"]');
$button.on('click', $button, function() {
    var $that = $(this);
    $that.text('Save Changes');
    $that.addClass('js-editing');
    if ($that.hasClass('js-editing')) {
        $that.off('click').on('click', $that, function() {
            alert('Saved!');
            $that.text('Edit');
            $that.removeClass('js-editing');
        });
    }
});

});​

Answer by qw3n

Try this http://jsfiddle.net/bpD8B/4/

$(function() {
    $button = $('button[name="edit"]');
    $button.on('click', $button, function() {
        var $that = $(this);
        if($that.text()=='Edit'){
          $that.text('Save Changes');
          $that.addClass('js-editing');
        }
        else{
                alert('Saved!');
                $that.text('Edit');
                $that.removeClass('js-editing');
        }
    });
});

Answer by Starx

This is just a logic problem. And with $that.off('click').on('click', $that, function() { you are delegating to itself, which is not how you should do it.

Here is a solution using your code:

$(function() {

    $button = $('button[name="edit"]');
    $button.on('click', $button, function() {
        var $that = $(this);
        if ($that.hasClass('js-editing')) {
            alert('Saved!');
            $that.text('Edit');
            $that.removeClass('js-editing');        
        } else {      
            $that.text('Save Changes'); 
            $that.addClass('js-editing');
        }
    });

});​

Demo

Read more
April 15, 2012

PHP header not working for server bu works perfectly fine on local host

Question by user1334969

this below code works perfectly fine when i try on my local host but when i upload it on to my server it dose not work

$sendquery = "INSERT INTO `message` (`from`, `to`, `subject`, `date`,`message`) VALUES ('".$_SESSION['UID']."', '".$toarray['Uid']."', '$subject','$d', '$message')";
$sendresult = mysql_query($sendquery);

if($sendresult)
{
    header('Location: home.php?action=inbox&success=1');

}
else
{
    header("Location: home.php?action=inbox&success=2");
}

is it problem with my server or the code

Answer by Starx

The localhost and webserver have many differences together. I am guessing the problem to be developed on windows and hosted on linux.

Lowercase all the header string and stop the execution after it

header("location: home.php?action=inbox&success=2");
exit();
Read more

Working with modules inside codeigniter

Question by Jeff Davidson

I’m using the wiredesigns moduler extentions framework with codeigniter and I’m trying to find out what I need to do to get my links working correctly because the pages I”m trying to load are not loading. I’m receiving a error message saying “The page you requested was not found”.

-application/controllers/dashboard.php
-application/modules/bios/controllers/quotes

When I’m inside the dashboard controller it shows the dashboard view file and there is a link that I want to open the quotes controller inside the bios module because it’ll show the quotes view page so what is the correct I have this for the link:

link url/modules/bios/quotes

Is that not how the link is supposed to look.

Answer by Starx

Unless the settings have been manipulated, the url should look something like this

url/<module name>/<controller name>/<method name>
Read more

SQL Statement Giving me an error?

Question by PHPLOVER

My code below (must be something to do with sql statement (the UPDATE query statement), basically when i go in browser and visit script with a key that i know exists in the database i get the following error:

[15/04/2012 18:33:57] - exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'user_activation_key'' in C:wampwwwuser-verify.php:53
Stack trace:
#0 C:wampwwwuser-verify.php(53): PDOStatement->execute(Array)
#1 {main}

Here is my code:, not sure what it saying about duplicate entry, as the user_activation_key column is unique and yes i am using InnoDB and foreign keys for data interigty.

// check if key is set and alphanumeric and equals 40 chars long
// we use sha1 so it will always be 40 chars long.
if(isset($_GET['key']) && ctype_alnum($_GET['key']) && strlen($_GET['key']) == 40){
    $key = trim($_GET['key']);
}

// if key isset
if(isset($key)){

try {
        // connect to database
        $dbh = sql_con();

        // if key is of valid length and type we need to update the `user_activation_key` in the `users_status` table to NULL
        // and update the `user_status`in the `users` table to 1 (tinyint)(active) based on the condition that the 
        // activation key can be found in the users_status.user_activation_key column and user_uid match in both users_status and users table
        $stmt = $dbh->prepare("
                            UPDATE
                                users
                            JOIN
                                users_status
                            ON
                                users_status.user_activation_key = ?
                            SET
                                users.user_status = 1,
                                users_status.user_activation_key = NULL
                            WHERE
                                users_status.user_uid = users.user_uid");

        // execute query
        $stmt->execute(array($key));

        if ( $stmt->rowCount() > 0 ) {

            echo 'account now activated';
            exit;

        } else {
            echo 'could not activate account at this time';
            exit;
        }

    // close database connection
    $dbh = null;

} // if any errors found log them and display friendly message
catch (PDOException $e) {
    ExceptionErrorHandler($e);
    require_once($footer_inc);
    exit;
}

} else {

// else key not valid or set
echo '<h1>Invalid Activation Link</h1>';

$SiteErrorMessages =
"Oops! Your account could not be activated. Please recheck the link in your email.
The activation link appears to be invalid.<br /><br />
If the problem persists please request a new one <a href='/member/resend-activation-email'>here</a>.";

SiteErrorMessages();

include($footer_inc);
exit;

}

Not sure why i am getting that error, any know what it means exactly ?

It won’t perform the update even thou the key exists in the users_status table. if i enter an invalid key it says could not activate account at this time which is what it should do but when the key is valid it should update but it’s outputting the error above.

Thanks,
phplover

UPDATE:

Thanks for the quick replies as always!

Here is the the database design for those two tables.

CREATE TABLE `users` (
  `user_uid` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'users unique id',
  `user_status` tinyint(1) unsigned NOT NULL COMMENT '0 = verify | 1 = active |  2 =  suspended | 3 = delete | 4 = spam |',
  `user_login` varchar(15) NOT NULL COMMENT 'users login username',
  `user_pass` char(152) NOT NULL,
  `user_email` varchar(255) NOT NULL COMMENT 'users email',
  `user_registered` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'user registration date',
  `user_display_name` varchar(60) NOT NULL COMMENT 'users display name (first & last name)',
  `user_failed_logins` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT 'failed login attempts',
  PRIMARY KEY (`user_uid`),
  UNIQUE KEY `user_login` (`user_login`),
  UNIQUE KEY `user_email` (`user_email`),
  KEY `user_pass` (`user_pass`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COMMENT=Users Table';

CREATE TABLE `users_status` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'auto generated id',
  `user_uid` int(10) unsigned NOT NULL,
  `user_activation_key` char(40) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_uid` (`user_uid`),
  UNIQUE KEY `user_activation_key` (`user_activation_key`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COMMENT='user status table, when a user registers they must first activate there account';


ALTER TABLE `users_status`
  ADD CONSTRAINT `FK_user_status` FOREIGN KEY (`user_uid`) REFERENCES `users` (`user_uid`) ON DELETE CASCADE ON UPDATE CASCADE;

Answer by Starx

On your query you are setting users_status.user_activation_key as NULL, and I am pretty sure, it has a UNIQUE index and value NULL must be already available in the table.

That’s why you are receiving that error.

Read more
...

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