【问题标题】:Save web form info to xml?将 Web 表单信息保存到 xml?
【发布时间】:2014-10-23 06:02:59
【问题描述】:

我来自设计背景,只有 HTML 和 CSS 方面的经验,所以我对任何涉及 PHP 的东西都非常陌生,并且在最好的情况下只能掌握剪切和更改代码。

我找到了一些 PHP 代码,可以让我创建一个 Web 表单,在使用时包含用户名、电子邮件、电子邮件主题和电子邮件正文 - 这已经过测试并且工作正常,我正在接收来自表格没有问题。

我现在要做的是让表单创建/更新一个 XML 文件,每次有人使用表单时,我都会使用用户信息托管该文件。

我尝试搜索,但实际上找不到任何有用的东西。

我的 PHP:

<?php

$ip = $_POST['ip']; 
$httpref = $_POST['httpref']; 
$httpagent = $_POST['httpagent']; 
$visitor = $_POST['visitor']; 
$visitormail = $_POST['visitormail']; 
$notes = $_POST['notes'];
$attn = $_POST['attn'];

if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,"."))) 
{
echo "<h2>Use Back - Enter valid e-mail</h2>\n"; 
$badinput = "<h2>Feedback was NOT submitted</h2>\n";
echo $badinput;
die ("Go back! ! ");
}

if(empty($visitor) || empty($visitormail) || empty($notes )) {
echo "<h2>Use Back - fill in all fields</h2>\n";
die ("Use back! ! "); 
}

$todayis = date("l, F j, Y, g:i a");

$attn = $attn ; 
$subject = $attn; 

$notes = stripcslashes($notes); 

$message = "
Subject: $attn \n
Message: $notes \n 
From: $visitor ($visitormail)\n
Additional Info : IP = $ip \n
Browser Info: $httpagent \n
";

$from = "From: $visitormail\r\n";


mail('youremail@website.com', $subject, $message, $from);

?>

我找到了这个 PHP 并尝试调整它以使用我的代码,但它似乎不起作用:

<?php 

$file="test_xml.xml";

$visitor="Name";
$ip="IP";

//load xml object
$xml= simplexml_load_file($file);

//assign name
$xml->auth->ids = $visitor;

//assign ip
$xml->auth->key = $ip;

//store the value into the file
file_put_contents($file, $xml->asXML());

?>

我是否走在正确的轨道上?

【问题讨论】:

  • 我会推荐DOMDocument——它也很容易使用。

标签: php xml


【解决方案1】:

只需用这个替换你的 php 中的 XML 代码并尝试一下

$xml = new SimpleXMLElement('');

    $mydata = $xml->addChild('VisitorInfo');
    $mydata->addChild('Visitor',$Visitor);
    $mydata->addChild('Key',$ip);

    $mydata->PHP_EOL;

mysql_close($db);

$fp = fopen("VisitorData.xml","wb");


fwrite($fp,$xml->asXML());

fclose($fp);

【讨论】:

  • 我已将它添加到我的 php 代码中,但它似乎没有做任何事情。我的消息发送正常且未出错,但似乎没有将日期保存到 XML
猜你喜欢
  • 1970-01-01
  • 2012-09-13
  • 1970-01-01
  • 1970-01-01
  • 2022-08-19
  • 2021-10-11
  • 1970-01-01
  • 1970-01-01
  • 2020-08-10
相关资源
最近更新 更多