我会尝试解释我的问题。我有一个表单,在该表单内部我有一个“选择状态”选项:
<select name="State">
<option value="0" selected="selected">Select a State</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
etc.....
</select>
所以一个例子是:客户填写表格并选择阿拉巴马州作为州。然后他们提交表格,表格连接到数据库,在那里它看到州阿拉巴马州(AL)。然后它从ip部分收集关联到ALbama部分的ip,并将其提交到我的电子邮件地址以及其余的表单信息(名称,电子邮件等等)。它还需要从阿拉巴马州随机选择一个ip,因为在数据库中我有多次Alabama(AL),所以它只选择Alabama ip中的任何一个
+-------+---------------+
| state | ip |
+-------+---------------+
| AL | 67.100.244.74 |
| AK | 68.20.131.135 |
| AZ | 64.134.225.33 |
+-------+---------------+
感谢这个论坛上的一些人和另一个我设法汇总一些PHP代码给你看。我已将此代码添加到自己的php文件中,与html表单分开
<?php
// visit http://php.net/pdo for more details
// start error handling
try
{
// connect
$pdo = new PDO('mysql:host=localhost;dbname=name', 'dbuser', 'pass');
// enable error handling through exceptions
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// create safe query
$query = $pdo->prepare("SELECT ip FROM vincer WHERE state = ? ORDER BY rand() LIMIT 1");
// pass data & execute query (since the data are of string type
// and therefore can be passed in this lazy way)
$query->execute(array($_POST['State']));
// get value
$ip = $query->fetchColumn();
// print out the IP address using $ip
}
catch (Exception $e)
{
echo "sorry, there was an error.";
mail("email@gmail.com", "database error", $e->getMessage(), "From: email@gmail.com");
}
?>
接下来我将php文件名添加到表单的post部分,如下面的代码
<form name="contactform" method="post" action="form.php">
现在好了,当我去测试它一切正常,我没有错误,但我没有收到电子邮件。我检查了垃圾邮件,但它不存在。
因为我没有收到我认为可能需要某种php电子邮件表单的电子邮件。所以我将上面已有的代码添加到下面的代码中
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "1stoptutorials@gmail.com";
$email_subject = "This is a test";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['State']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$state = $_POST['State']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "State: ".clean_string($state)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
if (!mail($email_to, $email_subject, $email_message, $headers))
{
echo "failed to send message";
}
?>
现在,当我发送电子邮件时,我确实收到了电子邮件,但状态部分不正确。这是我作为电子邮件收到的内容
First Name: afdf
Last Name: sfgsdf
Email: sd@fd.com
State: AZ
Comments: ali
你可以看到它正在拉入两个字母的状态,但是我希望它从我的数据库中的ip表中获取AZ的ip号码。这就是我想要它看起来像
First Name: afdf
Last Name: sfgsdf
Email: sd@fd.com
State: 64.134.225.33
Comments: ali
如果有人能看到我错过了什么以及为什么这不是从ip表中获取一个随机的ip地址,那将非常感激
谢谢大家
阿里
答案 0 :(得分:0)
$state = $_POST['State']; // not required
在上述声明之后,你正试图
$email_message .= "State: ".clean_string($state)."\n";
这将发送$ _POST ['State']的任何值。 您必须从表单发送ip或从上面的代码获取ip并使用
$email_message .= "State: ".clean_string($ip)."\n";