0

Following is a practice code from a book titled "Head First PHP and sql" which I am studying. When I run sendemail.php in localhost sever I get two errors:

  • Undefined variables Subject & text.

Both however were defined with the post array at the beginning. This code is about an HTML form that is part of PHP script, sticky form data and built-in PHP superglaobal variable $_Server['PHP_SELF']. Please see if I am missing something.

enter image description here

<!DOCTYPE html >
<html lang="en">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>sendmail.html</title>
</head>

<body>

    <?php 
        if(isset($_POST['Submit'])){
        $from='elmer@makemelvis.com';
       **$subject=$_POST['subject'];** 
       **$text =$_POST['elvismail'];**
         $output_form =false;

    if(empty($subject) && empty($text)){
        echo 'You forgot the email subject and body text 
         <br/>';
        $output_form =true;
        }   

    if(empty($subject) && (!empty($text))){
        echo 'You forgot the email subject<br/>';
        $output_form =true;
        }
         

     if((!empty($subject)) && empty($text)){
        echo 'You forgot the email body text<br/>';
        $output_form =true;
        }   

    if((!empty($subject)) && (!empty($text))){
            

    $dbc = mysqli_connect('localhost', 'root', 
    '#######', 'elvis_store')
     or die('Error connecting to Mysql Server.');
        
    $query = "SELECT * FROM email_list";        
            $result = mysqli_query($dbc, $query)
            or die ('Error querying database.');
    
        while($row = mysqli_fetch_array($result)){
               
        $first_name= $row['first_name'];    
        $last_name = $row['last_name'];
        $msg= "Dear $first_name $last_name,\n $text";            
        $to= $row['email'];
        
        mail( $to , $subject, $msg, 'From:'. $from);
            
        echo 'Email sent to:' . $to.'<br>';
        
            }   
        
        mysqli_close($dbc);
    
            }
        }// This will close the first if, which tells us 
            if the form was submitte.
        
        else{
                $output_form =true; //if the from's never 
               been submitted, we definitely need to show 
                it!
            }
                if ($output_form){/* This if statment 
                checks the $output_form variable
                and displays the form is it is true.*/
        
        ?>      <!--closed PHP-->
        
        <form method="post" **action="<?php echo 
         $_SERVER['PHP_SELF']; ?>" >**

        <label for="subject">Subject of email:</label> 
         <br/>
            
        <input id="subject" name="subject"      
         type="text" size="30" **value ="<?php echo 
         $subject; ?>"/>**<br/> <!--Added sticky form 
          data-->

            <label for="elvismail">Body of email:</label> 
             <br />
            
         <textarea id="elvismail" name="elvismail" 
          rows="8" cols="40"> <?php echo $text; ?> 
         </textarea><br /><!--Added sticky form data-

->
            <input type="submit" name="Submit" 
             value="Submit"/>
        </form>

        <?php

        } //this curly bracket closes the 
        if($output_form)
        ?>

    </body>
    
    </html>

     Thx
      SG_Gham
Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
user943062
  • 11
  • 2
  • 1
    Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Dharman Jan 12 '21 at 09:35
  • [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Tomer Shetah Jan 14 '21 at 06:30

0 Answers0