Jump to content

How to solve and simplify my form validation


bugzy

Recommended Posts

Hello!

 

I have this approach on form validation(I'm just a newbie)

 

 

<?php


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


	$proj_name = me_mysql_prep(trim($_POST['proj_name']));
	$proj_content = me_mysql_prep($_POST['proj_content']);



	if(empty($_POST['proj_name']))
	{
		$empty_name = array('Project Name Cannot Be Empty');
	}

	if(empty($_POST['proj_content']))
	{
		$empty_content = array('Content Cannot Be Empty');
	}




	if(isset($empty_name) || isset($empty_content));
	{
		$error_merge = array_merge($empty_name, $empty_content);
	}





	if(!empty($error_merge))
			  {
				  foreach($error_merge as $error)
				  {
					  echo "<span class=\"error_validation\">*". $error . "<br></span>";
				  }
			  }
			  else
			  {

						$query = "INSERT into rec_projects (proj_name, content, date_reg) VALUES('{$proj_name}', '{$proj_content}', 		 '{$datenow}')";

						$result = mysql_query($query,$connection);

			  }
			  
			  echo "<br><br>";

}	


?>

 

 

 

My problem is at this line

 

<?php

	if(isset($empty_name) || isset($empty_content));
	{
		$error_merge = array_merge($empty_name, $empty_content);
	}
?>

 

 

because if, let's say $empty_name is not set, I'm going to have an error like

 

"undefined $empty_name"
or something like that.

 

 

I wonder if there's a PHP function that disregard a value in an array if it's not set.. or you guys have different approach to solve and simplify this??

 

To solve the empty string problem place this in start of if statement:

if(isset($_POST['submit']))
{
$empty_name = "";
$empty_content = "";

 

 

I know this already but what I want is something more robust like what my code from above. I can fix it but the code will be long so I'm asking if there's a way to solve it and simplify the code so it won't be that long...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.