[英]How can I send input tag value from HTML file to PHP file without form tag?
I have the following input and button tag in my HTML code:我的 HTML 代码中有以下输入和按钮标签:
<input id = "inputarea" type = "text" name = "inputarea" size = "40">
<button id = "sendMessage">Submit</button>
I want to take the value of the input tag and send it to a variable in my PHP file every time the user clicks on the button.每次用户单击按钮时,我想获取输入标签的值并将其发送到我的 PHP 文件中的变量。 I don't want to use a form tag because I don't want the HTML page to get redirected to the PHP file when the user clicks on the submit button.
我不想使用表单标签,因为我不希望 HTML 页面在用户单击提交按钮时重定向到 PHP 文件。 Instead, I simply want the input tag value to be sent to PHP where the value will be processed and then sent somewhere else.
相反,我只是希望将输入标签值发送到 PHP ,在那里将处理该值,然后将其发送到其他地方。 How can I do this?
我怎样才能做到这一点?
You are wanting to learn what AJAX is and how to use it.您想了解 AJAX 是什么以及如何使用它。 It's not a matter of adding form tags.
这不是添加表单标签的问题。 Its a matter on how you post the form data.
它与您如何发布表单数据有关。
jQuery Ajax POST example with PHP jQuery Ajax POST 示例与 PHP
You dont even need jquery.你甚至不需要 jquery。 Is just the first example on a google search.
只是谷歌搜索的第一个例子。 You can use vanilla js to accomplish this.
您可以使用 vanilla js 来完成此操作。
HTML HTML
<textarea id = "displayChat" rows = "25" cols = "41" readonly></textarea>
<input id = "inputarea" type = "text" name = "inputarea" size = "40">
<button id = "sendMessage">Submit</button>
AJAX AJAX
$(document).ready(function() {
$("#sendMessage").click(function () {
var message = $("#inputArea").val();
$.ajax({
type: "POST",
url: "chatbox.php",
data: {messageText: message},
success: function(response) {
$("#displayChat").append(response);
}
});
});
});
chatbox.php聊天框.php
$message = $_POST["messageText"];
echo "Message successfully retrieved by PHP";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.