Jump to content

Recommended Posts

Hi I'm new here and a very novice php coder, so please forgive me if this is a stupid question.

 

I am trying to code a form which has the following characteristics;

 

Pre filled email (to)

Pre filled subject (selection)

Pre filled message (selection) with links (html)

 

I can code the form itself, html is easy-ish, what I'm having trouble with is populating the subject and message fields.

 

I know how to include an external file with variables in, but I have no idea how to make them into a selectable form, also I have found that the <textarea> tag strips html so live links cannot be included - which I need.

 

I am not using a database except if you consider a flat-file a DB, settings are stored in a php document.

 

Any and all help welcomed smile.gif

 

Thanks smile.gif

Link to comment
https://forums.phpfreaks.com/topic/273608-selectable-messages-in-mail-form/
Share on other sites

Just basic at the moment as I have no idea where to begin if I'm honest.

 

I'm using the standard phpmailr & smtp classes along with the form parser here:

<?
ob_start();
if(isset($_POST['btnSubmit']))
{
require("class.phpmailer.php");


$mail = new PHPMailer();


//Your SMTP servers details


$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "yourmailserver.com or localhost; // specify main and backup server or localhost
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "example@yourdomain.com"; // SMTP username
$mail->Password = "password"; // SMTP password
//It should be same as that of the SMTP user


$redirect_url = "http://".$_SERVER['SERVER_NAME']; //Redirect URL after submit the form


$mail->From = $mail->Username; //Default From email same as smtp user
$mail->FromName = "Display Name";


$mail->AddAddress("info@yourdomain.com", "your name"); //Email address where you wish to receive/collect those emails.


$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML


$mail->Subject = $_POST['subject'];
$message = "Name of the requestor :".$_POST['fullname']." \r\n <br>Email Adrress :".$_POST['email']." \r\n <br> Query :<a href="http://www.google.com">GOOGLE!</a>".$_POST['query'];
$mail->Body = $message;


if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}


echo "Message has been sent";
header("Location: $redirect_url");
}
?>

 

And the HTML elements here:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact Us</title>
<script language="javascript">
function validate()
{
if(document.getElementById("fullname").value=='')
{
alert("Please provide your full name");
document.getElementById("fullname").focus();
return false;
}
if(document.getElementById("email").value=='')
{
alert("Please provide email address");
document.getElementById("email").focus();
return false;
}
if(document.getElementById("subject").value=='')
{
alert("Please provide subject line");
document.getElementById("subject").focus();
return false;
}
if(document.getElementById("query").value=='')
{
alert("Please enter your query");
document.getElementById("query").focus();
return false;
}

}
</script>
</head>


<body>
<form name="form" method="post" action="contact.php">
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1">
<tbody>
<tr><td colspan="3" align="center"><h2>Contact Us</h2></td></tr>
<tr>
<td width="39%" align="right" ><strong>Full Name<font color="#ff0000"> *</font></strong></td>
<td width="1%" ><div align="center">:</div></td>
<td width="60%" height="25"><input type="text" name="fullname" id="fullname" /></td>
</tr>
<tr>
<td width="39%" align="right" ><strong>Email<font color="#ff0000"> *</font></strong></td>
<td width="1%" ><div align="center">:</div></td>
<td width="60%" height="25"><input type="text" name="email" id="email" /></td>
</tr>
<tr>
<td align="right"><strong>Subject<font color="#ff0000"> *</font></strong></td>
<td ><div align="center">:</div></td>
<td height="25"><input type="text" name="subject" id="subject" /></td>
</tr>
<tr>
<td align="right" valign="top"><strong>Please Type Your Query If Any<font color="#ff0000"> *</font> </strong></td>
<td ><div align="center">:</div></td>
<td height="25"><textarea name="query" id="query" rows="5" cols="30"></textarea></td>
</tr>
<tr>
<td colspan="2" align="right"></td><td><input name="btnSubmit" type="submit" onclick="javascript: return validate();" value="Submit" /></td>
</tr>
</tbody>
</table>
</form>


</body>
</html>

 

These are the basics which I have changed to work, then used variables to pre-fill some fields such as name and email.

Marking this as solved because I added a small piece of javascript to handle it which works.

 

Here's the code for anybody trying the same thing:

 

<form name="form" method="post" action="contact.php">
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1" class="compare">
<tbody>
<tr>
<td class="col1"><label for="to_email">Email</label></td>
<td class="col2"><input type="text" name="email" id="email" value="<?php echo $contact_email ;?>"/></td>
<td class="note"><?php if (isset($form_incomplete)) { echo '<span class="red">Required.</span>'; } else { echo 'Required.';} ?> Pre-filled from Data, replace if required.</td>
</tr>
<tr>
<td><label for="fullname">Name</label></td>
<td><input type="text" name="fullname" id="fullname" value="<?php echo $contact_first.' '.$contact_last; ?>" /></td>
<td class="note"><?php if (isset($form_incomplete)) { echo '<span class="red">Required.</span>'; } else { echo 'Required.';} ?> Pre-filled from Data, replace if required.</td>
</tr>
<tr>
<td colspan="3">
<center>
<select name="menu">
<option value="#">Select subject...</option>
<option value="This is for value no.2">Appointment</option>
<option value="This is for value no.2">Value No. 2</option>
<option value="This is for value no.3">Value No. 3</option>
<option value="This is for value no.4">Value No. 4</option>
<option value="This is for value no.5">Value No. 5</option>
<option value="This is for value no.6">Value No. 6</option>
</select>
<input value="Populate" type="button" onclick="document.form.accept.value=document.form.menu.options[document.form.menu.selectedIndex].value;document.form.textareaaccept.value=document.form.menu.options[document.form.menu.selectedIndex].value;">
 
<select name="menu2">
<option value="#">Select message...</option>
<option value="This is for value no.1">Value no.1</option>
<option value="This is for value no.2">Value No. 2</option>
<option value="This is for value no.3">Value No. 3</option>
<option value="This is for value no.4">Value No. 4</option>
<option value="This is for value no.5">Value No. 5</option>
<option value="This is for value no.6">Value No. 6</option>
</select>
<input value="Populate" type="button" onclick="document.form.message.value=document.form.menu2.options[document.form.menu2.selectedIndex].value;document.form.textareamessage.value=document.form.menu2.options[document.form.menu2.selectedIndex].value;">
</center>
</td>



</tr>
<tr>
<td><label for="subject">Subject</label></td>
<td><input type="text" id="accept" name="subject"/></td>
<td class="note"><?php if (isset($form_incomplete)) { echo '<span class="red">Required.</span>'; } else { echo 'Required.';} ?> Change as you wish.</td>


</tr>
<tr>


<td class="indent"><label for="query">› Default Message</label></td>
<td><textarea name="query" id="message" rows="5" cols="30"><a href="">test</a></textarea></td>
<td class="note">Change as you wish.</td>



</tr>


</tbody>
</table>
<input name="btnSubmit" type="submit" onclick="javascript: return validate();" value="Send Email" />
</form>

Javascript validation is not security at all, since it's trivial to circumvent it: Just turn off JS in the browser, and you can send whatever you like. That coupled with the fact that your PHP doesn't do any validation at all, means that someone who wanted could send you whatever they like. This includes either an empty form, or HTML code that hides your original mail and shows spam adverts instead.

Since there is no real need for your mail to be sent as HTML, I recommend using standard plain text mails instead. Also, remove the ob_start () from the code, it doesn't give you any advantage, and use the full PHP tags.

 

I've posted an example of how I do the contact form processing in my framework. In there you'll see that I use a $page variable, to hold the page I want the template engine to read the HTML contents from. This file is read after the file containing the posted code exits, and as such I can easily repopulate the form with the data that the user posted, should there be a problem with the validation.

This also means that I don't have any echos, or any other output of any kind, in my PHP scripts. Which makes it a lot easier to write applications that change their behaviour, depending upon user-input and other states internally. After all, if something fails I can just opt not to send the previously generated content. While it would be impossible to take back something already sent to the client.

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.