简体   繁体   English

如何在没有表单标签的情况下将输入标签值从 HTML 文件发送到 PHP 文件?

[英]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.

相关问题 如何在没有 PHP 输入标签的情况下通过 POST 将值发送到其他页面 - how can i send a value to other page via POST without Input Tag in PHP 如何使用php mime从两个不同的输入文件标签发送2个附件文件? - How do i send 2 attachment file from two different input file tag using php mime? 如何从自动完成中获得单击的输入单选值以将表单发送到 php 文件 - How can I get clicked input radio value from autocomplete to send in form to php file 如何从php中的HTML输入标签获取选定的文件路径? - How to get selected file path from HTML input tag in php? 如何通过HTML表单发送PHP数组(使用输入标签) - How to send a PHP array via HTML form (using input tag) 如何在PHP文件中使用“复选框”输入标签? - How can I use 'checkbox' input tag in the PHP file? PHP 文件中 HTML 文件的回显标签值 - Echo tag value from HTML file in PHP file 我怎么能放一个Html<form> PHP 函数中的标记,这会将我重定向到另一个 .php 文件? - How can I put an Html <form> tag inside a PHP function, which would redirect me to another .php file? 如何在没有本地存储的情况下通过 PHP 将文件上传从 HTML 表单发送到 S3? - How do I send a file upload from an HTML form to S3 via PHP without local storage? 从HTML表单标签调用PHP文件中的不同函数 - Calling different functions inside a PHP file from HTML form tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM