加载PHP的内容与jQuery AJAX加载、内容、AJAX、PHP

2023-09-10 20:09:57 作者:可可欧尼酱

我的问题:

我的index.html:

I have index.html:

<form action="toload.php" method="post">
Input: <input type="text" name="something" value="" /><br />
<input type="submit" value="Submit!" />
</form>

toload.php 是这样的:

<?php

echo "Your input was: " . $_POST["something"];

?>

现在的问题是很简单的。

The question is quite simple.

当我preSS的提交!按钮,我想动态加载内容 toload.php index.html的无刷新的需要。

When I press the Submit! button, I would like to dynamically load the content toload.php in index.html without the need of a refresh.

在此先感谢!请评论任何需要澄清。

Thanks in advance! please comment for any needed clarification.

修改,更详细的解释:

我不知道我是明确的(或者也许我不理解的答案做我缺乏技术技能),所以我给它一个去。 (重写)

I'm not sure I'm being clear (or maybe I'm not understanding the answers do to my lack of technical skills) so I'll give it another go. (re-write)

我有一个HTML的一个提交按钮,通过POST方法发送的变量。

I have an HTML for with a submit button that sends a variable through POST method.

此变量使用的PHP文件,并有一定的处理后,它插入和更新MySQL数据库和呼应了一些其他的东西。

This variable is used by a PHP file and after a certain process, it inserts and update a MySQL database and echoes out some other stuff.

这是工作得很好。

但现在我想通过避免在网页重载来提高它(要去的.php)。

But now I want to improve it by avoiding the page "reload" (going to the .php).

我要那当属我的HTML文件的输出HTHL在我的HTML页面可以动态显示。

I want the HTHL that comes as an output of my HTML file to be dynamically shown in my HTML page.

是不是更清楚了吧?

推荐答案

像这样使用jQuery:!

something like this with jQuery!:

<form action="toload.php" method="post">
Input: <input type="text" id="something" name="something" value="" /><br />
<input type="submit" value="Submit!" onclick="submitme()" />
<div id="something2"></div>
</form>

和功能提交:

function submitme(){
var tosend=document.getElementById("something").value;
$.ajax({
        type: 'POST',
        url: 'toload.php',
        data: 'something='+tosend,
        success: function(msg){
            if(msg){
                document.getElementById("something2").innerHTML=msg;
            }
            else{
                return;
            }
        }
    });
}