...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
July 2, 2010

select all checkbox by one checkbox not working

Question by User123

following is my code, can any one tell me whats going wrong.

<input type="checkbox" name="Chk[]" value="<?php echo($arr['id']); ?>" onClick="Click()" id="CK">// all checkboxex
<input onClick="selectall()" type="checkbox" value="yes" name="c1">// one checkbox
/*function called*/
function selectall()
    {
         if(document.frm.chk.value=="false")
         {
             document.frm.chk.value="true";
             for(var i=0; i<document.frm.elements.length; i++)
               {
                 if((document.frm.elements[i].type)=="checkbox")
                   {
                     document.frm.elements[i].checked = true;
                   }
                }
         }
        else if(document.frm.chk.value=="true")
        {
            document.frm.chk.value="false";
             for(var i=0; i<document.frm.elements.length; i++)
               {
                 if((document.frm.elements[i].type)=="checkbox")
                   {
                     document.frm.elements[i].checked = false;
                   }
                }
        }
    }

Answer by Andy Robinson

I grabbed your code and tweaked it slightly bit rough and ready but seems to work…

    <form name='frm'>
<input type="checkbox" name="chk" value="<?php echo($arr['id']); ?>" onClick="Click()" id="CK">// all checkboxex
<input onClick="selectall()" type="checkbox" value="yes" name="c1">// one checkbox
</form>
/*function called*/
<script>
function selectall()
{
if(!document.frm.chk.checked)
{
    document.frm.chk.value="true";
    for(var i=0; i<document.frm.elements.length; i++)
    {
        if((document.frm.elements[i].type)=="checkbox")
        {
            document.frm.elements[i].checked = true;
        }
    }
}
else if(document.frm.chk.checked)
{
  document.frm.chk.value="false";
    for(var i=0; i<document.frm.elements.length; i++)
    {
        if((document.frm.elements[i].type)=="checkbox")
        {
            document.frm.elements[i].checked = false;
        }
    }
}
}
</script>     

Now when clicking the second checkbox it will check/uncheck the other one. Had to check the checked property rather than value.

Oh and like @Gordon said much easier to use a debugger like Firebug to find out why it’s not working. Assuming your form is called is called frm?

Answer by Starx

use document.form not document.frm

Read more
July 1, 2010

How to use jQuery to set value on screen rather than use alert?

Question by Autolycus

I am using the code that I will post below. I generates the password and works fine. Only problem is that it shows me the regenerated password in the alert box. I want to echo it on the screen. how can I do that. Thanks Using jQuery fancybox and php

$.ajax({
    type: 'POST',
    url: '/processPassword.php',
    data: 'newPassword=' + password,
    success: function(success) {
        if(success == 1) {
            alert('The password has been reset. to: ' + password);
            location.href = 'mainpage.php';
        } else {
            alert('The password was not reset.');
        }
    }
});
});

function newPassword() {
        var password = "";
        some logic...
        return password;
}

Answer by RSolberg

Try this. Replace the “alert” call with the jQuery line below to set the HTML of a div…

HTML

<div id="newPass"></div>

jQuery

//this assumes that "password" has already been setup.
$("#newPass").html(password); 

I also would strongly advise you to consider having your PHP page generate the password and to use jQuery or something similar to request a PW to be built with server side code. Making the PW with client side code seems to be a huge security hole, almost like giving the blue prints of the prison to the prisoners…

Answer by Starx

This is something you could try

$.post {
   "processPassword.php",
   { newPassword: 'password' },
   function(data) {
       alert('Your new password is+'+data) ;
});

In this the data is the value echoed by the page processPassword.php not return so you must echo your new password in the end of page or somewhere.

Read more

Looking for jQuery carousel

Question by Mike

Looking for jQuery carousel script that can do the following or similar I can tweak.

Go from this

imgur.com/hj9GL.gif

to this on rollover/hover (also rotate slowly on its own)

imgur.com/L2XFp.gif

Can anyone point me in the right direction? Thanks!

Answer by Starx

Create the carousel based on <div> rather than <img> because you can use background property in <div> and you can easily change it hover.

This is a much simpler approach to what you want to accomplish.

Read more

Temporary Variable

Question by Alex

I have a function that retrieves some data from the database, formats it as HTML and then stores this HTML into a variable, $output. The problem is that $output cannot be echoed on the screen now, only later, after other functions run. Also this data must be retrieved at this stage, not later.

So how can I call this $output variable in another function, after output to the screen has started?

Answer by Pekka

You could define $output in the main script, and import it into a function:

function output() 
 {  
   global $output;

This will probably work for your situation at hand. However, it is considered bad practice to use the global variable space with stuff like this, and rightly so. (Believe me, I’ve done it for years. 🙂

There are some other approaches that are better for long-term code quality and maintainability.

Global config array

You could either keep one global array for all global settings. Do this somewhere in your main script:

$config = array();
$config["output"] = "<html>.......</htmL>";
$config["user_language"] = "en";
.....

you import the configuration array into the function like so:

function output()
 { global $config;
   echo $config["output"];

Registry pattern

if you want to do some reading, you could use something more advanced like the Registry Pattern. The snippet shown here looks a nice example for a registry. The Zend Framework also has a class for this.

But things like a Registry are really, really advanced, and probably not necessary for you at this point. I would suggest using one central config array. Should the need for something more complex arise, the config array is easy to find and replace.

Context: Are global variables in PHP considered bad practice?

Answer by Starx

Use Session

For example:

session_start();
$query = '...........';
$result = mysql_query($query);
$_SESSION['mysqlResult'] = $result;

Now you can use it from any page at any time by simply calling like this

$myresult = $_SESSION['mysqlResult'];
Read more

PHP change file extension

Question by Kyle

I am trying to change a file exenstion, but whenever I do the file seems to corrupt.

$oldFileName = $targetDir . DIRECTORY_SEPARATOR . $fileName;
$newString = preg_replace('".tmp$"', '.jpg', $oldFileName);
rename($oldFileName, $newString);

The code works and changes the extension, but yet the file when downloaded, comes up as being corrupt.

The exension is .tmp and I am trying to change it to .jpg.

If I download the .tmp and manually change it to a .jpg it works, but not when the PHP does it.

Anyone know why this may be happening?

Thanks!

Answer by Starx

try this

<?php
$file = 'example.txt';
$newfile = 'example.txt.bak'; //new file with extension

if (!copy($file, $newfile)) {
    echo "failed to copy $file...n";
}
?>
Read more
June 28, 2010

Can't get jQuery hide working

Question by fearofawhackplanet

I need a sanity check as I’ve spent about an hour trying to figure this out!

getRows().each(function() {
    alert(this);     // alerts '[object HTMLTableRowElement]', nothing wrong here
    this.hide();     // row not hidden - wtf?
    alert('hidden'); // no alert - more wtf!
});

What can be wrong that calling hide() is bombing out?

Answer by Nick Craver

Inside the .each() this is a DOM element (HTMLTableRowElement), you need to wrap it to make it a jQuery object again (which has the .hide() method) like this:

$(this).hide();

Without this, you’re getting a method undefined error, because HTMLTableRowElement doesn’t have the .hide() method 🙂 This error is also why the alert isn’t firing afterwards, because execution stopped on the error.

Answer by Starx

try $(this).hide()

Read more

how to replace hyphen with blank space / white space? php

Question by mali

i don’t know much about PHP but these days I’m modifying an existing script. I want to know how can I replace – with white or blank space

For example a variable contains ‘Love-you’ and I want to replace this hyphen between them with a space like this ‘Love you’.

I’ll appreciate your feed back.

Answer by Starx

You can also use explode and implode

Here is an example

$string = "Love-You";
$arr = explode("-",$string);
$string = implode(" ",$arr);
Read more

sticky CSS footer broken

Question by MyBigBigHands_32

My footer is designed to stay at the bottom of the page even if the div above it only has a small amount of content. It worked until recently, and I seem to have broken it somehow.
Can you take a look?

Thanks in advance.

CSS:

body {
    margin: 0;
    padding: 0;
    height: 100%;
    font: 100% Helvetica, sans-serif, Arial, sans-serif;
    color: #000;
    background-color: #FFF;
    background-image: url(images/BGmain.png);
    background-repeat: repeat-x;
}
/*----------
Div styles
----------*/
#container {
    min-height: 100%;
    position: relative;
}
.header {
    padding: 0 0 230px 0;
    text-align: center;
    background-image: url(images/BGlogo_55top.png);
    background-repeat: no-repeat;
    background-position: top;
}
.column1 {
    padding-bottom: 50px;
    width: 960px;
    margin: 0 auto;
    position: relative;
}
.footer {
    position: absolute;
    bottom: 0;
    width: 100%;
    height: 50px;           
    text-align: center;
}
/*----------
Other
----------*/
.plainimg {
    border-style: none
}
/*----------
Text styles
----------*/
p {
    font-size: 80%;
    color: #333;
    line-height: 150%;
    text-align: left;
    padding: 1px 15px 1px 15px;
}
h1 {
    font-size: 100%;
    color: #000;
    padding: 0;
}
h2 {
    font-size: 100%;
    font-weight: 500;
    color: #000;
    padding: 0;
    text-align: center;
}
/*----------
Links
----------*/
a.navlink:link, a.navlink:visited {
    text-decoration: none;
    display: inline-block;
    color: #F1F1F1;
    width: 120px;
    text-align: center;
    padding: 0 0 3px 0;
    font-size: 80%;
}
a.navlink:hover, a.navlink:active {
    text-decoration: none;
    display: inline-block;
    color: #FFF;
    background-color: #000;
    width: 120px;
    text-align: centre;
    padding: 0 0 3px 0;
    font-size: 80%;
}
a:link, a:visited {
    text-decoration: none;
    color: #AEAEAE;
}
a:hover, a:active {
    text-decoration: underline;
    color: #999
}

The div arrangement is as follows:

<div id=container>
<div class=header></div>
<div class=column1></div>
<div class=footer></div>
</div>

Answer by Brock Adams

As Jason McCreary said, you need to add height to the html CSS.

Use:

html 
{
    height:     100%;
    margin:     0;
    padding:    0;
}

On your pages this triggers an extraneous scrollbar for some reason.

UPDATE:
The scrollbar appears to be triggered by the overflow of the .footer h6.
Adding: bottom: 2.5ex; and line-height: 1; to the footer style appears to clears that.

But a better way is to use a CSS reset.

With no reset, at the minimum, add:

.footer h6 {
    margin: 0;
    padding: 0;
}

.

A CSS reset will also minimize cross-browser variation that busts the layout from platform to platform.

Answer by Starx

Solved. Easy Solution Just put your Footer Division outside of your Container Division.

<div id=container>
    <div class=header></div>
    <div class=column1></div>
</div>
<div class=footer></div>
Read more
June 25, 2010

Precedence of the javascript events

Question by Purushotham Reddy

Is there any precedence to the javascript events that occur in a HTML page ?
For example:

consider these two events onchange and onblur.

Change the text of a text box. The event onchange occurs only onblur of the text box.
In this case which event will be processed first.

I hope I gave the understandable example.

Answer by Starx

onChange, occurs first, if this is what you are asking

Read more

PNG transparency with IE6

Question by Leo

I need to overlay two PNG images and see this in IE6.

Simple overlay like:

<div style="position: relative; width: 150px; height: 300px;">
<img src="im1.png" alt="" style="position: absolute; top: 0; left: 0;">
<img src="im2.png" alt="" style="position: absolute; top: 0; left: 0;">
</div>

works only for 256 color images, it doesn’t work at all ofr true color images.

Is there any way to overlay true color images in IE6?

Answer by Starx

Try this, this ie pngfix also supports true color

http://www.twinhelix.com/css/iepngfix/

Read more
...

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