/    Sign up×
Community /Pin to ProfileBookmark

Show Div when page is submitted to itself

I have hidden divs on a page but I want them to appear when the form is submitted after the reload. Can javascript do this?

Does anyone have a tutorial or point to the right direction where I can get this done?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@MikeOSSep 16.2010 — The code below shows you one way how this can be done, however unless you have to use JavaScript I'd suggest using server side technology which can do this much simpler. Anyway the example below uses a cookie which is created when the user submits the form, the onload event on window checks to see if the cookie exists, if it does then the hidden div is shown.

<body>

<div style="visibility: hidden;" id="hiddenDiv">

<p>hidden div</p>

</div>

<form id="form1" onsubmit="submitform()">

<input type="submit" value="Send" id="sub" />

</form>

<script type="text/JavaScript">

function submitform() {

if(document.cookie.indexOf("div") == -1) {

document.cookie = "div=visibility";

}

}

window.onload = function() {

if(document.cookie.indexOf("div") != -1) {

document.getElementById("hiddenDiv").style.visibility = "visible";

}

}

</script>

</body>

Of course it won't work for those that deactivate cookies in their browser, but such people are extremely few and far between in my view.
Copy linkTweet thisAlerts:
@oasmatauthorSep 17.2010 — The scripts works but the problem is the cookies any way it can be programmed without the cookies?
Copy linkTweet thisAlerts:
@mavalos88Sep 18.2010 — If you are using a server side language then you'd simply do a check like so (in php)
[code=php]
if($_SERVER['REQUEST_METHOD'] == "POST")
echo(<div... )
[/code]


If not then you'd have to use GET. A variable in the URL that will tell you if the page was submitted or not. If the variable is set then show the div.
Copy linkTweet thisAlerts:
@MikeOSSep 18.2010 — The scripts works but the problem is the cookies any way it can be programmed without the cookies?[/QUOTE]

Yeah, AJAX would probably be a better way to go. My AJAX is a bit rusty so maybe someone else can provide a script for you.
×

Success!

Help @oasmat spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...