1

I am trying to submit a form post to a php page (which has dummy vars) then return the results and display them in the 'userinfo' div on the same page. I suspect I need to post the information to the same page or use Ajax but unsure on how to achieve this.

A few hours of trawling the web hasn't provided any decent examples of what I am trying to do.

any help appreciated:

Anyway here is the code:

        <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 dummy vars:

<?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);

?>
4

5 回答 5

1

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

<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 文件然后编写输出结果的回显语句?像这样的东西:

    <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>
于 2013-06-14T12:55:19.623 回答
0

使用 Jquery 和绑定事件来提交操作。

 $(function (){

 $("#userform").submit(function (){
  $.ajax({
    type: "POST",
    dataType: "json",
    url: "some.php",
    data: {username : $("#username").val()}
    success: function(data) {
       $("#userinfo b").html(data.username);
    }
  });

  });
 });
于 2013-06-14T13:00:06.653 回答
0

我建议使用jQuery.ajax()

$.ajax({
    type: "POST",
    dataType: "json",
    url: "some.php",
    data: {$("#username").val();}
    success: function(data) {
        $("#userval").text(data.username);
    }
});
于 2013-06-14T12:47:32.687 回答
0

您可以使用 jquery 库和这个 javascript 代码。

$(document).ready(function(){
    $('#find').click(function(){
        var form = $(this).closest('form');

        $.post(form.attr('action'), form.serialize(), function (data){
            var c;
            $.each(data, function(i, e){
                c += i + ':' + e;
            });
            $('#userinfo').html(c);
        }, 'json');
    });
});
于 2013-06-14T12:47:41.247 回答
0
<script type="text/javascript">

$(document).ready(function(){

$("#find").click(function(){
    var username  = $("#username").val();
        $.ajax({
        type: 'POST',
        dataType: 'json',
        url: 'includes/find.php',
        data: 'username='+username,
        success: function( data ) {
        //in data you result will be available...show here

        },

error: function(xhr, status, error) {
    alert(status);
    },
dataType: 'text'

});
    });

});



</script>

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

<div id ="userdiv">
  <p>Name (Lastname, firstname):</p>
  </label>
  <input type="text" name="username" id="username" class="inputfield" />
  <input type="button" name="find" id="find" class="passwordsubmit" value="find" />
</div>
</form>
<div id="userinfo"><b>info will be listed here.</b></div>
于 2013-06-14T13:14:28.207 回答