Jump to content

My code randomly stopped working...what gives?


Recommended Posts

I was on here a while back geting help with my code and everything finally got sorted out. Now I was told by a friend that it doesn't work anymore. I checked and it does not. The page that runs the INSERT is just bypassing all of my validations and not inserting anything into the database...it's basically just reloading that page...

 

Does anyone see anything wrong with this? It was working fine...

 

Thanks!

 

The code starts on this page (I took out all the extra html):

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

<div id="emailform">
          <form method="post" action="index.php"/>
<input type='text' value="" name="email" />
</div>
<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>

<?php
if ($_POST) {
    $emailaddy = $_POST['email'];
}
?>

 

Then goes to a popup window where the bulk of the code runs here:


<?php
session_start();
?>

<?php 

//CONNECT TO DATABASE

$dbhost = 'host';
$dbuser = 'user';
$dbpass = 'pw';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'marialist';

mysql_select_db($dbname);
$duplicate = 0;

print "<p>";

//IF THE SUBMIT BUTTON IS PRESSED, CHECK EACH POST FIELD FOR DATA & CONTINUE VALIDATION, OTHERWISE DISPLAY ERROR

if (isset($_POST['submit'])) {

if (!empty($_POST['email'])) {

//HERE WE ARE DECLARING VARAIBLES TO SEE IF THE EMAIL ADDRESS IS ALREADY ENTERED INTO THE DATABASE

$emailaddy = $_POST['email'];
$doubleaddycheck = "SELECT * FROM emaillist WHERE email = '$emailaddy'";
$doubleaddycheck = mysql_query($doubleaddycheck);
    	if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])) {
    			print "Please enter a valid e-mail address<br />";} 

//USING THOSE VARIABLES TO CHECK THE DATABASE

    		elseif (mysql_num_rows($doubleaddycheck) > 0) {
    			$duplicate = 1;
        		print "That e-mail address has already been added!<br />";
        		} 
       		else {
        		$emailaddy = $_POST['email'];
    }
} else {
  print "Please enter an e-mail address<br />";
}

if (!empty($_POST['firstname'])) {    

        $firstname = $_POST['firstname'];}
  else {
  		print "Please enter a first name.<br />";
}

if (!empty($_POST['lastname'])){

        $lastname = $_POST['lastname'];}
  else {
  		print "Please Enter a last name.<br />";
}

if ((strlen($emailaddy)>0) && (strlen($firstname)>0) && (strlen($lastname)>0) && ($duplicate)<1) {

$insertcontactdata = "INSERT INTO emaillist (email, firstname, lastname) VALUES ('$emailaddy', '$firstname', '$lastname')";

    mysql_query($insertcontactdata);
    print "Welcome to the list<b> $firstname! </b><br />\n";
    print "Your e-mail address: <b> $emailaddy </b>will be included in future e-mails.";
}
}

print "</p>";

mysql_close($conn);
?>

<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 />

No, this isnt a browser issue as I am using the same browser as before and it's not working. The popup comes up no problem, it's the php /mysql code not inserting data into the table, and I don't understand why this would randomly stop after it was working fine...

Run print_r() on $_POST on the second page.  I think the POST values aren't being passed to the second window.

 

It doesnt show anything with print_r(), however the only value being passed from page one to page two is the email address that is entered into the form, and that is working. The first and last name and email address are all actually submitted to the db from the second page...

weird...ok cool, well thats reassuring! Unfortunately, I think if I take that reload=1 out of there, the email address will not carry over to the popup until the page has been reloaded once. Do you know what I'm talking about? I came across this issue before too.

You could simply make the form submit with the reloaded=1 on there:

 

<form action="somepage.php?reloaded=1" method="POST">

 

 

I should mention, by the way, that this only works on POST forms.  On a GET submitted form, you would have to use a hidden field.

wait, I'm sorry, I don't see now where that reloaded=1 is at...are you talking about the javascript code itself or somewhere else? first or second page? I'm going to try and take out the js and do what you said but I cant even see where to take it out from...

<form method="post" action="mainsub.php?reloaded=1" />
<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 />

 

 

Then you won't need to remove the javascript.

that actually makes the page load to a blank white page after doing that...thats why I was asking where you see the reloaded=1 is at. I thought I knew, but I think I was thinking of something else...and I did a search in my code and it cannot find it anywhere..

Absolutely.

 

First Page:

 

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>The Bubble: A New Musical Dot Comedy</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css" />

<script type="text/javascript">

image1 = new Image();
image1.src = "images/homeselect.gif";

image2 = new Image();
image2.src = "images/castcrewselect.gif";

image3 = new Image();
image3.src = "images/mediaselect.gif";
</script>


<script type="text/javascript">

function wopen(url, name, w, h)
{
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="newcontainer">

<div id="emailheader">
	<div id="emailform">
	<form method="post" action="castcrew.php"/>
	<input type='text' value="" name="email" />

                      <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 id="newfullcontent">
</div>

<div id="nav">
<a href="index.html" onmouseover="image1.src='images/homeselect.gif';"
onmouseout="image1.src='images/home.gif';">
<img name="image1" src="images/home.gif" border="0"></a>
<a href="castcrew.html"> <img src="images/castcrewselect.gif" border="0"></a>
<a href="media.html" onmouseover="image3.src='images/mediaselect.gif';"
onmouseout="image3.src='images/media.gif';">
<img name="image3" src="images/media.gif" border="0"></a>
</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();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Subscribe</title>
<link rel="stylesheet" type="text/css" href="bubblesub.css" />


<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> 

</head>
<body>

<?php 

//CONNECT TO DATABASE

$dbhost = 'host';
$dbuser = 'user';
$dbpass = 'pw';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'bubblelist';
mysql_select_db($dbname);

$duplicate = 0;

print "<p>";
//IF THE SUBMIT BUTTON IS PRESSED, CHECK EACH POST FIELD FOR DATA & CONTINUE VALIDATION, OTHERWISE DISPLAY ERROR

if (isset($_POST['submit'])) {

if (!empty($_POST['email'])) {
//HERE WE ARE DECLARING VARAIBLES TO SEE IF THE EMAIL ADDRESS IS ALREADY ENTERED INTO THE DATABASE
$emailaddy = $_POST['email'];
$doubleaddycheck = "SELECT * FROM bubbleemail WHERE email = '$emailaddy'";
$doubleaddycheck = mysql_query($doubleaddycheck);
    	if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])) {
    			print "Please enter a valid e-mail address<br />";} 
//AND HERE WE ARE USING THOSE VARIABLES TO CHECK THE DATABASE
    		elseif (mysql_num_rows($doubleaddycheck) > 0) {
    			$duplicate = 1;
        		print "That e-mail address has already been added!<br />";
        		} 
       		else {
        		$emailaddy = $_POST['email'];
    }
} else {
  print "Please enter an e-mail address<br />";
}

if (!empty($_POST['firstname'])) {    
        $firstname = $_POST['firstname'];}
  else {
  		print "Please enter a first name.<br />";
}

if (!empty($_POST['lastname'])){
        $lastname = $_POST['lastname'];}
  else {
  		print "Please Enter a last name.<br />";
}


if ((strlen($emailaddy)>0) && (strlen($firstname)>0) && (strlen($lastname)>0) && ($duplicate)<1) {

$insertcontactdata = "INSERT INTO bubbleemail (email, firstname, lastname) VALUES ('$emailaddy', '$firstname', '$lastname')";
    mysql_query($insertcontactdata);
    print "Welcome to the list<b> $firstname! </b><br />\n";
    print "Your e-mail address: <b> $emailaddy </b>will be included in future e-mails.";
}
}
print "</p>";
mysql_close($conn);

?>


<form method="post" action="mainsub.php?reloaded=1" />
<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 />

</body>
</html>

Hey sorry....  Hurricane left overs knocked out some stuff here, so I haven't been able to get on the Internet.

 

Anyway, I would do it like this:

 

<?php
session_start();

//errrr.... You weren't checking if the POST variable was set or not...
if($_POST) {
$_SESSION['emailform'] = (isset($_POST['email'])) ? trim($_POST['email']) : null;
}

//You shouldn't have a DOCTYPE that's not at the very top....  Maybe I'm just paranoid ;p
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>The Bubble: A New Musical Dot Comedy</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css" />

<script type="text/javascript">

image1 = new Image();
image1.src = "images/homeselect.gif";

image2 = new Image();
image2.src = "images/castcrewselect.gif";

image3 = new Image();
image3.src = "images/mediaselect.gif";
</script>


<script type="text/javascript">

function HandleEmail() {
wopen('mainsub.php', 'email_popup', 400, 375);
document.email_form.submit();
}

function wopen(url, name, w, h)
{
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="newcontainer">

<div id="emailheader">
	<div id="emailform">
	<form method="post" action="castcrew.php" target="email_popup" onsubmit="HandleEmail(); return false;" name="email_form">
		<input type='text' value="" name="email" />
		<input type="submit" value="" class="button" onmouseover="this.className='button-over';" onmouseout="this.className='button';" />
	</form>
	</div>
</div>

<div id="newfullcontent">
</div>

<div id="nav">
<a href="index.html" onmouseover="image1.src='images/homeselect.gif';"
onmouseout="image1.src='images/home.gif';">
<img name="image1" src="images/home.gif" border="0"></a>
<a href="castcrew.html"> <img src="images/castcrewselect.gif" border="0"></a>
<a href="media.html" onmouseover="image3.src='images/mediaselect.gif';"
onmouseout="image3.src='images/media.gif';">
<img name="image3" src="images/media.gif" border="0"></a>
</div>

</div>

<?php

//Why would you have this at the end of the page?  You never use $emailaddy.
//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>

 

 

<?php
session_start();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Subscribe</title>
<link rel="stylesheet" type="text/css" href="bubblesub.css" />


<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> 

</head>
<body>

<?php

$duplicate = 0;

print "<p>";
//IF THE SUBMIT BUTTON IS PRESSED, CHECK EACH POST FIELD FOR DATA & CONTINUE VALIDATION, OTHERWISE DISPLAY ERROR

if (isset($_POST['submit'])) {

//I wouldn't connect to the DB unless you know you're going to need it.  waste of time connecting before they submit.

//CONNECT TO DATABASE

$dbhost = 'host';
$dbuser = 'user';
$dbpass = 'pw';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'bubblelist';
mysql_select_db($dbname);

if (!empty($_POST['email'])) {
	//HERE WE ARE DECLARING VARAIBLES TO SEE IF THE EMAIL ADDRESS IS ALREADY ENTERED INTO THE DATABASE
	$emailaddy = $_POST['email'];
	$doubleaddycheck = "SELECT * FROM bubbleemail WHERE email = '$emailaddy'";
	$doubleaddycheck = mysql_query($doubleaddycheck);
	if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])) {
    		print "Please enter a valid e-mail address<br />";
	} 
    	elseif (mysql_num_rows($doubleaddycheck) > 0) {
		//AND HERE WE ARE USING THOSE VARIABLES TO CHECK THE DATABASE
    		$duplicate = 1;
        	print "That e-mail address has already been added!<br />";
        } 
       	else {
		$emailaddy = $_POST['email'];
	}
} else {
	print "Please enter an e-mail address<br />";
}

if (!empty($_POST['firstname'])) {    
        $firstname = $_POST['firstname'];}
  else {
  		print "Please enter a first name.<br />";
}

if (!empty($_POST['lastname'])){
        $lastname = $_POST['lastname'];}
  else {
  		print "Please Enter a last name.<br />";
}


if ((strlen($emailaddy)>0) && (strlen($firstname)>0) && (strlen($lastname)>0) && ($duplicate)<1) {
	//unless your server has magic quotes on (which shouldn't be used), you need to be sanitizing SQL stuff....
	$insertcontactdata = "INSERT INTO bubbleemail (email, firstname, lastname) VALUES ('".mysql_real_escape_string($emailaddy)."', '".mysql_real_escape_string($firstname)."', '".mysql_real_escape_string($lastname)."')";
	if(mysql_query($insertcontactdata)) {
		print "Welcome to the list<b> $firstname! </b><br />\n";
		print "Your e-mail address: <b> $emailaddy </b>will be included in future e-mails.";
	}
}
mysql_close($conn);
}
print "</p>";


?>


<form method="post" action="mainsub.php?reloaded=true" >
<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 />
</form>
</body>
</html>

Errrr....  PHP doesn't work like that.  HTTP is a 'stateless' protocol, meaning one request is considered entirely unrelated to all previous ones (cookies provide a way to tell someone is the same client, and sessions are built on cookies [or GET variables]).

 

 

GET, POST, COOKIE, and sessions are essentially the only ways to transfer data from one page to another.

 

For example:

 

page1.php

$x = 5;

 

page2.php

//x isn't set on this page

Well...your code is cleaner definitely, and it does carry my email address over to the popup like I want, however when I submit all the info it is still reloading that page to a blank white page. This was the problem I was having before I posted the full code...It's obviously a php error...maybe I'm missing a semicolon  ???  Looking through to see if I see anything

Try:

 

 

<?php
session_start();
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Subscribe</title>
<link rel="stylesheet" type="text/css" href="bubblesub.css" />

</head>
<body>

<?php

$duplicate = 0;

print "<p>";
//IF THE SUBMIT BUTTON IS PRESSED, CHECK EACH POST FIELD FOR DATA & CONTINUE VALIDATION, OTHERWISE DISPLAY ERROR

if (isset($_POST['submit'])) {

//I wouldn't connect to the DB unless you know you're going to need it.  waste of time connecting before they submit.

//CONNECT TO DATABASE

$dbhost = 'host';
$dbuser = 'user';
$dbpass = 'pw';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'bubblelist';
mysql_select_db($dbname);

if (!empty($_POST['email'])) {
	//HERE WE ARE DECLARING VARAIBLES TO SEE IF THE EMAIL ADDRESS IS ALREADY ENTERED INTO THE DATABASE
	$emailaddy = $_POST['email'];
	$doubleaddycheck = "SELECT * FROM bubbleemail WHERE email = '$emailaddy'";
	$doubleaddycheck = mysql_query($doubleaddycheck);
	if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])) {
    		print "Please enter a valid e-mail address<br />";
	} 
    	elseif (mysql_num_rows($doubleaddycheck) > 0) {
		//AND HERE WE ARE USING THOSE VARIABLES TO CHECK THE DATABASE
    		$duplicate = 1;
        	print "That e-mail address has already been added!<br />";
        } 
       	else {
		$emailaddy = $_POST['email'];
	}
} else {
	print "Please enter an e-mail address<br />";
}

if (!empty($_POST['firstname'])) {    
        $firstname = $_POST['firstname'];}
  else {
  		print "Please enter a first name.<br />";
}

if (!empty($_POST['lastname'])){
        $lastname = $_POST['lastname'];}
  else {
  		print "Please Enter a last name.<br />";
}


if ((strlen($emailaddy)>0) && (strlen($firstname)>0) && (strlen($lastname)>0) && ($duplicate)<1) {
	//unless your server has magic quotes on (which shouldn't be used), you need to be sanitizing SQL stuff....
	$insertcontactdata = "INSERT INTO bubbleemail (email, firstname, lastname) VALUES ('".mysql_real_escape_string($emailaddy)."', '".mysql_real_escape_string($firstname)."', '".mysql_real_escape_string($lastname)."')";
	if(mysql_query($insertcontactdata)) {
		print "Welcome to the list<b> $firstname! </b><br />\n";
		print "Your e-mail address: <b> $emailaddy </b>will be included in future e-mails.";
	}
}
mysql_close($conn);
}
print "</p>";


?>


<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 "{$_POST['emailform']}"; ?>" name = "email" /><br /></p>
<br />
<input type='submit' value="Submit!" name="submit" /><br />
</form>
</body>
</html>

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.