tkdasearls Posted April 28, 2015 Share Posted April 28, 2015 Is there a way to evaluate a php variable as code instead of a string? $sql1 = mysql_query("SELECT * FROM form WHERE id='".$id."'"); while($row1 = mysql_fetch_array($sql1)){ $id1 =$row1["id"]; $name1 =$row1["name"]; $name1 = str_replace(' ', '', $name1); $name1 = str_replace('-', '', $name1); $errorline .= "".$_POST[$name1]."=='' || "; } $errors = "if(".$errorline."){"; $errors .= '$error=1}'; eval($errors); echo $error; I am able to make $errors look the way I want it to.: if(=='' || =='' || =='' || =='' || =='' || =='' || =='' || =='' || =='' || =='' || =='' || =='' || ){$error=1} Where before the "==" is a post variable, thats why its not showing. I need to know how I can change this to code instead of a variable. As you can see, I tried the "eval" function, but it gives a T_IS_EQUAL error. Can anyone help? If I have to change my logic I will, but I think you probably get the point as to what I am trying to achieve. This is for error checking of a dynamically created form. Quote Link to comment Share on other sites More sharing options...
tkdasearls Posted April 28, 2015 Author Share Posted April 28, 2015 I realized where my error is at. it is returning a blank for "$_POST[$name1] when I actually need it to return $_POST[$name1]. How do I make it do this? I tried putting this part inside of quotes, but it is still returning blank and not the actual term "$_POST[$name1]". How do I make it do this? Quote Link to comment Share on other sites More sharing options...
Solution tkdasearls Posted April 28, 2015 Author Solution Share Posted April 28, 2015 got it. didn't realize there was a difference between ' and " Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 28, 2015 Share Posted April 28, 2015 This is what functions are for. Trying to make a giant if statement and then eval it is a really bad idea. Heck using eval at all is something to be avoided. Just to get an idea of something that has actually been thought out, take a look at the symfony2 validation component: http://symfony.com/doc/current/book/validation.html With that said you could also just write something simple like: <?php function checkForNull($rows) { // return array of errors $errors = array(); foreach($rows as $row) { $name1 =$row["name"]; $name1 = str_replace(' ', '', $name1); $name1 = str_replace('-', '', $name1); if ($_POST[$name1] == '') { $errors[] = $name; } } return $errors; } $sql1 = mysql_query("SELECT * FROM form WHERE id='".$id."'"); $rows = array(); while ($rows[] = mysql_fetch_assoc($sql1)) { // Fetch all rows } $errors = checkForNulls($rows); if (count($errors) > 0) { // You can cycle through this to figure out all the null POST fields and display an error or annotate the form } Quote Link to comment 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.