Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Coder
    Join Date
    Jul 2006
    Posts
    171
    Thanks
    13
    Thanked 1 Time in 1 Post

    Undefined Index problem

    Hi,

    Having an error returned:

    PHP Code:
    Undefined index:  fname in c:apachehtdocsandytestformpost2.php</bon line <b>6</b><br />
    Undefined index:  lname in c:apachehtdocsandytestformpost2.php</bon line <b>7</b><br /> 
    My input formin2.php is:

    Code:
    <!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
    Code:
    <?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:

    Code:
    db: ajax table: formin
    	firstname 	varchar(30) 	latin1		
    	lastname 	varchar(30) 	latin1
    Would someone straighten me out on this, please.

    Thanks,

    Andy

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    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
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Regular Coder
    Join Date
    Jul 2006
    Posts
    171
    Thanks
    13
    Thanked 1 Time in 1 Post
    Hi Eric,

    I made the changes is the 'send' action. The file 'formpost2.php' continues to return the same:
    Code:
    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

  • #4
    Regular Coder
    Join Date
    Jul 2006
    Posts
    171
    Thanks
    13
    Thanked 1 Time in 1 Post
    Resolved!

    The problem has been identified. Thank you.

    Andy

  • #5
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Are you going to share your solution with the rest of us?
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #6
    Regular Coder
    Join Date
    Jul 2006
    Posts
    171
    Thanks
    13
    Thanked 1 Time in 1 Post
    Hi Ralph,

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

    submit form:
    Code:
    <!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:

    Code:
    <?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

  • #7
    Regular Coder
    Join Date
    Jul 2006
    Posts
    171
    Thanks
    13
    Thanked 1 Time in 1 Post

    Lesson Learned

    Ralph,

    The lesson I learned from this is to use form reference to form elements. I had used
    Code:
    document.getElementById
    initially and changed that to
    Code:
    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

  • #8
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Code:
    <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'>
    Code:
    form[0].['fname'].value
    PHP Code:
    $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?
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #9
    Regular Coder
    Join Date
    Jul 2006
    Posts
    171
    Thanks
    13
    Thanked 1 Time in 1 Post
    FF 2.0.0.5.

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

    Andy

  • #10
    Regular Coder
    Join Date
    Jul 2006
    Posts
    171
    Thanks
    13
    Thanked 1 Time in 1 Post
    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:
    Code:
    <!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:
    Code:
    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:
    Code:
    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:
    Code:
    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
    Code:
    <?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

  • #11
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    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() function.

    change
    PHP Code:
    $sql="INSERT INTO formpost (`firstname`,`lastname`) VALUES ('$firstname','$lastname')"
    to
    PHP Code:
    $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 try Googling the term.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #12
    Regular Coder
    Join Date
    Jul 2006
    Posts
    171
    Thanks
    13
    Thanked 1 Time in 1 Post
    Thank you, Ralph.

    I will sanitize the data before going live.

    Andy

  • #13
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Great. While you're sanitizing...might as well check for XSS holes too right!?! right!?! :P
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #14
    New Coder
    Join Date
    Oct 2008
    Posts
    26
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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:
    Code:
    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:
    PHP Code:
    $fname $_POST['fname'];
        
    $lname $_POST['lname'];
        
    $username $_POST['username'];
        
    $email $_POST['email']; 
    and the hole script is this:
    PHP Code:
    <?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!

  • #15
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    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?


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •