Skip to content

Instantly share code, notes, and snippets.

@mikestratton
Last active November 23, 2022 00:15
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mikestratton/f1f2160fccae791e30b6 to your computer and use it in GitHub Desktop.
Save mikestratton/f1f2160fccae791e30b6 to your computer and use it in GitHub Desktop.
Save form data in JSON file.
<?php
if(isset($_POST['submit'])) {
$file = "data.json";
$arr = array(
'name' => $_POST['name'],
'email' => $_POST['email'],
'phone' => $_POST['cell'],
'birthday' => $_POST['dob'],
'years' => $_POST['study']
);
$json_string = json_encode($arr);
file_put_contents($file, $json_string);
echo $json_string;
}
?>
<!doctype html>
<html>
<head>
</head>
<body>
<div style="text-align: center;">
<h1>Form</h1>
<form name="form1" method="post" action="">
<p>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="Your full name" autofocus required>
</p>
<p>
<label for="email">Email: </label>
<input type="email" name="email" id="email">
</p>
<p>
<label for="cell">Cell: </label>
<input type="tel" name="cell" id="cell">
</p>
<p>
<label for="dob">Date of birth: </label>
<input type="date" name="dob" id="dob">
</p>
<p>
<label for="study">Years of art study: </label>
0 <input type="range" name="study" id="study" min="0" max="16"> 16
</p>
<p style="text-align: center;">
<input type="submit" name="submit" id="submit" value="Submit">
</p>
</form>
</div>
</body>
</html>
@dnyaneshdeshpande2772
Copy link

The resultant json includes "submit": "Submit". How to avoid the same ?

@mikestratton
Copy link
Author

I have updated to remove submit from being inserted into json.

@dnyaneshdeshpande2772
Copy link

dnyaneshdeshpande2772 commented Jan 20, 2020 via email

@dnyaneshdeshpande2772
Copy link

Hi @mikestratton,
Pardon my ignorance, but this is the very first time that I am dealing with PHP and json.

Can you please advise how can the following heirarchy be set up in the array ?

"transactionAmount": {
"total": "1",
"currency": "036"
}

@mr-who-0903
Copy link

it is not saving multiple json objects. how to do that???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment