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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

I 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