Jump to content

Evaluate Variable as code


tkdasearls
Go to solution Solved by tkdasearls,

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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
}
 
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.