dounieyan2036 2017-04-19 10:29
浏览 71
已采纳

创建PHP iframe表单以将数据发布到电子邮件地址以及数据库表

The first thing I'm struggling to figure out is if it's possible to add this PHP form to an iframe so I can drop it on several different sites (Facebook, Wordpress, personal sites etc). If not how else can I create a form that will allow me to drop the iframe code on different sites and still function correctly?

Currently my form just submits the data to my email but I want the data to also get posted and stored within a database. Can I do this within an iframe?

Below is my form..

HTML

<form action="mail.php" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Phone</p> <input type="text" name="phone">

<p>Request Phone Call:</p>
Yes:<input type="checkbox" value="Yes" name="call"><br />
No:<input type="checkbox" value="No" name="call"><br />

<p>Website</p> <input type="text" name="website">

<p>Priority</p>
<select name="priority" size="1">
<option value="Low">Low</option>
<option value="Normal">Normal</option>
<option value="High">High</option>
<option value="Emergency">Emergency</option>
</select>
<br />

<p>Type</p>
<select name="type" size="1">
<option value="update">Website Update</option>
<option value="change">Information Change</option>
<option value="addition">Information Addition</option>
<option value="new">New Products</option>
</select>
<br />

<p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>

PHP

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$call = $_POST['call'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST['type'];
$message = $_POST['message'];
$formcontent=" From: $name 
 Phone: $phone 
 Call Back: $call 
 Website: $website 
 Priority: $priority 
 Type: $type 
 Message: $message";
$recipient = "youremail@here.com";
$subject = "Contact Form";
$mailheader = "From: $email 
";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
  • 写回答

1条回答 默认 最新

  • dsm0688 2017-04-19 10:46
    关注

    Yes, you can do this.

    If you host the iframe on your host, you have direct access to your database. The mail.php file should do the posting.

    Use this inside your code, before or after mailing the message:

    define('DBHOST', 'localhost');
    define('DBNAME', '');
    define('DBUSER', '');
    define('DBPASS', '');
    
    function nuukDb() {
        $nuukDb = new PDO('mysql:host=' . DBHOST . ';dbname=' . DBNAME . ';charset=utf8', DBUSER, DBPASS);
        $nuukDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $nuukDb->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    }
    
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    
    $nuuk = $nuukDb->prepare("INSERT INTO tablename (value1, value2, value3) VALUES ('$name', '$email', '$phone')");
    $nuuk->execute();
    

    Reference: https://github.com/wolffe/nuuk

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)