O'Reilly Forums: Basic "form Submission" Code - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Basic "form Submission" Code basic "form submission" code from head first php and mysql not working

#1 User is offline   necker 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 26-May 14

Posted 26 May 2014 - 09:37 PM

Hi all,

I browsed through various forums looking for a solution to the problem i am facing. i came upon a few similar problems others were facing but solution to their code does not seem to work with my code. i'll be grateful if someone could please point me in the right direction.

This is the notice that my browser shows me- Notice: Undefined variable: subject in C:\wamp\www\.....\sendemail4.php on line 69 Call Stack #TimeMemoryFunctionLocation 10.0010247784{main}( )..\sendemail4.php:0 ">

Also, please find attached a screenshot of what i am getting after running the php script in my localhost.

Here's the code from Head First php mysql book chapter 4 that i am trying to run-

<html>
<head>
	<title> Make Me Elvis - Send Email </title>
</head>
<body>
	<p><strong>Private:</strong> For Elmer's use only <br />
		Write and send an email to mailing list numbers.</p>

<?php

//Detecting form submission  using isset() function	
//Making forms sticky 

	if (!empty($_POST['submit'])) {

		$from = 'prasadwalvekar@yahoo.com';
		$subject = $_POST['subject'];
		$text = $_POST['elvismail'];
		$output_form = false;

		if( empty($subject) && empty($text) ) {
			//Both $subject and $text are blank
			echo 'You forgot the email subject and body text';
			$output_form = true;

		}

		if( empty($subject) && !empty($text) ) {
			//$subject is empty and $text is not empty
			echo 'You forgot email subject.';
			$output_form = true;
		}

		if( !empty($subject) && empty($text) ) {
			//$subject is not empty and $text is empty
			echo 'You forgot email body text.';
			$output_form = true;
		}
	}

	else {
			
		$output_form = true;	//if form's never been submitted, show it!
	}

	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)) {
			$to = $row['email'];
			$first_name = $row['first_name'];
			$last_name = $row['last_name'];
			$msg = "Dear $first_name $last_name, \n $text";
			mail($to, $subject, $msg, 'From:' . $from);
			echo 'Email sent to: ' . $to . '<br />';
		}
		mysqli_close($dbc);
	}

	if ($output_form) {
?>

	<form method="post" action="<? php echo $_SERVER['PHP_SELF']; ?>">
		Subject of email: <input type="text" name="subject" value=" <?php echo $subject; ?> "><br />
		Body of email: <textarea name = "elvismail" rows="8" cols="40"><?php echo $text; ?></textarea><br /> <br />
		<input type = "submit" name = "submit" />
	</form>

<?php
	
	}
?>

</body>
</html>

Attached thumbnail(s)

  • Attached Image: oreilly.jpg

0

#2 User is offline   lemuel 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 577
  • Joined: 13-April 09

Posted 27 May 2014 - 06:09 AM

See comments regarding Page 204 here:
http://www.oreilly.c...n=9780596006303
0

#3 User is offline   necker 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 26-May 14

Posted 27 May 2014 - 10:04 PM

View Postlemuel, on 27 May 2014 - 06:09 AM, said:

See comments regarding Page 204 here:
http://www.oreilly.c...n=9780596006303


Thanks a lot.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users