Jump to content

Checked if isset, but still get undefined index. What's wrong here?


Lee

Recommended Posts

Hi, I'm still a bit of a noob with php, so this code might be a bit ugly, but anyway I'm making a form to send data to a mysql table. As a rule I was told to check isset on one of the input fields, which should surpress the undefined index error. I did this but I still get: Undefined index: year in D:\wamp\www\practice\karaoke-site\admin\functions.php  on line 37 Can anyone tell me where I've gone wrong? Thanks :)

 

function SetupForm($params)
{
	            
if(isset ($_POST ['year']) ) {
        $CompanyName         = $_POST['name'];
$email		     = $_POST['email'];
$area		     = $_POST['area'];
$description         = $_POST['description'];
$year		     = $_POST['year']; 
}         

$CompanyName   = $params['name'];
$email		   = $params['email'];
$area		   = $params['area'];
$description   = $params['description'];
$year		   = $params['year'];

$errors = array();

if(strlen($CompanyName) < 1)
	$errors[] = 'Company name must be at least 2 characters.';
if(strlen($area) < 2)
	$errors[] = 'You must enter the area where you operate.';
if(strlen($description) < 1)
	$errors[] = 'You must enter a description for your company.';
if( (strlen($year) < 4 )&&(!is_numeric($year)) )
	$errors[] = 'You must enter a 4 digit year i.e. 2009.';	
if(!preg_match('/^[A-Za-z][\w._-]+@\w[\w-.]+\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$/' , $email))
	$errors[] = 'You must enter a valid email address';


if($errors)
	return $errors;

$query = sprintf (
              "INSERT into setup
                                set
                                   name        = '%s',
                                   email       = '%s',
                                   area        = '%s',
                                   description = '%s',
                                   year        = '%s'
                                ",
                                   mysql_real_escape_string($params['name']),
                                   mysql_real_escape_string($params['email']),
                                   mysql_real_escape_string($params['area']),
                                   mysql_real_escape_string($params['description']),
                                   mysql_real_escape_string($params['year'])
                                   );
    $result = mysql_query($query); 
    
if (!$result) {
        return false;
    }
    else {
        return true;
    }                           
}

 

 

Link to comment
Share on other sites

What? You define $_POST as variables, but you define $params as the same variables. What is the objective of this?

 

Also, if you have a submit button, it'll be better to check isset on the submit button. Rather than a form field?

It's hard to help without the html form too and all that.

Link to comment
Share on other sites

What? You define $_POST as variables, but you define $params as the same variables. What is the objective of this?

 

Also, if you have a submit button, it'll be better to check isset on the submit button. Rather than a form field?

It's hard to help without the html form too and all that.

Hi, thanks for the reply.

 

At the top of the function, I set $CompanyName  = $_POST['name']; and so on....  to grab the input from the form.

Then later I set $CompanyName  = $params['name']; and so on.... to do the mysql query. I don't really know how else to shorten that.

 

This is the form:

		<?php
require_once ('functions.php');


	if(isset($_POST['submit']))
	{
		$result = SetupForm($_POST);
		if($result === true)
		{
			echo 'Setup details successfully submitted!';
			die();
		}
	}

	?>
	<legend>Setup Form</legend>
	<?php
		if(isset($result) && $result)
		{
			echo '<div align="center"><ul>';
			foreach($result as $error)
				echo '<li>' . $error . '</li>';
			echo '</ul></div>';
		}
	?>
<div align="center"	>	
<form id="setup" name="setup" method="post" action="setup.php">
<b>Enter your name:</b><br />      
      <input name="name" type="text" id="name" size="60" />      <br />
      <br />
      <b>Enter the area where you are based:</b><br />
      <input name="area" type="text" id="area" size="60" />
      <br />
      <br />
      <b>Your email address: </b>      <br />
      <input name="email" type="text" id="email" size="60" maxlength="80" />      <b><br />
      <br />
      Short description about you:</b><br /> 
      <input name="description" type="text" id="description" size="60" />
      <b><br />
      <br />
      Enter the year that you started: </b>      <br />
      <input name="description2" type="text" id="description2" size="4" maxlength="4" />
      <br />
      <br />      
      <label>
      <input type="submit" name="submit" value="submit" />
      </label>
</form>
</div>

 

 

Link to comment
Share on other sites

<input name="description2" type="text" id="description2" size="4" maxlength="4" />

 

This should be name="year" I'm guessing?

 

Also, you don't need

        $CompanyName         = $_POST['name'];
$email = $_POST['email'];
$area = $_POST['area'];
$description         = $_POST['description'];
$year = $_POST['year']; 

 

Because it's changed $_POST to $params, so using $_POST anywherei n the function is completely useless.

Link to comment
Share on other sites

Also, I'm certain this will give you a better result.

 

<?php
function SetupForm($params)
{
if(isset($param['submit'])){         

	$errors = array();	

	if(strlen($params['name']) < 1) {
		$errors[] = 'Company name must be at least 2 characters.';
	}
	if(strlen($params['area']) < 2) {
		$errors[] = 'You must enter the area where you operate.';
	}	
	if(strlen($params['description']) < 1) {
		$errors[] = 'You must enter a description for your company.';
	}
	if( (strlen($params['year']) < 4 )&&(!is_numeric($year)) ) {
		$errors[] = 'You must enter a 4 digit year i.e. 2009.';
	}
	if(!preg_match('/^[A-Za-z][\w._-]+@\w[\w-.]+\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$/' , $params['email'])) {
		$errors[] = 'You must enter a valid email address';
	}

	if($errors) {
		return $errors;
	} else {
		$CompanyName = mysql_real_escape_string($params['name']);
		$email = mysql_real_escape_string($params['email']);
		$area = mysql_real_escape_string($params['area']);
		$description = mysql_real_escape_string($params['description']);	
		$year = mysql_real_escape_string($params['year']);

		$query = sprintf ("INSERT into setup SET name='$CompanyName', email='$email', area='$area', description='$description', year='$year'");
		    $result = mysql_query($query); 
    
	    if (!$result) {
			return false;
    		} else {
			return true;
    		}                           
	}
}
}
?>

Link to comment
Share on other sites

<input name="description2" type="text" id="description2" size="4" maxlength="4" />

This should be name="year" I'm guessing?

OOPS!! What a rookie mistake, gives away that I am using dreamweaver to quickly copy & paste form fields. Thanks  :shy:

But at least I know now that there is something more important wrong with my function. So I'm guessing I should define those variables that grab the form data outside the function?

 

Link to comment
Share on other sites

Ah thanks, that's much cleaner lol.

 

However, with that code, it is not collecting the data from the form and the errors do not return now. Sorry, I'm really struggling to get to grips with functions (which is why I am trying this exercise), so although I can see what you have written is doing, I'm a little lost now about collecting the form data and returning the error.

Link to comment
Share on other sites

Haha, I'm making you do it now.  :P

 

That works a treat now. I just realised how the form data is being sent doh!

<?php

	if(isset($_POST['submit']))
	{
		$result = SetupForm($_POST);
		if($result === true)
		{
			echo 'Congratulations - Your setup details have been submitted!';
			die();
		}
	}

	?>

 

Thanks a lot, that has really helped. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.