我有表格,并将数据提交给电子邮件。
提交除提名选项以外的所有内容。
这里添加了我的PHP代码和表单代码。
我想从数据库中获取列表,并将它们放入选择选项中。
提交表格后,应发送选项。
<?php
if(isset($_POST['submit'])){
$to = "synergyacademy2014@gmail.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$course = $_POST['course'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'] . $course;
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers,$course);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
}
?>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Course: <select name="course">
<option>Select Course</option>
<option value="1">MBA</option>
<option value="2">MCA</option>
</select><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
答案 0 :(得分:1)
如果要从数据库中获取选项列表,可以尝试使用循环。确保已建立与相应数据库的连接 -
<select name="course">
<?php
$tpl_options = "<option>Select Course</option>";
$query = "SELECT column FROM table [WHERE]";
$courses = mysql_query($query);
$i=1;
while($row = mysql_fetch_array($courses)){
$tpl_options .= "<option value='".$i."'>".$row['column']."</option>";
}
?>
</select>
答案 1 :(得分:1)
<select>
选项,我们需要首先确定哪些优秀的API,例如mysqli
或PDO
,使用。mysqli_*
prepared statement。代码:
<?php
/* ESTABLISH FIRST YOUR CONNECTION; REPLACE NECESSARY DATA BELOW */
$con = new mysqli("YourHost", "YourUsername", "YourPassword", "YourDatabase");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
?>
建立连接后,我们现在可以从您的数据库中获取数据。
Course: <select name="course"> <!-- START OF YOUR SELECT -->
<?php
/* REPLACE NECESSARY COLUMN NAMES AND TABLE NAME BELOW */
if($stmt = $con->prepare("SELECT id, course FROM course_table")){ /* PREPARE YOUR QUERY AND CHECK IF YOUR QUERY IS TRUE */
$stmt->execute(); /* EXECUTE THE QUERY */
$stmt->bind_result($id,$course); /* BIND THE RESULTS TO THESE VARIABLES */
while($stmt->fetch()){ /* THEN WE GET ALL THE ROWS BASED FROM THE QUERY WE HAVE CREATED */
echo '<option value="'.$id.'">'.$course.'</option>'; /* STORE THE VALUES WE HAVE FETCHED TO THIS OPTIONS */
} /* END OF WHILE LOOP */
$stmt->close();
} /* END OF YOUR PREPARED STATEMENT */
?>
</select>