admin管理员组文章数量:1326286
I have added an email input / form to my Wordpress page and want to collect the information from this as JSON data.
The HTML code is:
<form method="get"><p id="myform"><input type="email" name="EMAIL" placeholder="enter email address" required />
<input id="submit" type="submit" value="Sign up" /></p></form>
The php, which is in my functions.php file, is:
if (isset($_POST['submit'])) {
$file = "data.json";
$json_string = json_encode($_POST, JSON_PRETTY_PRINT);
file_put_contents($file, $json_string, FILE_APPEND);
}
I have a data.json file, which is being referenced in the php part of the above code, which sits at the first level of my child theme.
I'm not getting any error messages, and the input field is allowing email addresses to be input, but the data isn't going to my data.JSON file.
I can't seem to work out why?
Any help would be fab.
Emily
I have added an email input / form to my Wordpress page and want to collect the information from this as JSON data.
The HTML code is:
<form method="get"><p id="myform"><input type="email" name="EMAIL" placeholder="enter email address" required />
<input id="submit" type="submit" value="Sign up" /></p></form>
The php, which is in my functions.php file, is:
if (isset($_POST['submit'])) {
$file = "data.json";
$json_string = json_encode($_POST, JSON_PRETTY_PRINT);
file_put_contents($file, $json_string, FILE_APPEND);
}
I have a data.json file, which is being referenced in the php part of the above code, which sits at the first level of my child theme.
I'm not getting any error messages, and the input field is allowing email addresses to be input, but the data isn't going to my data.JSON file.
I can't seem to work out why?
Any help would be fab.
Emily
Share Improve this question asked Nov 20, 2016 at 22:00 pjk_okpjk_ok 9082 gold badges15 silver badges36 bronze badges1 Answer
Reset to default 0I have checked the code. file_put_contents
function accepts the absolute path for the file. Use the 'get_template_directory()' function for the absolute path. Please check the updated code.
<form method="get"><p id="myform"><input type="email" name="EMAIL" placeholder="enter email address" required />
<input id="submit" name="submit" type="submit" value="Sign up" /></p></form>
if (isset($_POST['submit'])) {
$file = get_template_directory() . '/data.json';
$json_string = json_encode($_POST, JSON_PRETTY_PRINT);
file_put_contents($file, $json_string, FILE_APPEND);
}
Note: Code is writing all POST data into the json file. If you just want email address then change json_encode($_POST, JSON_PRETTY_PRINT);
into json_encode($_POST['EMAIL'], JSON_PRETTY_PRINT);
Cheers :)
本文标签: phpInput data from email form not going to JSON file
版权声明:本文标题:php - Input data from email form not going to JSON file 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742210883a2433688.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论