gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:10:54 25 4
gpt4 key购买 nike

我正在尝试向 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com