...

View Full Version : PHP form submit's, $_POST empty



kareems
10-29-2009, 11:29 PM
For the life of me I can't figure out why all fields in $_POST array are unset. When I change method of form to GET it works perfectly, all information is appended to URL. Only POST fails. Any help would be greatly appreciated, frustration is mounting.

pre_php.php is a db connect script (it works)
pre_content.php is an html header (it also works).



<?php
include_once 'includes/pre_php.php';
$error_message="";
$clean = array();

if (isset($_POST['signin'])) {
$fields = array("username", "password");
foreach($fields as $field) {
if (isset($_POST[$field]) && !empty($_POST[$field])) {
$clean[$field] = trim($_POST[$field]);
} else {
$error_message.="Please enter your $field<br/>";
}
}

if ($error_message=="") {
$sql = "SELECT * FROM accounts WHERE username=? AND password=? LIMIT 1";
$st = $pdo->prepare($sql);
$values = array($clean["username"], $clean["password"]);
$st->execute($values);
if (($row=$st->fetch())!==false) {
$session = session_start();
if ($session) {
$_SESSION['username'] = $row["username"];
$_SESSION['first_name'] = $row["first_name"];
$_SESSION['last_name'] = $row["last_name"];
header("Location: index.php");
} else {
$error_message.="We could not sign you in. Please contact your ****** representative for assistance.<br/>";
}
} else {
$error_message.="Incorrect email/password combination.<br/>";
}
}
} else {
$error_message.="WHAT!"; /*this is for debugging, and it executes every time*/
}


include 'includes/pre_content.php';
?>

<div align="center">
<h2>Please sign in below.</h2>
<h3><?php print $error_message; ?></h3>
<div id="sign_in_form">
<form id="sign_in" name="sign_in" action="sign_in.php" method="post">
<div id="sign_in_fields">Username </div><input type="text" name="username"/>
<div id="clear"></div>
<div id="sign_in_fields">Password </div><input type="password" name="password"/>
<div id="clear"></div>
<input type="hidden" name="signin" value="signin"/>
<input type="submit" value="Sign in"/>
</form>
</div>
</div>

Phil Jackson
10-29-2009, 11:30 PM
method="get">


shopuld be "post"

kareems
10-29-2009, 11:36 PM
method="get">


shopuld be "post"

Sorry, that should have been "post". I changed it to test if "get" worked, it still doesn't work wil post.

Phil Jackson
10-29-2009, 11:42 PM
<input type="submit" value="Sign in"/>


should be


<input type="submit" value="Sign in" name="signin" />

kareems
10-29-2009, 11:49 PM
<input type="submit" value="Sign in"/>


should be


<input type="submit" value="Sign in" name="signin" />

Changed it, didn't do the trick.

Phil Jackson
10-29-2009, 11:51 PM
the page above is sign_in.php?

Phil Jackson
10-29-2009, 11:52 PM
remove this too

<input type="hidden" name="signin" value="signin" />

Phil Jackson
10-29-2009, 11:54 PM
put

print_r($_POST);

at the top and run it again

Phil Jackson
10-29-2009, 11:55 PM
include_once 'includes/pre_php.php';

would be

require_once("includes/pre_php.php");

Phil Jackson
10-29-2009, 11:56 PM
or include but brackets should be used

_Aerospace_Eng_
10-29-2009, 11:57 PM
or include but brackets should be used

Though not required.

kareems
10-29-2009, 11:59 PM
remove this too

<input type="hidden" name="signin" value="signin" />

Yes, the page above is sign_in.php (when I hit submit it goes to that page, and executes the else statement for the if(isset($_POST['signin']))

I tried removing the hidden field, that didn't work.

Also, I use the hidden field to know whether I should execute the script or not. It's useful when I have multiple forms on a page each performing a different action, so I can tell which form has been submitted. It's a method I've used many times in the past.

PappaJohn
10-30-2009, 12:00 AM
or include but brackets should be used
include, include_once, require & require_once are language constructs not functions. As such the parenthesis are optional.

Phil Jackson
10-30-2009, 12:01 AM
thats what the name in your submit button is for. did you add print_r($_POST); at top of the page?

Phil Jackson
10-30-2009, 12:02 AM
your not altering the post array in include_once 'includes/pre_php.php';
are you?

kareems
10-30-2009, 12:04 AM
include_once 'includes/pre_php.php';

would be

require_once("includes/pre_php.php");

I put print_r, output is "Array()"

So POST is empty, it would seem.

I have error_reporting to E_ALL so would still see a notice if file was not being included (only difference from 'require' that I'm aware of), but will try anyway.

I never use brackets with include, it still works.

Phil Jackson
10-30-2009, 12:06 AM
running php5 or 4

Phil Jackson
10-30-2009, 12:07 AM
dont know what host your on nor there settings.

for php5, create a file called php5.ini and put in
display_errors = 1

same for php 4 but file called php.ini

upload to root directory of your server.

kareems
10-30-2009, 12:08 AM
your not altering the post array in include_once 'includes/pre_php.php';
are you?


Nope. Here's pre_php.php and pre_content.php

pre_php.php


<?php

$db_server="********";
$db_user="******";
$db_pass="******";
$db_name="******";

$pdo = new PDO('mysql:host=' . $db_server . ';dbname=' . $db_name, $db_user, $db_pass);
$st = $pdo->query("SET NAMES 'utf8'"); // when you look in phpmyadmin, utf looks nice


?>


pre_content.php




<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
?>
<!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" />
<meta name="robots" content="noindex, nofollow" />
<meta http-equiv="Content-Language" content="en-us" />
<link rel="stylesheet" type="text/css" media="screen" charset="utf-8" href="css/screen.css" />
<script type="text/javascript" src="dojo/dojo.js" djConfig="parseOnLoad:true, isDebug:false"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.3/dijit/themes/tundra/tundra.css">
<script type="text/javascript">dojo.require("dijit.form.DateTextBox");</script>
<script type="text/javascript">dojo.require("dijit.form.CheckBox");</script>

<title>title</title>
</head>
<body class="tundra">
<div align="center">
<!-- Begin Header -->
<div id="header">
<div id="logo">
<a href="<?php print($web_prefix); ?>"><img src="images/logo.jpg"/></a>
</div>
<div id="right-title">
PAGE RIGHT TITLE
</div>
</div>

<!-- End Header -->
<div id="content" align="left">

Phil Jackson
10-30-2009, 12:08 AM
or put atr the top of script


echo $madeUpVar;

kareems
10-30-2009, 12:14 AM
running php5 or 4

PHP Version 5.2.6

errors seem to be displaying correctly, i've gotten numerous errors on this script today (all levels)

kareems
10-30-2009, 12:17 AM
or put atr the top of script


echo $madeUpVar;

Put it, got a notice of an undefined variable, so error reporting works.

Phil Jackson
10-30-2009, 12:19 AM
make a new page, blank apart from



<?php
print_r($_POST);
?>


alter form action to go there and see what it says...

kareems
10-30-2009, 12:24 AM
make a new page, blank apart from



<?php
print_r($_POST);
?>


alter form action to go there and see what it says...

It displayed the POST information correctly.

kareems
10-30-2009, 12:39 AM
make a new page, blank apart from



<?php
print_r($_POST);
?>


alter form action to go there and see what it says...

I'm sorry for wasting all of your time, I figured it out.

It was actually an error in another script. Upon successful login, when redirecting to index.php it failed to identify the session, so redirected back to sign_in.php (clearly with an empty $_POST).

Thanks for your help guys.

Phil Jackson
10-30-2009, 12:39 AM
just try:


<form id="sign_in" name="sign_in" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

cyclerider
04-26-2010, 07:30 PM
So many suggestions. Take a look through php form tutorials (http://*************/tutorial/tutorial.html).

Phil Jackson
04-26-2010, 07:33 PM
reported as spam



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum