...

Hi! I’m Starx

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

jQuery Load form elements through ajax, from onchange event of drop down box

Question by miojamo

I have dropdown and need to dynamical load form elements based on statement

<select name="type" id="type">
   <option value="1">input</option>
   <option value="2">text</option>
</select>

case 1 load some form elements (inout etc..)
case 2 clear these elements

Thanks

Answer by andres descalzo

try this:

$(function() {

  $('#type').bind('change', function(ev) {

     var value = $(this).val();

     $.ajax({
        ...
        data: {valueType: value, html: encodeURIComponent($("#addhtml").html())},
        ...
     });

  });


});

Answer by Starx

We may want to try this also

$("#type").change(function() {
    $.post(
        "yourRequestHandlingPage.php",
        { 
          param: $(this).val();
        },
        function(data) {
            //supposing data holds the html output of dynamically creawted form element
            $(".myformcontent").append(data); //add the elements at the end of the form elemetn
        }
});
Read more

jQuery resizable plugin, to resize only height

Question by Goppie TOp

How to make alsoResize option to only work for height but not for both width and height?

Answer by Starx

Actually if you set maxWidth to a limit, we cannot resize the width more than it.

For example,

$( ".selector" ).resizable({ maxWidth: 250 }); //For initialzation 

will not allow the resize to occur more that 250px in width

If you check this demo
http://jqueryui.com/demos/resizable/#max-min, you will notice the width resizing being limited to a point.

So in your case set the initial width of the .selector and maxWidth same and it is done.

For further information go to http://jqueryui.com/demos/resizable/#option-maxWidth

Read more
December 2, 2010

How to write specific CSS for mozilla, chrome and IE

Question by Sussagittikasusa

What would be the CSS conditional statement you can use to include specific CSS for IE, Mozilla, Chrome.

If IE  
#container { top: 5px; }  

If Mozilla 
#container { top: 7px; }    

If Chrome  
#container { top: 9px; }

What would be the respective ‘If’s’ ?

Answer by Starx

For that

  • You can scan user Agent and find out which browser, its version. Including the OS for OS specific styles
  • You can use various CSS Hacks for specific browser
  • Or Scripts or Plugins to indentify the browser and apply various classes to the elements

Using PHP

See

Then then create the dynamic CSS file as per the detected browser

Here is a CSS Hacks list

/***** Selector Hacks ******/

/* IE6 and below */
* html #uno  { color: red }

/* IE7 */
*:first-child+html #dos { color: red } 

/* IE7, FF, Saf, Opera  */
html>body #tres { color: red }

/* IE8, FF, Saf, Opera (Everything but IE 6,7) */
html>/**/body #cuatro { color: red }

/* Opera 9.27 and below, safari 2 */
html:first-child #cinco { color: red }

/* Safari 2-3 */
html[xmlns*=""] body:last-child #seis { color: red }

/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:nth-of-type(1) #siete { color: red }

/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:first-of-type #ocho {  color: red }

/* saf3+, chrome1+ */
@media screen and (-webkit-min-device-pixel-ratio:0) {
 #diez  { color: red  }
}

/* iPhone / mobile webkit */
@media screen and (max-device-width: 480px) {
 #veintiseis { color: red  }
}


/* Safari 2 - 3.1 */
html[xmlns*=""]:root #trece  { color: red  }

/* Safari 2 - 3.1, Opera 9.25 */
*|html[xmlns*=""] #catorce { color: red  }

/* Everything but IE6-8 */
:root *> #quince { color: red  }

/* IE7 */
*+html #dieciocho {  color: red }

/* Firefox only. 1+ */
#veinticuatro,  x:-moz-any-link  { color: red }

/* Firefox 3.0+ */
#veinticinco,  x:-moz-any-link, x:default  { color: red  }



/***** Attribute Hacks ******/

/* IE6 */
#once { _color: blue }

/* IE6, IE7 */
#doce { *color: blue; /* or #color: blue */ }

/* Everything but IE6 */
#diecisiete { color/**/: blue }

/* IE6, IE7, IE8 */
#diecinueve { color: blue9; }

/* IE7, IE8 */
#veinte { color/***/: blue9; }

/* IE6, IE7 -- acts as an !important */
#veintesiete { color: blue !ie; } /* string after ! can be anything */

Source: http://paulirish.com/2009/browser-specific-css-hacks/

If you want to use Plugin then here is one

http://rafael.adm.br/css_browser_selector/

Read more
November 30, 2010

Newbie stuck on a jQuery question…conditional statement

Question by flinx777

So I’m a complete newbie and stuck on a conditional statement. Here’s my code:

<script type="text/javascript">
$(document).ready(function(){
 if($('span.fc_cart_item_price_total') == 0) {
  $(span.fc_info).addClass('foo');
 };
});
</script>

So I’m attempting to see if span with a class of “fc_cart_item_price_total” has a value of “0”, to then add a class of “foo” to the span with a class of “.fc_info”. This code above is not working. Here’s the HTML:

<span class="fc_info">Info 1</span><br />
<span class="fc_cart_item_price_total">$0.00</span><br />
<span class="fc_info">Info 2</span>

Here’s the other challenge I have. I’m trying to select the span with the value of “fc_info” before the span with the class of “fc_cart_item_price_total” but have no idea of how to just select this one span.

Answer by einaros

For each cart item whose price is $0.00, this will add the “foo” class to the preceding (and only the preceding) fc_info.

$(function() {
    $(".fc_cart_item_price_total:contains($0.00)").each(function(i, n) {
        (n=$(n)).prevAll(".fc_info:first").addClass("foo");
    });
});

Answer by Starx

To make the above code working

<script type="text/javascript">
$(document).ready(function(){
 if($('span.fc_cart_item_price_total').html() == 0) {
  $(span.fc_info:first).addClass('foo');
 };
});
</script>

And to select the first class fc_info

Use $(".fc_info:first") as your selector

Read more
November 29, 2010

Rename and uploaded file?

Question by acctman

Is there a simple way to rename an upload file? I’d like to append $_POST['bid'] to the front of the file that was uploaded.

Answer by Starx

Rename a already uploaded file

Use rename function http://php.net/manual/en/function.rename.php

It is as easy as

rename ("/folder/file.ext", "/folder/newfile.ext");

Rename a file during upload

move_uploaded_file($_FILE['myfile'][tmp_name],$_POST['bid'].$_FILE['myfile'][name]);
Read more

How to move blog title in the middle of the screen?

Question by Startup Crazy

I am very new to blogging.My blog has its title description at the top left but I want it to be in the middle of the screen and the description of the blog beneath the image.Thanks in advance.

EDIT:

I did:

   <title style="text-align:center"><data:blog.pageTitle/></title >

But the title is still at the topleft corner.

Answer by Stephan Muller

The <title> element isn’t used for the title displayed on your website, only for the title in the browser’s top bar.

You’ll have to edit the CSS file and set #titlewrapper { text-align: center }

Alternatively you can add style="text-align: center" in the <div id='titlewrapper'> in your HTML.

Answer by Starx

Well, I got it. The title is wrapped with a <h1> which is by default aligned at the left. so change it’s CSS to text-align:center;

Use this

#titlewrapper h1 { text-align:center; }
Read more

What sort of page weight / size should I be aiming for when optimising for mobile?

Question by Simon Scarfe

I’m looking to optimise the mobile browser experience in a small webapp, using the awesome jQuery mobile to do so.

It goes without saying that a user doesn’t want to DL 200k of data, I’m just trying to draw the line between using external and internal URLs. Is there any existing guideline of what sort of page sizes / loading times I should be shooting for? I’d prefer to stick to internal URLs (keep the mobile interface effectively in one place from a maintenance point of view), but am weary of bogging the user down with lots of information that they have no intention of viewing.

Answer by galambalazs

One thing to note is that e.g. iPhone won’t cache components bigger than 25K.

Also consider minifing and gzipping your code. And @jfar got one thing right. HTTP requests can be a huge performance hit, so you may also want to reduce them as much as possible.

Answer by Starx

I would say try to lower your page as much to 100kb, because if u manage to do so loading different pages as per user request might be negligible.

However, loading the big pile of content at a time and using simple javascript show and hide, to display the content in almost realtime manner might impress the viewer too.

Read more
November 28, 2010

Deleting an array element with PHP

Question by user478419

I have an array with several elements. The array keys are numeric.

I now want to remove a certain element. All I know is the element’s content, not the key.

Is there an easy way to remove this element from the array?
Or do I need to loop through all elements of the array to get the key and then unset the element with that key?

Answer by Codemwnci

You can use the array search function to get the key

$key = array_search('searchterm', $items); 
unset($items[$key]);

Answer by Starx

sure

foreach($myarray as $key=>$item) {
    if($item=='myknowncontent') { unset($myarray[$key]; }
}
Read more
November 26, 2010

Problem with form submission

Question by shalu

I am facing a problem. I am having a form. Its action is ac.php. I want to change the action to sit.php.

For this what i did

   <script type="text/javascript">
     document.getElementById("digForm").innerHTML="<form enctype='multipart/form-data' id='product_addtocart_form' method='post' action='http://localhost/bracelatest2/index.php/checkout/cart/add/uenc/aHR0cDovL2xvY2FsaG9zdC9icmFjZWxhdGVzdDIvaW5kZXgucGhwL2JsYWNrLWJyYWNlLmh0bWw_X19fU0lEPVU,/product/203/'>";
   </script>

<div id="digForm">
    <form action="ac.php">
</div>

Now I am using ajax and changing the inner html of div myform by

    <form action="sit.php">

But when i hit submit form is going to ac.php not to sit.php.

Please help me.

Answer by Starx

If you just want to change the attribute then use

document.getElementById('myform').action = 'sit.php';

Check the demo here Shalu http://jsfiddle.net/bR2KA/

Read more
...

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