Jump to content

Trouble using sessions...


Kane250

Recommended Posts

I have a form that submits data (an email address) to a popup window where the user fills in the rest of their contact information and the e-mail address field is already prefilled with what they had typed on the original form.

 

I am using a session to carry that data into a new window. However, after this is done once, it cannot be done again. (ie: If I refresh the page, everything works fine, but after I use it once, the data will not carry into the new window without another refresh). Sometimes it won't even work the first time without a refresh on the page. How can I fix this so that the data always carries over?

 

My code is rather lengthy...I'll try and shorten it for you.

 

The session on the main page.

<?php
session_start();
$_SESSION['emailform'] = $_POST['email'];
?>

 

The popup window that is pulling the data from the main page. (mainsub.php)

<?php
session_start();
?>

<form method="post" action="mainsub.php" />

<p>	
First name: <input type='text' value="" name="firstname" /><br /><br />
Last name: <input type='text' value="" name="lastname" /><br /><br />
E-Mail Address: <input type='text' value="<?PHP echo "{$_SESSION['emailform']}"; ?>" name = "email" /><br /></p>
<br />
<input type='submit' value="Submit!" name="submit" /><br />

Link to comment
Share on other sites

A few things that might help.

 

- Are you starting the session on the page where the user first types their email address?

- Are you using Javascript to open your popup window? If so you need to change that to a form (if this is so say and I'll explain more)

- Post a sample of your first page (where the email is entered) as this may help

Link to comment
Share on other sites

A few things that might help.

 

- Are you starting the session on the page where the user first types their email address?

- Are you using Javascript to open your popup window? If so you need to change that to a form (if this is so say and I'll explain more)

- Post a sample of your first page (where the email is entered) as this may help

 

I am starting the session on that first page

YES, I am using javascript to make a popup window for the second page...explain?

You can actually see it in action here: www.mariaforcouncil09.com

Link to comment
Share on other sites

I think the error has something to do with the ordering of things.

 

When you submit the form, before it's loaded and the email is added to a session variable, the window is popped up. The form is then processed and the email added to the session variable. There are two ways you can fix this. Firstly, you could process then the form and then when the page has reloaded call the popup window JS function. Secondly, you could modify the Javascript function to tag what ever value has been entered into the input box onto the url that is being opened. For example rather than open mainsub.php open mainsub.php?email=testemail@hotmail.com. Then in mainsub.php extract the email from the URL and enter it into the form. Personally, I would go for the second one.

 

One thing to note though is that some people don't have Javascript enabled in their browsers because of security restrictions (public libaries, university's or just people who are anal about security). So it's worth noting that without Javascript these people will not be able to access your mailing list what so ever. I would recommend either when the email is entered, a whole seperate page is loaded in the main window or you find space for a name input field on the mainpage. With your site being based around a political campaign, every vote counts!

Link to comment
Share on other sites

Thanks so much! I'm gonna try and reorder things first because I'm not ultra savvy with php just yet, so I'm not too sure exactly how that second option should be written.

 

I agree with you about the javascipt thing actually, but the person in charge wanted a popup, so that's what they got!

 

Thanks for looking at it for me...hopefuly I can make this work now!

Link to comment
Share on other sites

Actually, I am having some trouble with reordering my code, and I'm not too sure why this isn't working.

 

Before this site was posted, I had a one page static page up with the exact same code and everything worked fine. Is there a reason why this would change when using multipe pages? Everything is exactly the same, even down to the popup window except that I was using a standard submit button before and now I'm using a rollover..

Link to comment
Share on other sites

Posting Shortened Code Just in Case this will help anyone help me! I need it!  :(

 

First Page

<?php
session_start();
$_SESSION['emailform'] = $_POST['email'];
?>

<head>

<script type="text/javascript">
<!--
function wopen(url, name, w, h)
{
// Fudge factors for window decoration space.
// In my tests these work well on all platforms & browsers.
w += 20;
h += -20;
var win = window.open(url,
  name,
  'width=' + w + ', height=' + h + ', ' +
  'location=no, menubar=no, ' +
  'status=no, toolbar=no, scrollbars=no, resizable=no');
win.resizeTo(w, h);
win.focus();
}
// -->
</script>
</head>

<body>
<div id="container">

<div id="emailform">
<form method="post" action="index.php"/>
<input type='text' value="" name="email" />
       <div id="emailcheck">
	<input type="submit" value="" class="button" onclick="wopen('mainsub.php', 'popup', 400, 375); return true;" onmouseover="this.className='button-over';" onmouseout="this.className='button';" />
	</div>
</div>
</div>

<?php

//WHEN FORM IS POSTED, VALIDATE THE E-MAIL FORM. IF ALL IS OK, ADD THE EMAIL TO THE VARIABLE.
if ($_POST) {
    $emailaddy = $_POST['email'];
}

?>

</body>
</html>

 

Popup Page

<?php
session_start();
?>

<head>
</head>

<body>
<form method="post" action="mainsub.php" />	

First name: <input type='text' value="" name="firstname" /><br /><br />
Last name: <input type='text' value="" name="lastname" /><br /><br />
E-Mail Address: <input type='text' value="<?PHP echo "{$_SESSION['emailform']}"; ?>" name = "email" /><br /></p>

<input type='submit' value="Submit!" name="submit" /><br />

</body>
</html>

Link to comment
Share on other sites

I've come to figure out that the problem lies in the popup window. If I reload the popup window after I submit, the data fills in correctly, but I have to reload. Each time I run it, it shows the old data until I reload the popup. So maybe this is simpler to help me with now? Is there a way to force the popup to automatically refresh the session?

 

I realize I'm talking to nobody but I'm hoping someone can chime in with help!

Link to comment
Share on other sites

The page currently refresh however it refreshes into a recursive loop. You're on the right tracks!

 

What I would do in this situation is you load the page mainsub.php but when you redirect, you redirect to mainsub.php?ref=1. Now each time mainsub.php loads up you say

 

<?
session_start();

// format ref as number. if less than 1 (ie. not set)
if (number_format($_GET['ref'], 0, "", "") < 1)
{
    header("mainsub.php?ref=1");
    exit;
}

 

This way, the first time the page loads, the ref value will not be set which will cause the above code to execute. Then, when you reload the page with ?ref=1 on the end, this code wont be displayed and the page will load.

Link to comment
Share on other sites

Thanks, I appreciate your reply and your code. That code is making my page get an error, an internal server error, and the mainsub page will not load at all actually...I'm trying to debug now...it looks correct but I'm unfamiliar with parts of it..

Link to comment
Share on other sites

Which parts are you unsure of? To be honest I'm not too sure what causing the Internal Server error, I'd have to see more of your code to have a guess.

 

If this doesn't work you could always try the Javascript way I mentioned earlier?

 

wopen('mainsub.php?email='+document.forms['formName'].email.value, 'popup', 100, 100);

Link to comment
Share on other sites

Okay, I found this online and it seems to be woring so far!

 

Lets hope it continues to work...any reason why it wouldn't?

 

Thanks again so much!

 

<script>
var reloaded = false;
var loc=""+document.location;
loc = loc.indexOf("?reloaded=")!=-1?loc.substring(loc.indexOf("?reloaded=")+10,loc.length):"";
loc = loc.indexOf("&")!=-1?loc.substring(0,loc.indexOf("&")):loc;
reloaded = loc!=""?(loc=="true"):reloaded;

function reloadOnceOnly() {
if (!reloaded)
window.location.replace(window.location+"?reloaded=true");
}
reloadOnceOnly();
</script> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.