There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer black screen boot computer connection crash css dell display driver drivers email error ethernet excel explorer firefox firefox 3 game hard drive internet internet explorer itunes laptop lcd linux malware monitor network networking nvidia outlook outlook 2003 outlook express partition password printer problem router slow software sound trojan usb video virus vista windows windows xp wireless
Web Design & Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
Solved: Undefined index


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
skyhigh007's Avatar
Senior Member with 393 posts.
 
Join Date: Jun 2004
29-Jun-2008, 03:40 PM #1
Solved: Undefined index
I created an check.php page that takes in users E-maill address.

Code:
  <form action="check.php" method="post" >
              <input  type="text" name="email"  size="10" />
              <input  type="submit" value="submit" />
        </form>
On the same page, I want to store it into the database

Code:
<?php
	     $email=mysql_real_escape_string($_POST["email"]);
              echo "<center>$email</center>";
	     $shop->db->check_email($email);
  $insert = "INSERT INTO tbl_subscribe (cst_submail) VALUES ('$email')";
	$result = $shop->db->executeQuery($insert, "result");	
	    
	   ?>
and the ERROR is "Notice: Undefined index: email in C:\wamp\www\newer\check.php on line 60 "

The link 60 is: $email=mysql_real_escape_string($_POST["email"]);

Does anyone know why I got the undefined index?
MMJ's Avatar
MMJ MMJ is online now
Distinguished Member with 3,250 posts.
 
Join Date: Oct 2006
29-Jun-2008, 05:56 PM #2
That just means that it can't find the $_POST["email"] variable. "Notice" errors should not even appear, depending on your setup.

Are you sure the from is sending the data correctly?
skyhigh007's Avatar
Senior Member with 393 posts.
 
Join Date: Jun 2004
29-Jun-2008, 10:55 PM #3
Even If I changed the PHP code to
Code:
<?php  $email=$_POST["email"];
              echo "<center>$email</center>";
           ?>
it still gives me the error. If I type in something in the input text box, the echo will print out what i typed in the box. However, if I just directly go to the check.php page, it gives me the undefined index error.
Mudley's Avatar
Computer Specs
Senior Member with 100 posts.
 
Join Date: Apr 2008
Experience: Advanced
30-Jun-2008, 12:29 AM #4
I assume you're getting the error upon first loading the page, and you're not even seeing the form.

you need to use some logic to control portions of the code.

wrap the form action with a condition.
i usually name my form submits, so <input type="submit" name="form_submit" value=Submit">

then for the condition, <? if(array_key_exists('form_submit',$_POST))

if the $_POST array doesn't exist, you shouldn't be trying to take action
skyhigh007's Avatar
Senior Member with 393 posts.
 
Join Date: Jun 2004
30-Jun-2008, 12:41 AM #5
well, I load it to that page and it gives me the error. If I type something then it won't give me the error. I guess you are right, i will try it out.
MMJ's Avatar
MMJ MMJ is online now
Distinguished Member with 3,250 posts.
 
Join Date: Oct 2006
30-Jun-2008, 06:26 AM #6
Quote:
Originally Posted by skyhigh007 View Post
However, if I just directly go to the check.php page, it gives me the undefined index error.
Exactly. Thats because the form data isn't sent; so $_POST["email"] isn't set.
skyhigh007's Avatar
Senior Member with 393 posts.
 
Join Date: Jun 2004
30-Jun-2008, 04:28 PM #7
so I used
Code:
 if(isset($_POST['email'])){
             }else{}
However, the footer.php that I included at the end of check.php wont show up because during the checking of the validation of the E-mail, I used php die or exit fuction if the E-mail is not correct or does not exists. so I guess it terminates everything ?
Mudley's Avatar
Computer Specs
Senior Member with 100 posts.
 
Join Date: Apr 2008
Experience: Advanced
30-Jun-2008, 04:33 PM #8
yes, they terminate the execution of the script.

take them out

if all your trying to do is stop the form from displaying, wrap with a hide form condition

<? if(isset($hideForm)): ?>
<form>...</form>
<? endif; ?>
skyhigh007's Avatar
Senior Member with 393 posts.
 
Join Date: Jun 2004
30-Jun-2008, 04:53 PM #9
well, its during the checking e-mail process and if there's an error in the input e-mail, then i want to display a message and stop the next checking step. I changed to Echo instead of die or exit, then every checking steps were excuted and prints outs a lot of messages.
you cant used break in if statement right ? any alternatives?
tomdkat's Avatar
Computer Specs
Distinguished Member with 3,715 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
30-Jun-2008, 04:59 PM #10
If I were you, I would put the form validation code in a function that you would call like this:

// Get submitted form data

$result = validateForm({pass form data or get it directly via _POST in function});

// Print footer and other page info

Then, "validateForm()" would issue any messages that need to be displayed. If it encounters a problem, it simply returns an error code to the main script (the caller) and the main script can decide what to do, as appropriate. This way, the validation of the form data won't interfere with the mechanics of building and sending the complete HTML page to the browser.

Peace...
Mudley's Avatar
Computer Specs
Senior Member with 100 posts.
 
Join Date: Apr 2008
Experience: Advanced
30-Jun-2008, 05:03 PM #11
why not check all inputs at once?

here's usually how i go about form validation:
$errArr = array(); // empty array for holding error message

if(!isset($_POST['email']) || $_POST['email'] == '')
$err[] = "you must enter a valid email address";

if(!isset($_POST['name']) || $_POST['name'] == '')
$err[] = "you must enter a valid name";

// and then, after you've trapped all the inputs:
if(!count($errArr))
{
// your real form action goes here
$hideForm = true;
}
else {
// output errors
echo implode("<br>",$errArr);
}
tomdkat's Avatar
Computer Specs
Distinguished Member with 3,715 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
30-Jun-2008, 05:12 PM #12
Quote:
Originally Posted by Mudley View Post
why not check all inputs at once?
I agree to a point but if he wanted to address each error individually, he's got to stop the checking and report the error.

Using your code as an example, if/else blocks will be required:

// Pseudo-code
if (not valid e-mail address)
report invalid e-mail address
else if (missing name)
report missing name
else if (some other problem)
report other problem

and so on. That's partially why I suggest putting that logic in a separate function. The details of how the validation is neatly organized and won't impact the flow of the creation of the HTML page. If he used a function, the logic would look more like what you wrote:

validateForm() {
if (not valid e-mail address) {
err = not valid e-mail address;
return err;
}

if (missing name) {
err = missing name;
return err;
}

if (some other problem) {
err = some other problem;
return err;
}
return 0 (or success or whatever)
}

You get the idea.

Peace...
Mudley's Avatar
Computer Specs
Senior Member with 100 posts.
 
Join Date: Apr 2008
Experience: Advanced
30-Jun-2008, 05:59 PM #13
unless the validation depends on previous inputs, there's not really any point in halting validation upon the first error

in fact, its a big UI 'piss off' factor to do so
skyhigh007's Avatar
Senior Member with 393 posts.
 
Join Date: Jun 2004
30-Jun-2008, 06:26 PM #14
Here's my function:
Code:
	function check_email ($email){
           if ($email==""){
            die("Please type in your E-maill address");
         }elseif(strpos($email, "@" )<2 ){
            die("Please type in a correct E-mail address");
          }elseif(strpos(substr($email,strpos($email,"@")), ".") < 2){
           die("Please type in a correct E-mail address");
         }elseif( strlen($email) <7 ){
          die("Please type in a correct E-mail address");
        }elseif(ubstr_count ($email,"@") !=1 ){
          die("Please type in a correct E-mail address");
        }else {

            echo "Thank you for signing up our newsletter";
       }
  }
The die function will terminate the checking once the error is found, so that it does not go on to the next checking. Die and exit terminates my include footer.php in the check.php page.
Mudley's Avatar
Computer Specs
Senior Member with 100 posts.
 
Join Date: Apr 2008
Experience: Advanced
30-Jun-2008, 07:49 PM #15
not a very functional function

function isemail($email) {
return preg_match('|^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{2,})+$|i', $email);
}

if(!isemail($email))
echo "please enter a valid email";
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 07:57 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.