Ridiculous, easy problem.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Steji
Forum Newbie
Posts: 19
Joined: Fri Jun 18, 2010 6:06 am

Ridiculous, easy problem.

Post by Steji »

I'm having a nightmare with this. It's a simple form using the POST method but the script on the other side is not picking any of the variables up that have been sent. Can anyone tell me why?

This is the header.php file containing the form, which is included by the index.php.

Code: Select all

<form action="password.php" method="post" name="loginform" id="loginform" onSubmit="return valid(this);">
    <table width="155" border="0" cellspacing="0" cellpadding="0" class="login">
      <tr>
        <td colspan="2">
           <input name="email" type="text" id="email"/>
        </td>
      </tr>
      <tr>
        <td height="20" colspan="2">
	<input name="pass" type="password" />
        </td>
	<td><input type="submit" name="loginbutton" id="loginbutton" value="Login" /></td>
      </tr>
      <tr>
        <?php
	if($_SESSION['cid']=="" and !isset($_SESSION['login']))
	{?>
		<!--<td width="66" align="left"><a href="javascript:submitForm();"><img src="images/login-button.gif" alt="Login" width="60" height="22" /></a></td>-->
		
		<td width="89" align="right"><img src="images/register.gif" alt="Register" width="83" height="22" /></td>
	<?php 
	} else 
	{ ?>
		<td width="66" align="left"><a href="myaccount.php"><img src="images/myaccount.gif" alt="My account" width="60" height="22" /></a></td>
		<td width="89" align="right"><img src="images/logout.gif" alt="Logout" width="83" height="22" /></td>
	<?php
	} ?>
	<td><input type="hidden" name="redirect" value="<?=$_GET["goto"];?>" /></td>
      </tr>
    </table>
</form>
And here is the script it is being sent to, password.php.

Code: Select all

  include("connection.inc.php");
  $emailnew=$_POST['email'];
 if(isset($_POST['loginbutton'])) {
	echo "submit works";
}
else {
	echo "login is not working";
}
 echo $_POST['email'];
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Ridiculous, easy problem.

Post by Jade »

Have you tried doing print_r($_POST); in the password.php page?
Steji
Forum Newbie
Posts: 19
Joined: Fri Jun 18, 2010 6:06 am

Re: Ridiculous, easy problem.

Post by Steji »

Jade wrote:Have you tried doing print_r($_POST); in the password.php page?
That returns Array ( )

??
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Ridiculous, easy problem.

Post by AbraCadaver »

Could be some problem in the javascript function valid(). Try removing the onSubmit="return valid(this);" to isolate.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Steji
Forum Newbie
Posts: 19
Joined: Fri Jun 18, 2010 6:06 am

Re: Ridiculous, easy problem.

Post by Steji »

AbraCadaver wrote:Could be some problem in the javascript function valid(). Try removing the onSubmit="return valid(this);" to isolate.
Good thinking, but its still the same.

Array ( ) login is not working
Warning: Cannot modify header information - headers already sent by (output started at D:\inetpub\vhosts\northerngraphic.co.uk\httpdocs\password.php:4) in D:\inetpub\vhosts\northerngraphic.co.uk\httpdocs\password.php on line 124
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Ridiculous, easy problem.

Post by AbraCadaver »

Steji wrote:
AbraCadaver wrote:Could be some problem in the javascript function valid(). Try removing the onSubmit="return valid(this);" to isolate.
Good thinking, but its still the same.

Array ( ) login is not working
Warning: Cannot modify header information - headers already sent by (output started at D:\inetpub\vhosts\northerngraphic.co.uk\httpdocs\password.php:4) in D:\inetpub\vhosts\northerngraphic.co.uk\httpdocs\password.php on line 124
We'd need to see all of the password.php code to tell.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Steji
Forum Newbie
Posts: 19
Joined: Fri Jun 18, 2010 6:06 am

Re: Ridiculous, easy problem.

Post by Steji »

AbraCadaver wrote:
Steji wrote:
AbraCadaver wrote:Could be some problem in the javascript function valid(). Try removing the onSubmit="return valid(this);" to isolate.
Good thinking, but its still the same.

Array ( ) login is not working
Warning: Cannot modify header information - headers already sent by (output started at D:\inetpub\vhosts\northerngraphic.co.uk\httpdocs\password.php:4) in D:\inetpub\vhosts\northerngraphic.co.uk\httpdocs\password.php on line 124
We'd need to see all of the password.php code to tell.
Without trying to echo the variables it just sends me back to the index with err=1 in the url.

Code: Select all

<?
  include("connection.inc.php");
  $emailnew=$_POST['email'];
  print_r($_POST);
 if(isset($_POST['loginbutton'])) {
	echo "submit works";
}
else {
	echo "login is not working";
}
 echo $_POST['email'];
 
  $regemail = stripslashes($_GET['regemail']);
  $regpass = stripslashes($_GET['regpass']);

  $page = "";
  if (isset($_POST['goto'])) {
 	 $page = base64_decode($_POST['goto']);
	} else if (isset($_GET['goto'])) {
 	 $page = base64_decode($_GET['goto']);
	}
  if($_POST['email']=="")
  {
  		if($_POST['email2']!="")
		{
			$emailnew = $_POST['email2'];
		}
  }
  $pass=$_POST['pass'];
  if($_POST['pass']=="")
  {
  		if($_POST['pass2']!="")
		{
			$pass = $_POST['pass2'];
		}
  }
  $remember = $_POST['rememberme'];
  if($regemail !="")
  {
  	$emailnew = $regemail;
  }
  if($regpass !="")
  {
  	$pass = $regpass;
  } 
 
  $query="select * from registration where email='$emailnew' and pass_word='$pass'";
  $result=mysql_query($query);
  $total = mysql_num_rows($result);
     if($total>0)
	  {
		 echo "here";
	  	$row=mysql_fetch_array($result);
		
		if($row['customer_status']=='0')
		{
			/*if($_REQUEST["productid"]!="")
			{
				$_SESSION['login'] = true;
				$_SESSION['emailaddress'] = $row["email"];
				$_SESSION['cid'] = $row["id"];
				if ($remember == "checked") {  //Remember user
					setcookie(ini_get("session.name"),session_id(),time()+(84600*30*12));
				}
				
				header("location: item-details.php?prid=".$_REQUEST["productid"]);
				exit;
			}*/
			  $_SESSION['login'] = true;
			  $_SESSION['emailaddress'] = $row["email"];
			  $_SESSION['cid'] = $row["id"];
			  if ($remember == "checked") {  //Remember user
					//setcookie(ini_get("session.name"),session_id(),time()+(84600*30*12));
					$cookie_time = (3600 * 24 * 30); // 30 days
					$cookie_name = 'siteAuth';
					$password_hash = md5($pass); // will result in a 32 characters hash
					setcookie ($cookie_name, 'usr='.$emailnew.'&hash='.$password_hash, time() + $cookie_time);
				}
			if($_POST['redirect']!="")
			{
				header("location:".base64_decode($_POST['redirect']));
				exit;
			}
			elseif($page!="")
			{
				header("Location:$page");
				exit;
			}
			elseif($regemail!="")
			{
				header("Location: register-success.php?message=1");
				exit;
			}
			
			else
			{	
				  
				  header("Location: myaccount.php");
				  exit;
			}			
		}
		else
		{
			if($_REQUEST["page"]!="")
			{
				header("Location: login.php?err=3");	
				exit;		
			}
			else
			{
				header("Location: index.php?err=4");
				exit;
			}
		}	  
 	  }
	   else
	  {
	  		if($_REQUEST["page"]!="")
			{
				header("Location: login.php?err=2");			
			}
			else
			{
				header("Location: index.php?err=1");
			}
	  }
  
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Ridiculous, easy problem.

Post by pickle »

Steji wrote:
Jade wrote:Have you tried doing print_r($_POST); in the password.php page?
That returns Array ( )

??
That means none of the form variables are being sent. $_POST is always present, and will always be an array. Only if a form is submitted will $_POST be populated.

Is the form being submitted at all? What happens if you try to reload the page after you submit the form? Does it ask you to resubmit form data or does it happily reload? If it doesn't ask you to resubmit, then your form isn't being submitted in the first place.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Steji
Forum Newbie
Posts: 19
Joined: Fri Jun 18, 2010 6:06 am

Re: Ridiculous, easy problem.

Post by Steji »

pickle wrote:
Steji wrote:
Jade wrote:Have you tried doing print_r($_POST); in the password.php page?
That returns Array ( )

??
That means none of the form variables are being sent. $_POST is always present, and will always be an array. Only if a form is submitted will $_POST be populated.

Is the form being submitted at all? What happens if you try to reload the page after you submit the form? Does it ask you to resubmit form data or does it happily reload? If it doesn't ask you to resubmit, then your form isn't being submitted in the first place.
It mustn't be being submitted because it isn't asking me to resend the data. Why would my submit button not be submitting the data?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Ridiculous, easy problem.

Post by Jade »

Are you using nested <form> tags anywhere? for instance:

Code: Select all

<form action=post>
... form stuff here

<form action=post>
..more form stuff here
</form>

</form>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Ridiculous, easy problem.

Post by pickle »

You're using <a> elements to trigger form submission, not <input> elements. When the link is clicked, it is followed - regardless of what Javascript you have running. If you want to have the form submitted when you click the link, have your submitForm() function return boolean false.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Ridiculous, easy problem.

Post by Jonah Bron »

Steji wrote:Without trying to echo the variables it just sends me back to the index with err=1 in the url.

Code: Select all

<?
  include("connection.inc.php");
  $emailnew=$_POST['email'];
  print_r($_POST);
 if(isset($_POST['loginbutton'])) {
	echo "submit works";
}
else {
	echo "login is not working";
}
 echo $_POST['email'];
 
  $regemail = stripslashes($_GET['regemail']);
  $regpass = stripslashes($_GET['regpass']);

  $page = "";
  if (isset($_POST['goto'])) {
 	 $page = base64_decode($_POST['goto']);
	} else if (isset($_GET['goto'])) {
 	 $page = base64_decode($_GET['goto']);
	}
  if($_POST['email']=="")
  {
  		if($_POST['email2']!="")
		{
			$emailnew = $_POST['email2'];
		}
  }
  $pass=$_POST['pass'];
  if($_POST['pass']=="")
  {
  		if($_POST['pass2']!="")
		{
			$pass = $_POST['pass2'];
		}
  }
  $remember = $_POST['rememberme'];
  if($regemail !="")
  {
  	$emailnew = $regemail;
  }
  if($regpass !="")
  {
  	$pass = $regpass;
  } 
 
  $query="select * from registration where email='$emailnew' and pass_word='$pass'";
  $result=mysql_query($query);
  $total = mysql_num_rows($result);
     if($total>0)
	  {
		 echo "here";
	  	$row=mysql_fetch_array($result);
		
		if($row['customer_status']=='0')
		{
			/*if($_REQUEST["productid"]!="")
			{
				$_SESSION['login'] = true;
				$_SESSION['emailaddress'] = $row["email"];
				$_SESSION['cid'] = $row["id"];
				if ($remember == "checked") {  //Remember user
					setcookie(ini_get("session.name"),session_id(),time()+(84600*30*12));
				}
				
				header("location: item-details.php?prid=".$_REQUEST["productid"]);
				exit;
			}*/
			  $_SESSION['login'] = true;
			  $_SESSION['emailaddress'] = $row["email"];
			  $_SESSION['cid'] = $row["id"];
			  if ($remember == "checked") {  //Remember user
					//setcookie(ini_get("session.name"),session_id(),time()+(84600*30*12));
					$cookie_time = (3600 * 24 * 30); // 30 days
					$cookie_name = 'siteAuth';
					$password_hash = md5($pass); // will result in a 32 characters hash
					setcookie ($cookie_name, 'usr='.$emailnew.'&hash='.$password_hash, time() + $cookie_time);
				}
			if($_POST['redirect']!="")
			{
				header("location:".base64_decode($_POST['redirect']));
				exit;
			}
			elseif($page!="")
			{
				header("Location:$page");
				exit;
			}
			elseif($regemail!="")
			{
				header("Location: register-success.php?message=1");
				exit;
			}
			
			else
			{	
				  
				  header("Location: myaccount.php");
				  exit;
			}			
		}
		else
		{
			if($_REQUEST["page"]!="")
			{
				header("Location: login.php?err=3");	
				exit;		
			}
			else
			{
				header("Location: index.php?err=4");
				exit;
			}
		}	  
 	  }
	   else
	  {
	  		if($_REQUEST["page"]!="")
			{
				header("Location: login.php?err=2");			
			}
			else
			{
				header("Location: index.php?err=1");
			}
	  }
  
?>
That "headers already sent" error is being given because you have to set cookies and stuff before any output at all.
Steji
Forum Newbie
Posts: 19
Joined: Fri Jun 18, 2010 6:06 am

Re: Ridiculous, easy problem.

Post by Steji »

Jade wrote:Are you using nested <form> tags anywhere? for instance:

Code: Select all

<form action=post>
... form stuff here

<form action=post>
..more form stuff here
</form>

</form>
I'm not unfortunately.

pickle wrote:You're using <a> elements to trigger form submission, not <input> elements. When the link is clicked, it is followed - regardless of what Javascript you have running. If you want to have the form submitted when you click the link, have your submitForm() function return boolean false.
I'm not sure what you mean? I have:

Code: Select all

 <form action="password.php" method="post" name="loginform" id="loginform" onSubmit="return valid(this);">
<input name="email" type="text" id="email"/>
<input name="pass" type="password" />
<input type="submit" name="loginbutton" id="loginbutton" value="Login" />
The javascript one has been commented out for now until I can get the basic form working.
Jonah Bron wrote:
Steji wrote:Without trying to echo the variables it just sends me back to the index with err=1 in the url.

Code: Select all

<?
  include("connection.inc.php");
  $emailnew=$_POST['email'];
  print_r($_POST);
 if(isset($_POST['loginbutton'])) {
	echo "submit works";
}
else {
	echo "login is not working";
}
 echo $_POST['email'];
 
  $regemail = stripslashes($_GET['regemail']);
  $regpass = stripslashes($_GET['regpass']);

  $page = "";
  if (isset($_POST['goto'])) {
 	 $page = base64_decode($_POST['goto']);
	} else if (isset($_GET['goto'])) {
 	 $page = base64_decode($_GET['goto']);
	}
  if($_POST['email']=="")
  {
  		if($_POST['email2']!="")
		{
			$emailnew = $_POST['email2'];
		}
  }
  $pass=$_POST['pass'];
  if($_POST['pass']=="")
  {
  		if($_POST['pass2']!="")
		{
			$pass = $_POST['pass2'];
		}
  }
  $remember = $_POST['rememberme'];
  if($regemail !="")
  {
  	$emailnew = $regemail;
  }
  if($regpass !="")
  {
  	$pass = $regpass;
  } 
 
  $query="select * from registration where email='$emailnew' and pass_word='$pass'";
  $result=mysql_query($query);
  $total = mysql_num_rows($result);
     if($total>0)
	  {
		 echo "here";
	  	$row=mysql_fetch_array($result);
		
		if($row['customer_status']=='0')
		{
			/*if($_REQUEST["productid"]!="")
			{
				$_SESSION['login'] = true;
				$_SESSION['emailaddress'] = $row["email"];
				$_SESSION['cid'] = $row["id"];
				if ($remember == "checked") {  //Remember user
					setcookie(ini_get("session.name"),session_id(),time()+(84600*30*12));
				}
				
				header("location: item-details.php?prid=".$_REQUEST["productid"]);
				exit;
			}*/
			  $_SESSION['login'] = true;
			  $_SESSION['emailaddress'] = $row["email"];
			  $_SESSION['cid'] = $row["id"];
			  if ($remember == "checked") {  //Remember user
					//setcookie(ini_get("session.name"),session_id(),time()+(84600*30*12));
					$cookie_time = (3600 * 24 * 30); // 30 days
					$cookie_name = 'siteAuth';
					$password_hash = md5($pass); // will result in a 32 characters hash
					setcookie ($cookie_name, 'usr='.$emailnew.'&hash='.$password_hash, time() + $cookie_time);
				}
			if($_POST['redirect']!="")
			{
				header("location:".base64_decode($_POST['redirect']));
				exit;
			}
			elseif($page!="")
			{
				header("Location:$page");
				exit;
			}
			elseif($regemail!="")
			{
				header("Location: register-success.php?message=1");
				exit;
			}
			
			else
			{	
				  
				  header("Location: myaccount.php");
				  exit;
			}			
		}
		else
		{
			if($_REQUEST["page"]!="")
			{
				header("Location: login.php?err=3");	
				exit;		
			}
			else
			{
				header("Location: index.php?err=4");
				exit;
			}
		}	  
 	  }
	   else
	  {
	  		if($_REQUEST["page"]!="")
			{
				header("Location: login.php?err=2");			
			}
			else
			{
				header("Location: index.php?err=1");
			}
	  }
  
?>
That "headers already sent" error is being given because you have to set cookies and stuff before any output at all.
I think that will be solved when I get it working because it won't have any output, it's just for testing purposes?

This has got me completely baffled....
Steji
Forum Newbie
Posts: 19
Joined: Fri Jun 18, 2010 6:06 am

Re: Ridiculous, easy problem.

Post by Steji »

Ok, so I know there is nothing wrong with the form because if I change form action to testscript.php which contains the first few lines of password.php

Code: Select all

<?php

include("connection.inc.php");
 $emailnew=$_POST['email'];
 print_r($_POST);
 if(isset($_POST['loginbutton'])) {
	echo "submit works";
}
else {
	echo "login is not working";
}
 echo $_POST['email'];
 


?>
Then everything is fine and I get this output:

Code: Select all

Array ( [email] => test.test@test.com [pass] => password[loginbutton] => Login [redirect] => ) submit works test.test@test.com
So it has to be something to do with password.php
Steji
Forum Newbie
Posts: 19
Joined: Fri Jun 18, 2010 6:06 am

Re: Ridiculous, easy problem.

Post by Steji »

It works now. All I had to do is change the name from password.php to something else? Why could that be?
Post Reply