Jump to content

Submit Query


Ali J

Recommended Posts

Hello,I have this simple PHP and HTML code but it just won't work.It's supposed to send an email using PHP.When I press the submit button, it does nothing.Here is the code:

<html><?phpecho "From:" . " " . "<input type='text' name='from' value='{$from}'>";echo "To:" . " " . "<input type='text' name='to' value='{$to}'>";echo "Subject:" . " " . "<input type='text' name='subject' value='{$subject}'>";echo "Message:" . " " . "<input type='text' name='message' value='{$message}'>";echo "<input type='hidden' name='headers' value='{$headers}'>";echo "<input type='submit' name='send' value='Send Message' action='mail($to,$subject,$message,$headers)'>";?></html>

I haven't worked on it for long.Just a minute or two.It should send a message .I was using the code below, and it worked fine but it had no text boxes so I tried change it to the one at the top.

<?php$to = "email@example.com";$subject = "Subject";$message = "Message";$from = "youremail@example.com";$headers = "From: $from";mail($to,$subject,$message,$headers);echo "Mail Sent.";?>

In this one the email was sent as soon a you loaded the page, because everything was predefined.Any ideas on how to get the one at the top to work?Any help is always appreciated.Thank you,Ali J.

Link to comment
Share on other sites

Your revised code will not work. When we mix PHP with HTML, the result is something that gets output to the user, and then PHP is done. Clicking a button will not run the PHP embedded in the document, because the document no longer sees the PHP, and PHP doesn't run on a browser.What we want is a system that will send form data to the server; when it reaches the server, a PHP script extracts the data and sends the mail.You can stick with some of your original PHP. As you say, the variables are predefined, and you don't want that. So change the definitions to respond to user input. We'll look at how the form should look first. (The form document is the one that the user loads into the browser. Call it something like mail.html :

<form method="post" action="myscript.php">	<input type='text' name='to'>	<input type='text' name='subject'>	<input type='text' name='message'>	<input type='submit' value='Send mail'></form>

Notice how this contains no PHP code. Notice also that the submit button does not have an action attribute. That belongs in the form tag.myscript.php is just a revision of the code you had earlier:

<?php 	$to = $_POST['to']	$subject = $_POST['subject']	$message = $_POST['message']	$from = "youremail@example.com";	$headers = "From: $from";	mail($to, $subject, $message, $headers);	echo "Mail Sent.";?>

Notice how the name of the $_POST array matches the method of your form. Notice how the name of the script matches the action attribute of the form. Notice how the indexes of the $_POST array match the name attributes of the form inputs. All these items have to match if you are to communicate with the correct script and extract the information that the user sends.Of course, that code will break if your user leaves any of the information blank. It will also break if the information is incorrect in some way. So we'll add some checking. Notice the if blocks:

<?php	$msg = "I could not send your mail";	if (!empty($_POST['to']) && !empty($_POST['subject']) && !empty($_POST['message']) ) {		$to = $_POST['to']		$subject = $_POST['subject']		$message = $_POST['message']		$from = "youremail@example.com";		$headers = "From: $from";		if (mail($to, $subject, $message, $headers) ) {			$msg = "Mail Sent.";		}	}	echo $msg;?>

The document that contains the form CAN be the same document that sends the mail, but let's learn one trick at a time. Keep them in separate documents for now. Master the basics of form interaction.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...