0

I'm trying to understand how to work with data and so I'm trying to get the value from input fields and write to the json file, but it doesn't work, I would be glad if you help me with this.

html:

<form id="myForm">
  <label for="yourname">Name:</label>
  <input type="text" name="name" placeholder="name" id="name">
  <label for="yourname">Last name:</label>
  <input type="text" name="last-name" placeholder="last name" id="lastName">
  <input type="submit" value="send" id="btn">
</form>

js:

$(function() {
    $("#myForm").submit(function() {
        $.ajax({
            method: "POST",
            url: "form.php",
            data: $("#myForm").serialize(),
            success: function(data) {
                var name = $("#name").val("");
                var lastName = $("#lastName").val("");
            }
        });
        return false; 
    });
});

php:

<?php 
$ToEmail = 'names.json'; 
$EmailSubject = 'форма'; 
$mailheader = "From: ".$_POST["email"]."\r\n";
$MESSAGE_BODY .= "Name: ".$_POST["name"].""; 
$MESSAGE_BODY .= "Last-Name: ".$_POST["last-name"]."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 
?>
9
  • You can't write into json in clientside. Are you using a server for saving json details? Commented Sep 3, 2016 at 16:28
  • yes, i use a server Commented Sep 3, 2016 at 16:30
  • so, what's not working then? maybe you might send the server code. Commented Sep 3, 2016 at 16:30
  • what about this version if i use a php file to send a data to json file (like a form) Commented Sep 3, 2016 at 16:35
  • are you trying to save a json file or trying to send a email from your form? Commented Sep 3, 2016 at 16:38

3 Answers 3

1

You can't write to JSON files directly with Javascript. You should create a PHP script that does this and then send the POST request to this PHP script.

EDIT: Here's a very simple script.

<?php file_put_contents("names.json","{\"name\":\"".$_POST["name"]."\",\"last-name\":\"".$_POST["last-name"]."\"}",FILE_APPEND); ?>
Sign up to request clarification or add additional context in comments.

1 Comment

can you please write the simplest version of php code for this?
1

Try this

 <?php

$inputArray = array(
    'name' => $_POST["name"],
    'last-name' => $_POST["last-name"],
    'email'=>$_POST["email"]
);
$myfile = fopen("myfile.json", "w") or die("Unable to open file!");
$text = json_encode($inputArray);

fwrite($myfile, $text);
fclose($myfile);

echo "file saved";
?> 

1 Comment

Welcome mate. If it works for you accept my answer :)
0

You need to create a Php script to write the received data into a json file.

Try using: http://php.net/manual/en/function.file-put-contents.php

As received data is in $_GET or $_POST, you can easily convert those arrays using the json_encode method: http://php.net/manual/en/function.json-encode.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.