• PHP Help PHP Coding
  • Undefined Variable issue when extract $_POST variable values into local variables

I'm very new to PHP - as in, I just started trying to figure it out a few days ago. I'm working on a MA thesis for which I'm collecting data over the internet. I found a seemingly simple PHP script that I could use with a <form method="post"> tag to email the results to myself.

This is the PHP script I'm using (except I only left in the names of the first few variables to save space):

// Get all $_POST variables
foreach($_POST as $var => $val)
{ 
   ${$var} = $val; 
} 

// Set up the email body
$body_body = $adfid.",".$relig12.",".$attend12.",".$relig1st.",".$seeker.",".$religity.",".$belief.",".$expsprt.",".$expone.",".$expoob.",".$expecst.",".$attend;

The questions in the form are simple enough:

<tr>
      <td>1. How personally religious were you at age 12?</td>
      <td>
         <input type="radio" name="relig12" value="0"/>Not religious at all</br> 
         <input type="radio" name="relig12" value="1"/>Not very religious</br>
         <input type="radio" name="relig12" value="2"/>Somewhat religious</br> 
         <input type="radio" name="relig12" value="3"/>Very religious</td>
    </tr>

When I fill out the form and press Submit, I receive an error message for every variable, like this:

Notice: Undefined variable: adfid in /pass/home/10/e/eas295/www/process.php on line 7

Notice: Undefined variable: relig12 in /pass/home/10/e/eas295/www/process.php on line 7

Notice: Undefined variable: attend12 in /pass/home/10/e/eas295/www/process.php on line 7

My intention is to have the email simply contain the value of each inputted variable separated by commas in a single line so that each email will be a line in a CSV file that I can then transfer into a statistical software package. I think that $body_body part is correct, but for some reason the $_POST variables don't seem to be transferring into local variables, because the email I receive contains nothing but commas (all the variable values missing).

I have a feeling this is very simple but I really haven't a clue what I'm doing. I understand the foreach loop is supposed to prevent me from having to write a $var = $_POST['var'] for each of the 120+ variables in my form, but I don't understand why it's not working for me. After some google searching I also attempted to use

extract($_POST);

rather than the foreach loop, but got the same error/email without any variable values.

Any suggestions?

    I hardly ever use [man]extract[/man] myself - and certainly not on user-supplied data (see the warning on the linked page). I generally just use $_POST['var'] directly. To avoid the error messages, I check to see that each variable actually exists before I try to use it.

    There's some commentary on the subject over here.

      Scratch the earlier response...I would edit but it hasn't been approved yet so I can't see it yet! The earlier error message was because I hadn't put a semi-colon after one of the lines. So having fixed that...

      <html>
      <body>
      <form action="test.php" method="post">
      <center>
      
      <!--Religious Background and Identity-->
      <p>The following questions ask about your religious background and identity.
      <table border="1" width=75%>
          <tr>
            <td>1. How personally religious were you at age 12?</td>
            <td>
               <input type="radio" name="relig12" value="0"/>Not religious at all</br> 
               <input type="radio" name="relig12" value="1"/>Not very religious</br>
               <input type="radio" name="relig12" value="2"/>Somewhat religious</br> 
               <input type="radio" name="relig12" value="3"/>Very religious</td>
          </tr>
      </table></p>
      <input type="submit" value="Submit">
      </center>
      </body>
      </html>
      
      <?php
      
      $var = $_POST['relig12'];
      
      // Set up the email body
      $body_body =",".$relig12.",";
      
      $to = 'me <#####@gmail.com>';
      $subject = 'Survey Results';
      $body = $body_body;
      
      mail($to, $subject, $body);
      ?>

      The email now sends as before, but I'm getting similar errors as before:

      Notice: Undefined index: relig12 in /pass/home/10/e/eas295/www/test.php on line 3

      Notice: Undefined variable: relig12 in /pass/home/10/e/eas295/www/test.php on line 6

        If a button isn't set you will get that error.

        Try

        if( isset( $_POST['relig12'] ) 
         $relig12 = $_POST['relig12'];
        

          Alright, I'm slowly getting there. Changing my PHP script to:

          <?php
          
          if( isset( $_POST['relig12'] ))
          $relig12 = $_POST['relig12']; 
          
          // Set up the email body
          $body_body =",".$relig12.",";
          
          $to = 'me <#####@gmail.com>';
          $subject = 'Survey Results';
          $body = $body_body;
          
          mail($to, $subject, $body);
          ?>

          eliminated the "undefined index on line 3" error, but I'm still left with an "undefined variable: relig12" on line 7.

          I wish I understood what was happening better. Trying to read tutorials on this stuff just confuses me even further because the code that I found that appeared to be a plug-and-play form is so unlike any of the examples in the basic explanations I find.

          I found the original source code I've been working with in a tutorial here on PHPBuilder: http://www.phpbuilder.com/columns/patterson20051018_2.php3

            I think this may be an issue with my server. I wrote two files copied directly from this simple form example and I am receiving the same errors. It is as if something is letting my survey call the PHP file, but stopping the $_POST variables from actually getting to it?

            Since it is now almost Monday, I will try to see if one of the tech guys in my building has a few minutes to squeeze me and my problem in between all the start-of-the-semester computer orders, approvals, repairs, etc.

              You error settings are set too high, so you get all the warnings.

              put
              error_reporting(E_ALL ^ E_NOTICE);

              at the top of the script and you will remove the notices.

              or define the variable.

              if( isset( $_POST['relig12'] ))
                  $relig12 = $_POST['relig12']; 
              else
                  $relig12 = ''; 
              
                HalfaBee wrote:

                and you will remove the notices.

                Well, not exactly - changing error reporting will hide errors in coding, but not fix them.

                Essentially, you need to reference the $_POST array for all POST'ed elements. If you never defined $relig12, then it's understandable that it wouldn't exist at that point (a.k.a. is undefined). A common shorthand way of defining incoming variables might be:

                $var1 = isset($_POST['var1']) ? $_POST['var1'] : '';
                $var2 = isset($_POST['var2']) ? $_POST['var2'] : '';

                That method is, at least, my personal preference - using the ternary operator.

                  Write a Reply...