...

Hi! I’m Starx

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

jquery/ajax post() in a posted page

Question by haltman

I have a page with a div, inside this div I load a page with a form with a select, I can get selected option by post() function but, how can I get at the same time selected option and full data option? I’ve tried to get full data with a post() in a click() function positioned directly on form page but it does not work, can I post 2 times on the same page(one for get selected option and one for full data option)?

thanks in advance

ciao,
h.

Answer by AdmSteck

I would use post to get all the options and then in the success function make another ajax post call to get the selected option. This way you can ensure all the options are loaded before trying to set the selected option.

Answer by Starx

If you are trying to load different portion on a page you can try this

  1. Create a page with all the contents you want to load, and seperate the contents with a switch for example “content.php”

    <?
    $key = $_POST['key'];
    switch($key) {
             case "news":
                  echo '<div id="news">.............News Content.............</div>';
                  break;
             case "link":
                  echo '<ul> 
                            <li> <a href="home.html">Home</a> </li> 
                            <li> <a href="aboutus.html">About us</a> </li> 
                        </ul>';
                  break;
             default:
                  echo "Sorry, the content is not available.";
                  break;
    }
    ?> 
    

Then afterwards in the page where you make calls do something like this

$(document).ready(function() {
     $("#menudiv").load("content.php", { key: 'link' });
     $("#newsdiv").load("content.php", { key: 'news' });

 });
Read more

How do I keep two divs that are side by side the same height?

Question by SLC

I have two divs side by side. I’d like the height of them to be the same, and stay the same if one of them resizes. I can’t figure this one out though. Ideas?

Edit: Here’s a mockup I did in notepad:

Edit: Updated to try the link suggested below, but still no luck.

Edit: To clarify my confusing question, I’d like both boxes to always be the same size, so if one grows because text is placed into it, the other one should grow to match the height.

<div style="overflow: hidden">

<div style="border:1px solid #cccccc; float:left; padding-bottom:1000px; margin-bottom:-1000px">

Some content!<br/>
Some content!<br/>
Some content!<br/>
Some content!<br/>
Some content!<br/>

</div>

<div style="border:1px solid #cccccc; float:left; padding-bottom:1000px; margin-bottom:-1000px">

Some content!

</div>

</div>

Answer by mqchen

This is a common problem which many have encountered, but luckily some smart minds have posted their solutions online: Ed Eliot’s blog, One true layout

Basically what you do is make both divs/columns very very tall by adding a padding-bottom: 1000px and then “trick the browser” into thinking they aren’t that tall using margin-bottom: -1000px. It is better explained by Ed Eliot on his blog, which also includes many examples.

Here’s an example for you.

Answer by Starx

You can use Jquery’s Equal Heights Plugin to accomplish, this plugins makes all the div of exact same height as other. If one of them grows and other will also grow.

Here a sample of implementation

Usage: $(object).equalHeights([minHeight], [maxHeight]);

Example 1: $(".cols").equalHeights(); 
           Sets all columns to the same height.

Example 2: $(".cols").equalHeights(400); 
           Sets all cols to at least 400px tall.

Example 3: $(".cols").equalHeights(100,300); 
           Cols are at least 100 but no more than 300 pixels tall. Elements with too much content will gain a scrollbar.

Here is the link

http://www.cssnewbie.com/equalheights-jquery-plugin/

Read more

to stop trigger in jquery

Question by AlexC

I have an AJAX call, which is doing this call every 5 seconds and when the call “succeed ” I have a trigger

 success: function (msg) {
        ...
        $('#test').trigger('click');
        return false;
    },
   ...

But i need to do this trigger just once , the first time, not every 5 second !

Can somebody suggest me how to stop this trigger, or maybe to use another stuff to trigger this “click “

Thanks!

Answer by RobertPitt

add a global variable outside the function to track the states

var trigger_triggered = false;

Somewhere in your ajax call

 success: function (msg) {
        ...
        if(trigger_triggered == false)
        {
            $('#test').trigger('click');
            trigger_triggered = true; //set it executed here
        }
        return false;
    },

Answer by Starx

Well, place the ajax call, in the $(document).ready(function() { }); it will only executes once your document is ready.

Or, when you do the ajax call, create a session flag to denote the script already being executed.

What I mean is

this is your ajax call page

<?
if(!isset($_SESSION['scriptflag']) or $_SESSION['scriftflag']==false) {
             //Your action
             $_SESSION['scriptflag'] = true; 
}
?>

get the point

Read more

How to rip specific data from an HTML file?

Question by SnirD

so I have multiple HTML files which I need to get some praticular data from, I mean theres a bunch of non relative information in this HTML files, but I need just lets say things that are between the <div class="myInfo"> and </div>, after getting this information I want to handle it in my own PHP page, so for example I can insert this data into some variables. Is it even possible using PHP only?
(Forgive me for my English mistakes)

Answer by RobertPitt

I would use SimpleDom

http://simplehtmldom.sourceforge.net/

// Find all article blocks
foreach(file_get_html('http://smysite.com')->find('div.myInfo') as $Info) {
    print_r($Info);
}

Alternative.
http://php.net/manual/en/book.simplexml.php

Answer by Starx

I think you need to use CURL for that

Here is a link
http://www.php.net/manual/en/book.curl.php

Read more
June 4, 2010

Is using AMP software like WAMP, MAMP, XAMPP a good idea?

Question by Starx

I know that softwares like WAMP, XAMPP save a lot of difficult configuration time. But, besides this, Is this a good idea? I, on the other would like to stick to traditional method and install each component seperately and use them.

Answer by Starx

Of course it’s a good idea. Easy configuration means, saved time. Saved time means, projects complete fast. Fast completion of projects means more projects and More projects mean more money. 🙂 lol

Read more
June 3, 2010

need to run php script when form is submitted

Question by Brad

I have a form that needs to run a php script once the submit button is clicked, it needs to be ajax.

<form method="post" action="index.php" id="entryform" name="entryform">
<input type="submit" name="submit" value="Submit" onclick="JavaScript:xmlhttpPost('/web/ee_web/include/email-notification.php', 'entryform')" />
</form>

In this situation, using if(form posted) { get results and run script } is not an option, I need to run the script externally, that is why I need it to execute the email-notification.php at onclick

When I point my web browser to domain.com/include/email-notification.php – it runs perfectly.

Any help is appreciated.

This executes the script before you submit the form, now I need to wait to execute the script once the submit button is clicked, possible to do this?

$.ajax({
    type: "POST",
    url: "/include/email-notification.php",
    dataType: "script"
});

Answer by KyleFarris

Check out the $.ajax() function of the jQuery javascript library. It will make you life much much easier and is quite minimal as far as added size to your page (like 12kb or something like that).

Answer by Starx

I suggest using jquery. because of its flexibility and conveinience

If you want to send email notification and then only post the data then do as following:

create a separate page to handle your email notification like email.php

// fill in headers and prepare content
$result = mail(............................);
if($result) { return 1; } else { return 0; }

Then on your form

$('#mysubmitbutton').click(function() {
      $.post(
           'email.php',
           { emailadd: 'my@email.com', content: 'mycontent' }, //send parameters to email.php
           function(data) {
                //now check whether mail sending was successfull
                if(data==1) {
                     //message sending was successful so carry out further operation
                     //.................................
                }
                else { alert("Sorry, email notification was unsuccessfull"); }
           });
});
Read more

How to fetch the value of selected checkbox inside a checkboxList?

Question by Starx

I want to know the selected value of the markup below. So that I can disabled a textbox, if one of the checkbox is selected.

    <asp:CheckBoxList ID="ChkTest" runat="server" RepeatDirection="Horizontal" CssClass="toggleYesNo">
        <asp:ListItem Value="1">Yes</asp:ListItem>
        <asp:ListItem Value="0">No</asp:ListItem>
    </asp:CheckBoxList>

I tried using this function it doesnot seem to work

$(document).ready(function() {
    $("#<%=ChkTest.ClientID %>").click(function() {
        value = $(this).val();
        if(value=='1') {
            $('#atextbox').attr('disabled','');
         }
         else {
            $('#atextbox').attr('disabled','disabled');
         }           

    });
});

I also track the output HTML but the id the CheckBoxList the assigned to a table instead.

UPDATED

<table id="ChkTest" class="toggleYesNo" border="0">
    <tr>
        <td><input id="ChkTest_0" type="checkbox" name="ChkTest$0" /><label for="ChkTest_0">Yes</label></td><td><input id="ChkTest_1" type="checkbox" name="ChkTest$1" /><label for="ChkTest_1">No</label></td>
    </tr>
</table>

Answer by Starx

Ok, I solved it

my jQuery function is

$(document).ready(function() {
    $("#<%=ChkTest.ClientID %> input").click(function() {
        value = $(this).attr('checked');
        if(value==true) {
            $("#TxtName").removeAttr("disabled");
        }
        else {
            $("#TxtName").val('');
            $("#TxtName").attr("disabled","disabled");
        }    
    });
});

This totally solved it

Read more

Hide a div after some time if no activity occurs on that div

Question by snell

I Am trying to hide a div when the user doesn’t click any button on it for 15 seconds.

I know how to hide and display the div by using element.style.display = 'none'; but I am not able to understand how to calculate the time after last activity on the div.

Answer by Starx

If you are familier with jquery, there is the plugin known as jquery timer(the link is below), which is going to enable you run certain code in regular interval of time.

Whenever a page or a div is initiated, setup a session, and keep the time in the session variable like $_SESSION['checkedtime'] = time();

Now use the jquery timer to sent a psot request to a file which checks the value and return the activity status a basic example of that page is like this

check.php

$oldtime = $_SESSION['checkedtime'];
if($oldtime<time()-15) { return 0; }
else { return 1; }

to setup a time function see the demo page

http://www.evanbyrne.com/article/jquery-timer-plugin

Read more
June 2, 2010

What is the minimum screen resolution for which you guys currently optimize your web designs?

Question by digitalis

I don’t really know if I’m asking in the right place, so if my question has to be transferred I apologize for it. I am totally noob in this place.

Thank you.

Answer by Josh K

Whatever I’m working on.

Really I try to fit everything on about 900px. 1024×768 is very much the norm, and the most common resolution today. I wouldn’t go past it without a very good reason too.

Answer by Starx

I suggest

750px if targeting 800 x 600 px

950px if targeting 1024 x 768 px

1220px if targeting 1280 x 1024 px

Whatever system you are targeting, make sure you leave at least 50px gap for the scroll bar and sidebar(in some browsers)

Read more
...

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