...

Undefined Index problem

1andyw
07-20-2007, 08:12 PM
Hi,

Having an error returned:

Undefined index: fname in c:\apache\htdocs\andytest\formpost2.php</b> on line <b>6</b><br />
Undefined index: lname in c:\apache\htdocs\andytest\formpost2.php</b> on line <b>7</b><br />

My input formin2.php is:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Form In</title>
<script type="text/javascript">
var request = null;
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = null;
}
}
}

if (request == null)
alert("Error creating request object!");
function formpost(){
var fname = document.getElementById('fname');
var lname = document.getElementById('lname');
var url="formpost2.php";
request.open("POST",url,true);
request.setRequestHeader("Content-Type" , "application/x-www.form-urlencoded");
request.send("FirstName=" + escape(fname) + "&LastName= " + escape(lname));
}
</script>
<style type="text/css">
body{font:bold 100% "Trebuchet MS", sans-serif;
color: #666; background: #ccc;
}
legend{font-size: 150%;}
form{width:500px;margin:0 auto; color:#666; background: #fff; padding:3em;}
label{width:100px; margin-left:1em;}
.input{width:25em; margin-left: 110px;}
.button{width:15em; margin-left: 175px;}
</style>
</head>
<body>
<form method="post" action="">
<fieldset><legend>Customer Name</legend>
<label for = "fname">First Name</label><input type="text" class="input" size="25" name="fname" id="fname" /><br />
<label for = "lname">Last Name</label><input type="text" class="input" size="25" name="lname" id="lname" />
<input type="button" class="button" name="postbutton" value="Post This Record" id="postbutton" onclick="formpost()" />
</fieldset>
</form>
<div id="current"></div>
</body>
</html>

and the process file is formpost2.php

<?php
$domain="localhost";
$user="root";
$password="me";
$conn = mysql_connect( $domain, $user, $password );
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$rs=mysql_select_db("ajax", $conn) or die('Could not connect: ' .mysql_error());
$sql="INSERT INTO formin (`firstname`,`lastname`) VALUES ('$fname', '$lname')";
$rs=mysql_query($sql,$conn);
?>


The mysql table is simply:


db: ajax table: formin
firstname varchar(30) latin1
lastname varchar(30) latin1


Would someone straighten me out on this, please.

Thanks,

Andy

A1ien51
07-22-2007, 04:06 AM
Look at your send, you are not passing up fname and lname. You are passing up FirstName and LastName when your code is looking for fname and lname! The server code only knows what you send to it.

Eric

1andyw
07-22-2007, 11:18 AM
Hi Eric,

I made the changes is the 'send' action. The file 'formpost2.php' continues to return the same:
Notice: Undefined index: fname in c:\apache\htdocs\andytest\formpost2.php on line 6

Notice: Undefined index: lname in c:\apache\htdocs\andytest\formpost2.php on line 7


Another thought?

Andy

1andyw
07-23-2007, 07:08 PM
Resolved!

The problem has been identified. Thank you.

Andy

StupidRalph
07-24-2007, 12:17 AM
Are you going to share your solution with the rest of us?

1andyw
07-24-2007, 10:08 AM
Hi Ralph,

This is still a work in process but this is where I am at now:

submit form:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Registration Form</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript">
var AdminResponse="";

function parseResponse(){
var nMessage=document.getElementById('echoMsg');
nMessage.style.display='';
nMessage.innerHTML="Last entry recorded is " + AdminResponse ;
}
function registerUser(){

var AdminRequest="";
AdminRequest=new XMLHttpRequest();
AdminRequest.onreadystatechange=function()
{
if(AdminRequest.readyState==4)
{
if(AdminRequest.status==200)
{
AdminResponse=AdminRequest.responseText
parseResponse();
}
else {
alert('Error post.php File'+AdminRequest.statusText)
}
}
}
var nForm=document.forms[0];
var infoStr="fname=" + nForm['firstName'].value;
infoStr += "&lastname=" + nForm['lastName'].value;
AdminRequest.open("POST","post.php", true);
AdminRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
AdminRequest.send(infoStr);
}


</script>
<style type="text/css">
body{font:bold 100% "Trebuchet MS", sans-serif;
color: #666; background: #ccc;
}
legend{font-size: 150%;}
form{width:500px;margin:0 auto; color:#666; background: #fff; padding:3em;}
label{width:100px; margin-left:1em;}
.input{width:25em; margin-left: 110px;}
.button{width:15em; margin-left: 175px;}
</style>
<body>
<form action="">
<fieldset><legend>Registration Information</legend>
<label for="firstName">First Name</label><input type='text' class='input' name='firstName' id='firstName'>
<label for="lastName">Last Name</label><input type='text' class='input' name='lastName' id='lastName'>
<input type='button' name='submit' value='Post This Information' class='button' onclick="registerUser()">
</fieldset>
<div id="echoMsg"></div>
</form>
</body></html>


and process file:

<?php
$message = "First Name - " . $_POST['fname'] . "<br>";
$message .= "Last name - " . $_POST['lastname'];
echo stripslashes($message);
$fname= $_POST['fname'];
$lastname= $_POST['lastname'];
$domain="localhost";
$user="root";
$password="andrew";
$conn = mysql_connect( $domain, $user, $password );
$rs=mysql_select_db("ajax", $conn) or die('Could not connect: ' .mysql_error());
$sql="INSERT INTO formpost (`firstname`,`lastname`) VALUES ('$fname','$lastname')";
$rs=mysql_query($sql,$conn);
?>

This is working fine up to this point.

Andy

1andyw
07-24-2007, 03:43 PM
Ralph,

The lesson I learned from this is to use form reference to form elements. I had used document.getElementById initially and changed that to form[0].['fname'].value to make it work.

Also, I changed the doc type from xhtml1.0 trans to html 4.01 trans. Doing this stopped the errors firebug was throwing, but I don't understand the difference.

I got that idea from http://forums.tizag.com/showthread.php?t=3467&page=2.

Ralph, I have a long way to go on this project so any comment from you will be appreciated

Andy

StupidRalph
07-24-2007, 11:47 PM
<label for="firstName">First Name</label><input type='text' class='input' name='firstName' id='firstName'>
<label for="lastName">Last Name</label><input type='text' class='input' name='lastName' id='lastName'>


form[0].['fname'].value


$fname= $_POST['fname'];
$lastname= $_POST['lastname'];

Does that work? It seems as if it shouldn't since PHP is expecting an element named $_POST['firstName'] and not $_POST['fname'].

I think the getElementById should have worked...hmm what browser is that?:confused:

1andyw
07-25-2007, 12:03 PM
FF 2.0.0.5.

I see your point in my error. I need more work on this.
Thanks again.

Andy

1andyw
07-25-2007, 06:42 PM
Ralph,

Can you make time for another look?

I hope I got the bugs out. This produces the results I expect in IE6 and FF 2.0.0.5.

I put the script and styles in separate files. Now the xhtml doesn't throw errors.

input form:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Registration Form</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript" src="FinalReg.js"></script>
<link rel="stylesheet" href="FinalReg.css" type="text/css" />
</head>
<body>
<form action="" id="form1">
<fieldset><legend>Registration Information</legend>
<label for="firstname">First Name</label><input type='text' class='input' name='firstname' id='firstname' />
<label for="lastname">Last Name</label><input type='text' class='input' name='lastname' id='lastname' />
<input type='button' name='submit' id='button' value='Post This Information' class='button' onclick="registerUser();" />
</fieldset>
<div id="echoMsg"></div>
</form>
</body></html>

The style sheet:
body{font:bold 100% "Trebuchet MS", sans-serif;
color: #666; background: #ccc;
}
legend{font-size: 150%;}
form{width:500px;margin:0 auto; color:#666; background: #fff; padding:3em;}
label{width:100px; margin-left:1em;}
.input{width:25em; margin-left: 110px;}
.button{width:15em; margin-left: 175px;}

The ajax script:
function createXHR()
{
var request = false;
try{
request = new ActiveXObject('Msxml2.XMLHTTP');
}
catch(err2){
try{
request=new ActiveXObject('Microsoft.XMLHTTP');
}
catch(err3){
try{
request=new XMLHttpRequest();
}
catch(err1){
request = false;
}
if (request == false){
alert("Error creating request object!");
}
}
}
return request;
}

The js:
var requestResponse="";

function formreset(){
document.getElementById("form1").reset();
}
function registerUser(){

var request="";
request=createXHR();
request.onreadystatechange=function()
{
if(request.readyState==4)
{
if(request.status==200)
{
requestResponse=request.responseText
parseResponse();
}
else {
alert('Error post.php File'+request.statusText)
}
}
}
var nForm=document.forms[0];
var infoStr="firstname=" + nForm['firstname'].value;
infoStr += "&lastname=" + nForm['lastname'].value;
request.open("POST","post.php", true);
request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
request.send(infoStr);
}
function parseResponse(){
var nMessage=document.getElementById('echoMsg');
nMessage.style.display='';
nMessage.innerHTML="Last entry recorded is " + requestResponse ;
formreset();
}

And the post.php
<?php
$message = "First Name - " . $_POST['firstname'] . "<br>";
$message .= "Last Name - " . $_POST['lastname'];
echo stripslashes($message);
$firstname= $_POST['firstname'];
$lastname= $_POST['lastname'];
$domain="localhost";
$user="root";
$password="andrew";
$conn = mysql_connect( $domain, $user, $password );
$rs=mysql_select_db("ajax", $conn) or die('Could not connect: ' .mysql_error());
$sql="INSERT INTO formpost (`firstname`,`lastname`) VALUES ('$firstname','$lastname')";
$rs=mysql_query($sql,$conn);
?>

Thanks,

Andy

StupidRalph
07-26-2007, 01:14 AM
Looks good. The only thing that immediately jumps out me as a concern is in the PHP file. If you don't have any type of sanitization routine cleaning out your values, then you have a major risk for SQL-injection. Try implementing a sanitizing routine or wrap your PHP variables in your SQL with the mysql_real_escape_string() (http://www.php.net/mysql-real-escape-string)function.

change

$sql="INSERT INTO formpost (`firstname`,`lastname`) VALUES ('$firstname','$lastname')";

to

$sql="INSERT INTO formpost (`firstname`,`lastname`) VALUES ('mysql_real_escape_string($firstname)','mysql_real_escape_string($lastname)')";


If you don't know the significance of SQL injection (http://www.google.com/search?q=SQL+injection) try Googling the term.

1andyw
07-26-2007, 09:37 AM
Thank you, Ralph.

I will sanitize the data before going live.

Andy

StupidRalph
07-26-2007, 09:46 AM
Great. While you're sanitizing...might as well check for XSS holes too right!?! right!?! :P

kleidi
01-11-2011, 08:18 PM
Hello there,
Googling i have arrived here since i have the same problem with my script...to be more exact...is a login script from codecanyon modified for my needs. Since the script is untouched it works without errors but, while i modify it to suite my needs it gives me thise errors:
Notice: Undefined index: fname in /var/www/vhosts/morevesh.com/httpdocs/regjistrohu.php on line 41
Notice: Undefined index: lname in /var/www/vhosts/morevesh.com/httpdocs/regjistrohu.php on line 42
Notice: Undefined index: username in /var/www/vhosts/morevesh.com/httpdocs/regjistrohu.php on line 43
Notice: Undefined index: email in /var/www/vhosts/morevesh.com/httpdocs/regjistrohu.php on line 44
What i modified is the style of the script and div's. Error lines are those:
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$username = $_POST['username'];
$email = $_POST['email'];

and the hole script is this:
<?php
include('header.php');
include('functions/dbconn.php');
include('functions/functions.php');
include('koka.php');
?>
</head>
<body>
<div class="trupi">

<?php
include('koka1.php');
include('koka2.php');
?>

<div class="indexqender">
<div class="trupimajtas">
<div class="lajmehot">Lajmi fundit - HOT</div>
<div class="lajmetqender">
<div id="kokakuqe"> <p>Bėhu pjesė e komunitetit tonė</p></div>
<div class="menupkokakuqe"></div>


<div class="faqeident">
<?php
// User is already logged in, they don't need to view this page.

if(isset($_SESSION['username'])) {

echo '<div class="main">';
echo '<div class="error_message">Kujdes! Ju keni njė llogari nė faqen tonė.</div>';
echo "<h2>Cfarė dėshironi tė bėni tani?</h2><br />";
echo "Kthehu <a href='javascript:history.go(-1)'>mbrapa</a> tek faqja e mėparshme.</li>";
include('footer.php');

exit();
}

// Get POST vars.

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$username = $_POST['username'];
$email = $_POST['email'];

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

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$password2 = $_POST['password_confirm'];
$valid = $_POST['validation'];

if(trim($fname) == '') {
$error = '<div class="error_message">Kujdes! Ju nuk keni vendosur emrin tuaj.</div>';
} else if(trim($lname) == '') {
$error = '<div class="error_message">Kujdes! Ju nuk keni vendosur mbiemrin tuaj.</div>';
} else if(!isEmail($email)) {
$error = '<div class="error_message">Kujdes! E-maili i vendosur nuk ėshtė i saktė. Ju lutemi tė provoni sėrish.</div>';
}

if($password != $password2) {
$error = '<div class="error_message">Kujdes! Fjalėkalimi juaj nuk pėrputhet.</div>';
}

if(strlen($password) < 5) {
$error = '<div class="error_message">Kujdes! Fjalėkalimi juaj duhet tė pėrmbajė minimumi 5(pesė) karaktere.</div>';
}

$count = mysql_num_rows(mysql_query("SELECT * FROM login_users WHERE username='".$username."'"));

if($count > 0) {
$error = '<div class="error_message">Na vjen keq! Pseudonimi i vendosur nuk ėshtė i lirė.</div>';
}

if($valid != '9') {
$error = '<div class="error_message">Kujdes! Vendosni kodin e verifikimit?</div>';
}

if($error == '') {

$sql = "INSERT INTO login_users (user_level, fname, lname, email, username, password)
VALUES ('3', '$fname', '$lname', '$email', '$username', MD5('$password'))";

$query = mysql_query($sql) or die("Fatal error: ".mysql_error());

echo '<div class="main">';
echo "<h2>Pėrgėzime!</h2>";
echo "<div class='success_message'>Ju tashmė jeni pjesė e komunitetit tonė. Klikoni <a href='identifikohu.php'>kėtu</a> pėr tu identifikuar!</div>";

echo "<h2>Tė dhėnat tuaja janė:</h2>";

echo "<ul class='success-reg'>";
echo "<li><span class='success-info'><b>Emri</b></span>$fname $lname</li>";
echo "<li><span class='success-info'><b>Pseudonimi</b></span>$username</li>";
echo "<li><span class='success-info'><b>E-Mail</b></span>$email</li>";
echo "<li><span class='success-info'><b>Fjalėkalimi</b></span>*i fshehur*</li>";
echo "</ul>";

echo "<h2>Cfarė dėshironi tė bėni tani?</h2><br />";
echo "Shkoni <a href='".$fillimi."'>nė faqen kryesore</a>.</li>";

}

}

if(!isset($_POST['new_user']) || $error != '') {

echo $error;

?>
<div class="main">

<form action="" method="post">

<label>Emri / Mbiemri</label><input type="text" name="fname" value="<?=$fname;?>" style="width: 46%;" />&nbsp;
<input type="text" name="lname" value="<?=$lname;?>" style="width: 46%;" /><br />

<script type="text/javascript">
function toggle_username(userid) {
if (window.XMLHttpRequest) {
http = new XMLHttpRequest();
} else if (window.ActiveXObject) {
http = new ActiveXObject("Microsoft.XMLHTTP");
}
handle = document.getElementById(userid);
var url = 'ajax.php?';
if(handle.value.length > 0) {
var fullurl = url + 'do=check_username_exists&username=' + encodeURIComponent(handle.value);
http.open("GET", fullurl, true);
http.send(null);
http.onreadystatechange = statechange_username;
}else{
document.getElementById('username').className = '';
}
}

function statechange_username() {
if (http.readyState == 4) {
var xmlObj = http.responseXML;
var html = xmlObj.getElementsByTagName('result').item(0).firstChild.data;
document.getElementById('username').className = html;
}
}
</script>

<label>Pseudonimi</label><input id="username" type="text" name="username" value="<?=$username;?>" onChange="toggle_username('username')" /><br />
<label>E-mail</label><input type="text" name="email" value="<?=$email;?>" /><br />
<label>Fjalėkalimi dėshiruar</label><input type="password" name="password" value="<?=$password;?>" /><br />
<label>Konfirmo Fjalėkalimin</label><input type="password" name="password_confirm" value="<?=$password2;?>" /><br /><br />
<label>Ju lutem, pergjigjjuni pyetjes? 4 + 5 =</label><input type="text" name="validation" value="<?=$valid;?>" /><br />

<input type="submit" value="Regjistrohu" name="new_user" />

</form>
<? } include('footer.php'); ?>


</div>


</div>
<div class="pastro"></div>

</div>
<?php
include('includet/bllok_djathte.php');
include('fundi.php');
?>

Can someone help me on this issue, please?
P.S. I know that this is an old thread, but i didn;t want to open a new thread since this exists! ;)

devnull69
01-11-2011, 08:32 PM
I'm sorry but I have to ask if you even tried to debug this...

The error says that there is no fname, lname, username, email in the $_POST array. What can be the reason for this?

- The parameters are not being transmitted
- Maybe there is no POST request

And bingo ... both are correct

- You are only transmitting two parameters "do" and "username"
- You are using a GET request instead of POST ... that's why the parameters will be in the $_GET array instead of the $_POST array

Will that help you or do you need more details?

djm0219
01-11-2011, 08:35 PM
This was a duplicate post. Already asked and answered in this thread (http://www.codingforums.com/showthread.php?t=214817).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum