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>';
}
My Answer:
You can generate a POST method link using JavaScript in a PHP file by using the following code:
php
echo '';
echo 'Submit Form';
?>
In this code snippet, we are creating a JavaScript function `submitForm()` that dynamically creates a form element with the POST method and adds a hidden input field with a parameter and value. When the link is clicked, the form is submitted.
Make sure to replace `"your_target_url_here"` with the actual URL you want to submit the form to, and `"param1"` and `"value1"` with the parameter and value you want to send.
As for the quotation marks, you can use either single `'` or double `"` quotation marks in JavaScript. Just make sure to escape them properly within the PHP echo statement.
Rate this post
5 of 5 based on 5018 votesComments