Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Uncaught TypeError: Failed to construct 'FormData'

Post

Uncaught TypeError: Failed to construct 'FormData' [closed]

+3
−2

Closed as not constructive by Alexei‭ on Dec 10, 2021 at 16:03

This question cannot be answered in a way that is helpful to anyone. It's not possible to learn something from possible answers, except for the solution for the specific problem of the asker.

This question was closed; new answers can no longer be added. Users with the reopen privilege may vote to reopen this question if it has been improved or closed incorrectly.

I am trying to submit a form with Ajax from a well-configured shared hosting environment. Emails don't arrive to my email client although I have already sent emails without Ajax from this environment without a problem so the problem shouldn't be in the email server. I assume that emails aren't sent in relation to the following error in browser console:

Uncaught TypeError: Failed to construct 'FormData': parameter 1 is not of type 'HTMLFormElement'. at behavior.js:2 What may cause that problem?


index.php

<!DOCTYPE html>
<html>
	<head>
		<link rel="stylesheet" href="./style.css"></link>
	</head>
	<body>
		<form id="#prcf_form">
			<div class="form">
				<span><label for="prcf_input_name" id="prcf_label_name" class="prcf_label">Name</label></span><span class="prcf_required">*</span>
				<input type="text" name="name" id="prcf_input_name" class="prcf_item" required>
				</input> <!-- required property-->
			</div>
			<div class="prcf_footers">
				<input type="submit" value="Send" id="prcf_submit" class="prcf_finalization_buttons"></input>
			</div>
		</form>
	</body>
	<script src="./behavior.js"></script>
</html>

behavior.php

<?php
	$name = $_POST['name']; # Required;
	$to = 'EMAIL_ADDRESS_NAME@DOMAIN';
	$subject = 'New message from: ' . $name;
	include 'message.php';
	$headers[] = 'MIME-Version: 1.0';
	$headers[] = 'Content-type: text/html; charset=utf-8';
	mail($to, $subject, $message, implode("\r\n", $headers));
?>

message.php

<?php
	$message = <<<LABEL
		<html>
			<head>
			</head>
			<body>
				<div><span class="form_output_col_1">Name: </span><span class="form_output_col_2">$name</span></div>
			</body>
		</html>
	LABEL;
?>

behavior.js

let formToWorkOn = document.querySelector('#prcf_form');
let formData = new FormData(formToWorkOn);
fetch('./behavior.php', {
  method: 'POST',
  body: formToWorkOn
});
History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

2 comment threads

Question closure (1 comment)
Form Data documentation (7 comments)
Question closure
Alexei‭ wrote over 2 years ago

Since the underlying cause is mostly a typo (extra # in the element id) as per this comment and the existing answer) I closed this question.