bugzy Posted April 22, 2012 Share Posted April 22, 2012 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?? Quote Link to comment https://forums.phpfreaks.com/topic/261409-how-to-solve-and-simplify-my-form-validation/ Share on other sites More sharing options...
sunfighter Posted April 22, 2012 Share Posted April 22, 2012 To solve the empty string problem place this in start of if statement: if(isset($_POST['submit'])) { $empty_name = ""; $empty_content = ""; Quote Link to comment https://forums.phpfreaks.com/topic/261409-how-to-solve-and-simplify-my-form-validation/#findComment-1339611 Share on other sites More sharing options...
shiza Posted April 22, 2012 Share Posted April 22, 2012 check the form validation script and modify it according to your own requirement link: http://www.echoecho.com/jsforms02.htm Quote Link to comment https://forums.phpfreaks.com/topic/261409-how-to-solve-and-simplify-my-form-validation/#findComment-1339616 Share on other sites More sharing options...
bugzy Posted April 22, 2012 Author Share Posted April 22, 2012 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... Quote Link to comment https://forums.phpfreaks.com/topic/261409-how-to-solve-and-simplify-my-form-validation/#findComment-1339618 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.