php - 将表单变量发送到 php 返回结果并放置在 div 中

标签 php javascript jquery ajax json

我正在尝试向 php 页面(具有虚拟变量)提交表单帖子,然后返回结果并将其显示在同一页面上的“userinfo”div 中。我怀疑我需要将信息发布到同一页面或使用 Ajax,但不确定如何实现这一点。

几个小时的网络搜索并没有提供任何像样的例子来说明我正在尝试做的事情。

感谢任何帮助:

无论如何,这是代码:

        <form name="Find User" id="userform" class="invoform" method="post" action="includes/find.php" />
            <div id ="userdiv">
                <p>Name (Lastname, firstname):</p></label><input type="text" name="username" id="username" class="inputfield" />
                <input type="submit" name="find" id="find" class="passwordsubmit" value="find" />
            </div>
        </form>
        <div id="userinfo"><b>info will be listed here.</b></div>

PHP 虚拟变量:

<?php

$user=$_POST['username'];
$username="username";
$gg_groups="gg_usergroups";
$enabled="True";

$result = array(
    'username' => $username,
    'user' => $user,
    'usergroups' => $gg_groups,
    'enabled'=> $enabled
);

echo json_encode($result);

?>

最佳答案

我不确定你到底想做什么?将表单提交到同一页面很容易,只需将操作属性留空,如下所示:

<form name="Find User" id="userform" class="invoform" method="post" action="">

然后将其添加到 php,以检查表单是否已发送:

if(isset($_POST['find'])){
    //Do whatever you want with the form here
}

评论后编辑: 好吧,如果没有所有代码,很难给你一个准确的代码,但是坚持只使用 php,为什么不将表单发送到你想要它显示的页面,包括处理结果的 php 文件,然后写入一条 echo 语句输出结果?像这样的东西:

    <form name="Find User" id="userform" class="invoform" method="post" action="">
        <div id ="userdiv">
            <p>Name (Lastname, firstname):</p></label><input type="text" name="username" id="username" class="inputfield" />
            <input type="submit" name="find" id="find" class="passwordsubmit" value="find" />
        </div>
    </form>
    <div id="userinfo">
    <?php
    if(isset($_POST['find'])){
        include('dummyvars.php');
        echo"Username = ". $username . "<br> User = ". $user ."<br> Usergroups = ". $gg_groups ."<br> Enabled = ". $enabled;
    }
    ?>
    </div>

关于php - 将表单变量发送到 php 返回结果并放置在 div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17108725/

相关文章:

php - 是否可以从 Google Play 获取应用程序的下载次数?

java - PHP:如何从同一 MYSQL 数据库的不同表中用 PHP 编写变量

javascript - 使用 useState() Hook 时 const 变量 'count' 如何更新

javascript - node.js 无法解析 ISOString 日期?

jquery - 恢复“选择的选择”中预先选择的值

PHP session 刷新!

javascript - 使用 PHP 计算多个点的距离

javascript - 我想通过在搜索框中输入经纬度值来在传单 map 中显示标记

javascript - 语法错误: unterminated parenthetical for validating the local file path

javascript - 从数据层中删除所有功能