sunshinekitty
sunshinekitty

Reputation: 2375

Using $_GET with Jquery

I currently have the following code on my website:

$(document).ready(function() {
$("#contact").on("click", function(e)
{ 
    e.preventDefault();
    $("#contactform").toggle('fast');
});
});

I would like to have an if(isset($_GET['email')); trigger this function as well, so have it open on page load if the $_GET variable is set.

I'm rather new with Jquery and not sure if this is possible, I also have another somewhat related question, I'm not sure if I should make a new question for this as I'm fairly new to stackoverflow as well, but here it is.

Say I have two of these:

$(document).ready(function() {
$("#contact").on("click", function(e)
{ 
    e.preventDefault();
    $("#contactform").toggle('fast');
});
});

$(document).ready(function() {
$("#archivestop").on("click", function(e)
{ 
    e.preventDefault();
    $("#archives").toggle('fast');
});
});

I want one to close if the other one is opened, how would I go about this?

Thanks!

Upvotes: 0

Views: 494

Answers (3)

Neil Girardi
Neil Girardi

Reputation: 4913

Regarding your second question you can use the following to determine if an element is visible:

var bool = $('.foo').is(":visible");

So to hide an element if it is visible you would do something like this:

if ($('.foo').is(":visible")) {
    $('.foo').hide();
}

Upvotes: 1

Mr. B.
Mr. B.

Reputation: 8697

Here's the Javascript-solution:

function getParam(key) {
        var paramsStr = window.location.search.substr(1, window.location.search.length),
            paramsArr = paramsStr.split("&"),
            items     = [];

        for (var i = 0; i < paramsArr.length; i++) {
            items[paramsArr[i].split("=")[0]] = paramsArr[i].split("=")[1];
        }

        if (key != "" && key != undefined) {
            // return single
            if (items[key] != undefined) {
                return items[key];
            } else {
                return null;
            }
        } else {
            // return all (array)
            return items;
        }
    };

if (getParam("email")) { 
    //    ...
}

Upvotes: 2

sunshinekitty
sunshinekitty

Reputation: 2375

I'm silly and have answered my first question. I still have yet to have my coffee.

The following works, just insert it into the div that is to be displayed:

<div id="contactform" style="<?php if(isset($_POST['email'])) echo "display:block;" ?>">
content
</div>

Upvotes: 0

Related Questions