Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    32
    Thanks
    15
    Thanked 0 Times in 0 Posts

    PHP tutorial problem

    Can anybody help with this please — it's a form validation exercise from a book I'm following.

    If you remove lines 4 and 25 the script works, except it returns the error message I've built in on line 11 when the form has never been filled in.

    Lines 4 and 25 are what the book tells me to add to stop this error message from happening when the page first loads, but then the script doesn't work.

    Thanks in advance for any help.

    Code:
    <p class="body">
    
    <?php
    if (isset($_POST['submit'])) { //This is the start of the problem line
    	$from = 'me@gmail.com'; //What mail recipient will see as mail sender
    	$subject = $_POST['subject']; //Field from form on send_email.html
    	$message = $_POST['message']; //Field from form on send_email.html
    	$output = false; //Flag for whether to re-output form set to false
    
        if (empty($subject) && empty($message)) {
    		echo '<b>Error:</b> Please enter a subject and message';
    		$output = true;
    	}
    	
        if (empty($subject) && (!empty($message))) {
    		echo '<b>Error:</b> Please enter a subject';
    		$output = true;
    	}
    	
        if ((!empty($subject)) && empty($message)) {
    		echo '<b>Error:</b> Please enter a message';
    		$output = true;
    	}
    	
      }//This closes the problem line
      
      else {
        $output = true;
      }	
    	
    	
    	if ((!empty($subject)) && (!empty($message))) {
    	
    		$dbc = mysqli_connect('localhost', 'xxx', 'xxx', 'xxx') //Open database connection
    		or die('Error connecting to MySQL database'); //Or die trying
    		
    			$query = "SELECT * FROM elvis_list"; //Query - select everything from the elvis_list table in the database above
    			$result = mysqli_query($dbc, $query) //'Result' is a new variable which is the result of the dbc and query above
    			or die('Error querying MySQL database'); //Or die trying
    		
    		echo '<b>Email sent to: </b><br /><br />'; //
    		
    		while ($row = mysqli_fetch_array($result)) { //Begin send email loop / where '$row' is a new variable / containing a row of info, or 'array' from '$result' variable
    			$to = $row['email']; //'$to' is a new variable using the 'email' column from '$row' above
    			$first_name = $row['first_name']; //'$first_name' is a new variable using the 'first_name' column from '$row' above
    			$last_name = $row['last_name']; //'$last_name' is a new variable using the 'last_name' column from '$row' above
    			
    			$msg = "Dear $first_name $last_name, \n $message"; //'$msg' is a new variable containing the string "Dear 'see variables above'"
    				
    			mail($to, $subject, $msg, 'from: ' . $from); //'mail' is a PHP command and the rest of this line is the variables with which to construct the message
    			
    			echo $to . '<br />'; //Echo string containing 'to' varable back to page
    			
    		}
    		
    		mysqli_close($dbc); //Close database connection
    		
    	}
    
    if ($output) { //Output form if true
    	
    ?>
    
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    	<p class="body"><label for="subject">Subject:</label>
        <input type="text" id="subject" name="subject" value="<?php echo $subject ?>" /><br /></p>
        <br />
        <p class="body"><label for="message">Message:</label>
          <textarea name="message" cols="50" rows="5" id="message"><?php echo $message ?></textarea>
          <br /></p>
        <br />
        <p class="submit"><input type="submit" id="submit" value="Send message" /></p>
    </form>
    
    <?php
    
    } //Jump back into PHP to close if statement
    
    ?>
    
    </p>

  • #2
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    change
    Code:
        <p class="submit"><input type="submit" id="submit" value="Send message" /></p>
    to
    Code:
        <p class="submit"><input type="submit" name="submit" id="submit" value="Send message" /></p>

  • Users who have thanked Adee for this post:

    Jimbolgs (11-21-2011)

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    32
    Thanks
    15
    Thanked 0 Times in 0 Posts
    That's fixed it — thanks. I take my hat off to you guys that can respond to these questions so quickly. Tell me, do you test the code or are you so experienced you can just read it and spot the errors?

    Thanks again.

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Your error was just a common one. It's a mixture of both. Sometimes we test peoples code, other times the error stands out by itself. It's just one of those things you get better at with experience.

    Talking of experience, avoid even using if ($_POST['submit']) completely (Adee should of mentioned this). See my signature for the reason why and the link below the php code box for an indepth explanation.
    I can't really think of anything to write here now...

  • #5
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    Quote Originally Posted by tangoforce View Post
    Your error was just a common one. It's a mixture of both. Sometimes we test peoples code, other times the error stands out by itself. It's just one of those things you get better at with experience.

    Talking of experience, avoid even using if ($_POST['submit']) completely (Adee should of mentioned this). See my signature for the reason why and the link below the php code box for an indepth explanation.
    i didn't mention it cause i knew for sure you would have LOL

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts


    (YEah right :P)
    I can't really think of anything to write here now...

  • #7
    New Coder
    Join Date
    Nov 2011
    Posts
    32
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Thanks, I'll make a note of that for any 'real-life' future forms, but for now I'm very new to all this so I'll just carry on with my book.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •