PHP Form Does Not Send Content To Webhook
one text
I am trying to send a form's content (an HTML form) via a Discord Webhook, to an embed message in a channel in my discord. While I am quite familiar with HTML, I am not as familiar with PHP, but I am not seeing any errors that would lead me to believe that my PHP code should not work.
This is the contact form in the HTML.
<form role="form" method="post" action="contact.php">
<div class="row">
<div class="form-group half">
<label class="form-control-label" for="exampleInputFirstName">Name <span
class="required-label">*</span></label>
<input type="text" class="form-control" id="exampleInputFirstName"
placeholder="Enter First Name">
</div>
<div class="form-group half">
<label class="form-control-label" for="exampleInputLastName">Last Name <span
class="required-label">*</span></label>
<input type="text" class="form-control" id="exampleInputLastName"
placeholder="Enter Last name">
</div>
</div>
<div class="form-group">
<label class="form-control-label" for="exampleEmail">Email <span
class="required-label">*</span></label>
<input type="email" class="form-control" id="exampleEmail" placeholder="Enter Email">
</div>
<div class="form-group">
<label class="form-control-label" for="exampleInputComment">Your Message <span
class="required-label">*</span></label>
<textarea id="exampleInputComment" class="form-control" rows="5" cols="80"></textarea>
</div>
<button type="submit" class="btn btn-default btn-lg">Submit</button>
</form>
This is the contact.php. This should trigger on submission of the form, create an array for the message, take each form input and create a field for it in the embed, create the embed, and then add the embed array to the message array. Then options are set for the POST to the webhook, and then it executes the POST.
<?php
function SendForm() {
require_once ('config.php');
$msg = array();
$desc = '';
foreach ($_POST as $name => $input) {
if ($input == null) {
$result = "Blank";
} else {
$result = $input;
}
$name_formatted = str_replace('_', ' ', $name);
$desc .= '**'.htmlspecialchars(ucwords($name_formatted)).':**'."\n".''.htmlspecialchars($result).''."\n\n".'';
}
$embed = array("embeds" => array(0 => array("title" => $embed_title, "description" => $desc, "color" => $embed_color),), "username" => $username);
$content = array_merge($msg, $embed);
$webhook = curl_init($webhook_url);
curl_setopt($webhook, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($webhook, CURLOPT_POSTFIELDS, json_encode($content));
curl_setopt($webhook, CURLOPT_RETURNTRANSFER, true);
return curl_exec($webhook);
}
if (SendForm() == null) {
header("Location: {$_SERVER['HTTP_REFERER']}?r=success");
exit();
} else {
header("Location: {$_SERVER['HTTP_REFERER']}?r=error");
exit();
}
?>
I don't get any errors, so it is unclear to me what the problem is.
Source