Jump to content
Sign in to follow this  
Ali J

Submit Query

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.

Share this post


Link to post
Share on other sites
Maybe it is better if you made 1 page for the form and another for the sending of the email.
Could you give me an example. I will make an other page but how do I link them?

Share this post


Link to post
Share on other sites

mmmmmmmmmm

Edited by Panta

Share this post


Link to post
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.

Edited by Deirdre's Dad

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...