I built a JAVASCRIPT into a PHP file that creates links to create links with the POST method, unfortunately it doesn't work, what am I doing wrong? which quotation marks should I choose in javascript?
echo '<form name="' . $i . '" method="post" action="?q=' . $query . '"><button type="submit" class="button" name="' . $i . '" value="' . $i . '">' . $i . '</button></form>';
echo '<script>
var button = document.querySelector(`form[name="` . $i .`"] > button`);
button.addEventListener(function() {
document.querySelector("form[name="` . $i .`"]").submit();
});
</script>';
}
You can use \' (backlash followed by ').
See this link where I tested it: https://onlinegdb.com/FF_Um4YdS
<?php
// random data for test
$i = "random-form";
$query = "nothing";
echo '<form name="' . $i . '" method="post" action="?q=' . $query . '"><button type="submit" class="button" name="' . $i . '" value="' . $i . '">' . $i . '</button></form>';
echo '
<script>
var formButton = "form[name=\''. $i .'\'] > button";
var button = document.querySelector(formButton);
button.addEventListener(function() {
document.querySelector("form[name=\'' . $i .'\']").submit();
});
</script>';
?>