...

Hi! I’m Starx

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

Does it matter where JavaScript is placed on a html page?

Question by bplus

I’ve messing about with html5, I’ve never really had a good look at JavaScript before.
I’m referencing script file like this (not in the head)

<script src="somthing.js"></script>

However the script only seems to work if it placed below certain elements on the page.

Are there particular situations when it matters where javascript is placed?

Thanks in advance.

Answer by Starx

Well, here is what I think.

If you need to execute your script, before the HTML starts the render in the clients browser then it will be better to be placed in <head> section.

And if you are executing a JavaScript code which communicates with a element in some way then the script should be placed behind that element, because if the script starts ahead then it can’t find its respective element to communicate with. So it is better to placed behind element.

Read more

Working with decimal numbers in PHP

Question by The Worst Shady

I want to somehow round the numbers for a rating system in PHP like this:

4.6667 = 4.6

5.0001 = 5.0

Is there any way to do that? (BTW, I read the data from a database.)

Answer by Sarfraz

You are not conforming to any single rule. For example:

4.6667 = 4.6
5.0001 = 5.1

See these functions anyway:

round
ceil
floor

And number_format.

Answer by Starx

Use this:

echo round(1.45667, 2);

The number “2” is how many decimal places you want.

This is output 1.46.

Read more

DIVS, CSS and Iframes

Question by Beauford

I have this DIV:

#bar {
  margin: 5px 0px 10px 0px;
  height: 25px;
  background:#c0c0c0;
  color: #E0E0E0;
  border: 2px solid #444444;
  background: #333333;
  padding: 5px;
}

And this Iframe:

<div id="bar">
<iframe src="myvotes.php?u=<? echo $_GET['u']; ?>" width="100%" height="28px" scrolling="no" frameborder="0"></iframe>
</div>

Inside the iframe (myvotes.php) there are 5 small images 16 x 16 each followed by some text. I am trying without success to have the images and text vertically aligned in the [div id=”bar”].

First off, how high should my iframe be. I have tried everything from 16px to 35px. I have tried adding various combinations of padding and margins to get it t center, but no luck,

Again, I have no link to provide, but any help is appreciatred. These little things are so annoying and waste huge anounts of time I don’t have.

Answer by Starx

Not sure of your question but I will try to give you a solution where you can align your 16×16 images vertically.

Create a div inside your myvotes.php and keep your images inside it. An Example.

<div id="someone" style="width:16px;">
   <img src=".........."/>
   <img src=".........."/>
   <img src=".........."/>
   <img src=".........."/>
   <img src=".........."/>
   <img src=".........."/>
</div>

This will bring your images in a vertical column.

Now your main div and iframe

<div id="bar">
<iframe src="myvotes.php?u=<? echo $_GET['u']; ?>" width="100%" height="80px" scrolling="no" frameborder="0"></iframe>
</div>

I am not sure, this is your answer though. Anyways hope it helps.

Read more
July 21, 2010

multiple 3D text shadow using css

Question by Kumod

I want to give shadow effect to some of my texts in a website which would make it look like 3D using CSS Is it possible?

Answer by Starx

I dont know what you mean by Multiple, but if you want 3d like shadow see this tutorial below

http://css-tricks.com/examples/3DTextTower/

UPDATE

In that case

see this for multiple text shadows

http://css-tricks.com/snippets/css/text-dripping-blood/

Read more

Disable Scrollbar conditionally without javascript

Question by Mac

i want to hide the scrollbar conditionally i.e. i want to hide the scrollbar of the body when javascript is disabled and enabling it when javascript is enabled because in my page iam showing an message when javascript is disabled and not allowing to user to do anything by graying out the whole screen but as the height for different pages are different so iam using a big height in that case the scrollbar is creating problem in case of small height pages.

Answer by Starx

try this

<noscript>
     <style>
         body { width:100%; hight: 100%; overflow:hidden; }
     </style>
</noscript>
Read more

database connectvity with html

Question by kaylan

how we connect the database(oracle) with html

Answer by Starx

HTML, is a markup language which is readable for browser only.

We need database connection in order to communicate with the database from our server and for that we need a Server Side language similar to PHP, ASP.NET which after processing the database and other operations, at the end also gives a HTML output to the client’s browser.

So It is Impossible.

Read more
July 20, 2010

How to mark required field in jQuery Validate

Question by Henning

I’m using jQuery Validate and xVal to validate in input forms. This works great.
However, I do want to mark my required fields with an asterix (*) or something like that to show which field is actually required.

I could do this by hand, but since I already generate my validation rules with xVal I thought I could mark the required fields automatically based on the provided rules.

Is there a way to hook into either xVal or jQuery Validate to retrieve the rules, or somehow make them mark my required fields?

I’m using the latest versions of jQuery and jQuery Validate.

Thanks

Answer by Starx

Give the class required to the form elements, and use the following code

$(document).ready(function() {
     $('<span style="color:red;">*</span>').insertAfter('.required');
});

This will attach “*” to every elements with required class

Hope it helps

Read more
July 18, 2010

jQuery submit ajax form with 2 submit buttons

Question by tetris

im trying to achieve the following, in php i have a form like this:

<form method="post"  id="form_1" action="php.php">
<input type="submit" value="add" name="sub"/>
<input type="submit" value="envoi" name="sub"/>
</form>

the form action file is:

<?php

if( $_POST["sub"]=="add"){  ?>

 <script>
 alert("")
 </script>
<?php  echo "ZZZZZZ";   ?>

<?php } ?>

so this means if i press sub with value add an alert prompt will come up, how can i do the same thing(differentiate both submit) but using a Ajax request:

the following code so does not work:

 $(function(){
      $('form#form_1').submit(function(){
var _data= $(this).serialize()
$.ajax({
        type: 'POST',
        url: "php.php?",
        data:_data,
        success: function(html){
         $('div#1').html(html)

          }
     })
})
  })
  </script>
</head>

<body>
<div id="1" style="width: 100px;height: 100px;border: 1px solid red"></div>

<form method="post"  id="form_1" action="javascript:;">
<input type="submit" value="add" name="sub"/>
<input type="submit" value="envoi" name="sub"/>
</form>
</body>

Answer by Pointy

You could put the event handler on the buttons instead of on the form. Get the parameters from the form, and then add a parameter for the button, and post the form. Make sure the handler returns “false”.

$(function() {
  $('input[name=sub]').click(function(){
    var _data= $('#form_1').serialize() + '&sub=' + $(this).val();
    $.ajax({
      type: 'POST',
      url: "php.php?",
      data:_data,
      success: function(html){
         $('div#1').html(html);
      }
    });
    return false;
  });
});

You have to explicitly add the “sub” parameter because jQuery doesn’t include those when you call “serialize()”.

Answer by Starx

Lots of semicolon missing, see below

 $(function(){
      $('form#form_1').submit(function(){
         var _data= $(this).serialize();
         $.ajax({
            type: 'POST',
            url: "php.php?",
            data:_data,
            success: function(html){
               $('div#1').html(html);    
            }
         });
      });
  });
Read more

which is better single query or multiple query?

Question by mathew

I do have 8 tables. when a query fires from search page data from all these 8 tables is pulled out and displayed on the result page. What I want to know is which is the best optimized query method for this process??

what I do now is :

$result = mysql_query("SELECT * FROM TG_dat,TG_dat1,TG_dat2,TG_dat3 WHERE 
TG_dat.web = TG_dat1.web AND TG_dat.web = TG_dat2.web AND TG_dat.web = 
TG_dat3.web AND TG_dat.web='".$uri."'")or die(mysql_error());

or do i need to use this??

$result = mysql_query("SELECT * FROM TG_dat WHERE web='$uri'")or die(mysql_error());
$result = mysql_query("SELECT * FROM TG_dat1 WHERE web='$uri'")or die(mysql_error());
$result = mysql_query("SELECT * FROM TG_dat2 WHERE web='$uri'")or die(mysql_error());
$result = mysql_query("SELECT * FROM TG_dat3 WHERE web='$uri'")or die(mysql_error());

Answer by OMG Ponies

Your existing query is perfect – always try to use as few queries as possible.

Answer by Starx

your existing query is good. The point is to limit querying db server for better effieciency

Read more
...

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