SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict Belfast75's Avatar
    Join Date
    Oct 2005
    Location
    London, England
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Undefined index from a <form> post

    Hi,

    I have a form on 'contact.php' which posts to 'thankyou.php' page that contains a function to capture the form ouput and write to a file. I can't see any thing wrong with the syntax but I get a 'Notice: Undefined index: txt_email in C:\wamp\www\o2aaic\smartsteps\thankyou.php on line 86' error. I can test if its set to stop the error but I can't see what I've missed to actually pull in the post request.

    Code HTML4Strict:
    <form name="tenderForm" action="thankyou.php" method="post" id="contactForm" enctype="text/plain" onsubmit="return validate_form();">            
        ...
        <input type="text" name="Email" value="" id="txt_email" class="required" />

    The php code is:

    Code PHP:
    function appendText() {
     
    	$file = "e_address.txt";
    	$fh = fopen($file, 'a') or exit("can't open file");
     
    	//Check file is writable and form data is set
    	if(is_writable($file) && isset($_POST['txt_email']))
    	  {
    		$fh = fopen($file, 'a') or exit("can't open file");
    		$stringData = $_POST["txt_email"];
    		fwrite($fh, $stringData);
    		echo "SUCCESS";
    		fclose($fh);
    	  }
    	else
    	  {
    	// Polite error message to be defined
    	  }
    }

    I've not done much php coding recently so I'm a little rusty but any help would be appreciated
    Thanks, Des

  2. #2
    Non-Member Max Height's Avatar
    Join Date
    Dec 2011
    Posts
    303
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Belfast75 View Post
    'Notice: Undefined index: txt_email in C:\wamp\www\o2aaic\smartsteps\thankyou.php on line 86' error. I can test if its set to stop the error but I can't see what I've missed to actually pull in the post request.
    that error message is telling you that you don't have an element whose name is txt_email in your form as referenced by your line of code:


    $stringData = $_POST["txt_email"];

    Form data is sent in name/value pairs, not id/value pairs





  3. #3
    SitePoint Addict Belfast75's Avatar
    Join Date
    Oct 2005
    Location
    London, England
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I've amended the code now but I still get the same error

    Code HTML4Strict:
    <input type="text" name="txt_email" value="" id="txt_email" class="required" />

    php is the same.

    The function works fine when I just try to append text eg:
    Code PHP:
    $stringData = "TEST\n";

    and I have other websites running on the same AMP stack and they process the POST correctly.

    any other ideas?

  4. #4
    Non-Member Max Height's Avatar
    Join Date
    Dec 2011
    Posts
    303
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    'Notice: Undefined index: txt_email in C:\wamp\www\o2aaic\smartsteps\thankyou.php on line 86'
    What is the code in line 86?

  5. #5
    SitePoint Addict Belfast75's Avatar
    Join Date
    Oct 2005
    Location
    London, England
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its the line that pulls in the POST data:

    Code PHP:
    $stringData = $_POST["txt_email"];

  6. #6
    SitePoint Addict Belfast75's Avatar
    Join Date
    Oct 2005
    Location
    London, England
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Solved.

    The form had an encoding type set, when I remove this it fixes the problem. So for completeness, the form code is now:
    HTML Code:
    <form name="tenderForm" action="thankyou.php" method="post" id="contactForm" onsubmit="return validate_form();">            
    rather than
    HTML Code:
    <form name="tenderForm" action="thankyou.php" method="post" id="contactForm" enctype="text/plain" onsubmit="return validate_form();">            
    Thanks for your help.
    Last edited by Belfast75; Jan 12, 2012 at 04:36. Reason: minor copy amend


Bookmarks

Posting Permissions

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