1

i am trying to pass a value form one div to another. the first div is generated by a for each loop and has a conditional statement that if the condition matches, the value is passed to second div and the second div is shown via a jquery popup window. here is my code:

<?php foreach ($value as $data) {
?>
<div class="center">
    <h4>Description:</h4>
    <?php 
        if(strlen($data['description'])<50)
        {
            echo $data['description'];
        }
        else
        {
            $str=$data['description'];
            $substr=substr($str,0,50);
            echo $substr.'<p class=button><a href=#>read more...<a/><p/>';
    ?>
    <?php }?>
</div>
<?php }?>
<div class="popupContact">
    <a class="popupContactClose">x</a>
    <h1>Description</h1>
    <p class="contactArea">
        <?php echo $str;?>
    </p>
</div>
<div class="backgroundPopup"></div>

here is my jquery popup

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
    //loads popup only if it is disabled
    if(popupStatus==0){
        $(".backgroundPopup").css({
            "opacity": "0.7"
        });
        $(".backgroundPopup").fadeIn("slow");
        $(".popupContact").fadeIn("slow");
        popupStatus = 1;
    }
}

//disabling popup with jQuery magic!
function disablePopup(){
    //disables popup only if it is enabled
    if(popupStatus==1){
        $(".backgroundPopup").fadeOut("slow");
        $(".popupContact").fadeOut("slow");
        popupStatus = 0;
    }
}

//centering popup
function centerPopup(){
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $(".popupContact").height();
    var popupWidth = $(".popupContact").width();
    //centering
    $(".popupContact").css({
        "position": "absolute",
        "top": windowHeight/2-popupHeight/3.25,
        "left": windowWidth/2-popupWidth/2
    });
    //only need force for IE6

    $(".backgroundPopup").css({
        "height": windowHeight
    });

}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){

    //LOADING POPUP
    //Click the button event!
    $(".button").click(function(){
        //centering with css
        centerPopup();
        //load popup
        loadPopup();
    });

    //CLOSING POPUP
    //Click the x event!
    $(".popupContactClose").click(function(){
        disablePopup();
    });
    //Click out event!
    $(".backgroundPopup").click(function(){
        disablePopup();
    });
    //Press Escape event!
    $(document).keypress(function(e){
        if(e.keyCode==27 && popupStatus==1){
            disablePopup();
        }
    });

});

now, as the first div is generated from a for each loop, i need to distinguish the read more.. link to determine which one of the read more is clicked and pass the appropriate $str to the second div. is it possible to pass the value via jquery? or is there any better way of doing it? it may sound confusing as i am weak with my words, but if someone do understand what i am saying please help.

Shabib
  • 1,697
  • 4
  • 20
  • 39
  • You should not be using the exact same "id" value for each `
    `, for one thing. The "id" values of elements should always be completely unique within a page. You could keep a counter and give "id" values made from "center_" plus the number, or something like that.
    – Pointy Feb 18 '12 at 15:09
  • how do you suggest to do that? – Shabib Feb 18 '12 at 15:19
  • Instead of having the "id" value be a constant, create it dynamically from a PHP variable in the loop. – Pointy Feb 18 '12 at 15:34

3 Answers3

2

Is it possible for you to do the DOM manipulation via jQuery instead? If so I would recommend changing to the following code:

$(document).ready(function () {
    $.each($value, function(index) {
        var desc = $value[index]["description"];
        var container = $('<div></div>').append('<h1>Description</h1>');
        if (desc.length < 50) {
            container.append(desc);
        } else {
            var anchor = $('<a href="#" class="button" onclick="return false;"></a>')
                //cache the rest of the description using .data()
                .data('desc', desc.substr(50, desc.length))
                .click(function () {
                    //using jQuery UI for a dialog
                    $('#genericDialog').html($(this).data('desc')).dialog();
                 })
                .text("Read more...").wrap('<p></p>');
            container.append(desc.substr(0, 50));
            container.append(anchor);
        }
        //append the container to the dom
        container.appendTo("#contactHolders");
    });
});

Here is the html I used:

<div id="contactHolders">
</div>
<div id="genericDialog"></div>

I made a fiddle here if you want to see this code working. Documentation for jQuery UI Dialogs is here.

sinemetu1
  • 1,726
  • 1
  • 13
  • 24
  • how do i pass the description value on the js? – Shabib Feb 20 '12 at 12:42
  • In this example I'm using [.data()](http://api.jquery.com/jQuery.data/) which allows me to store all the characters after the 50th one. – sinemetu1 Feb 20 '12 at 13:50
  • i meant how do i pass the $data[description] from php to jquery? – Shabib Feb 20 '12 at 13:56
  • Maybe check out these: http://groups.google.com/group/jquery-en/browse_thread/thread/010f76cc78d6eda2 or http://stackoverflow.com/questions/7776236/passing-data-from-php-to-jquery I haven't used PHP. That's why I was asking if you could just remove that PHP part and go with straight jQuery. – sinemetu1 Feb 20 '12 at 14:05
  • ok, thank you for your help. i liked your approach to solve this problem, that's why i am trying to go for it, but still no luck with passing value from php to jquery. anyways thank you. – Shabib Feb 20 '12 at 14:08
2

One of the simplest way to do, is to keep your long text in a hidden block, while you're in the foreach:

$str=$data['description'];
$substr=substr($str,0,50);
echo $substr.'<p class=button><a href=#>read more...<a/><p/>';
echo '<p class="full-desc">'.$str.'</p>';

then hide the p with css:

.full-desc{ display: none; }

and retrieve the value in javascript:

$(".button").click(function(){
    $(".contactArea").html($(this).next().text());
    //centering with css
    centerPopup();
    //load popup
    loadPopup();
});
CronosS
  • 3,129
  • 3
  • 21
  • 28
1

There are of course many ways to accomplish this if I understand your question right.

One solution is to use the html 5 data attributes to pass a value that identifies what link is pressed.

In your read more link you could add:

<p class=button data-descriptionId = [PHP code to write the Id]><a href=#>read more...<a/><p/>

Then in jQuery you can hook up the clicked event for the link, get the value and load content via an ajax call. I cannot see how you intend to load the data but it would look something like this:

$(document).ready(function(){

    $(".button").click(function(){
    event.preventDefault();
    var id = $(this).attr("data-decriptionId");

    //Make ajax call to the database see jQuery documentation for $.ajax passing inn the idvariable

});

    //set this as callback function to put the result in the second div

    function SetData(data){
    $(".contactArea").text(data); 
    }

});

To do the ajax call se documentation for it here

That should work.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
cfs
  • 1,304
  • 12
  • 30
  • well, finally your way of solution did my job. thanks a lot man :) @cfs – Shabib Feb 20 '12 at 14:37
  • 1
    @cfs Isn't calling `$(this).attr('data-descriptionId').val()` redundant? Calling `$(this).attr('data-descriptionId')` should return the value of that attribute. – sinemetu1 Feb 20 '12 at 18:53
  • @sgarret You're right about that. You don't need it, and the right thing is to just skip the .val(). I was typing it a bit fast. Thanks. – cfs Feb 20 '12 at 22:21