I want to send the data ('ABCDEF','23','1234567890','abc@def.com' and the image 'profile.jpg'
), into my php
script, with the help of FETCH api.
My HTML:
<form id="details" class="form">
Full name: <strong name="name_1">ABCDEF</strong><br><br>
ID No:<strong name="org_number_1">23</strong><br><br>
Mobile No:<strong name="ph_number_1">1234567890</strong><br><br>
E-mail: <strong name="email_1">abc@def.com</strong><br><br>
ID Card: <img src="profile.jpg" alt="preview" name="image" style="width: 100px; height: 100px;"><br><br>
<button id="go" onclick="submit()"type="button" value="submit">Submit</button>
</form>
My javascript:
function submit(){
var nme=document.getElementsByName("name_1")[0].innerHTML;
var id=document.getElementsByName("org_number_1")[0].innerHTML;
var phone=document.getElementsByName("ph_number_1")[0].innerHTML;
var email=document.getElementsByName("email_1")[0].innerHTML;
var img=document.getElementsByName("image")[0].src;
var dataString = {"name": nme, "email": email, "ID No": id, "Contact no": phone, "image": img};
const dForm = document.getElementById('details');
dForm.addEventListener('submit', function(e) {
e.preventDefault();
fetch("database_registration.php",{
method: 'post',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(dataString),
}).then(function (response){
return response.text();
}).then(function (text){
console.log(text);
}).catch(function (error){
console.error(error);
})
});
}
My php:
<?php
$post_data = file_get_contents("php://input");
$data = json_decode($post_data);
var_dump($data); //shows NULL
?>
Why is my $data
object NULL
? How do I fetch those data?
EDIT: I have also followed this step