簡體   English   中英

如何從html表中獲取數據並將其放在php文本字段中?

[英]how to get data from html table and put it in php text fields?

這是我創建的表:

    <table class="table table-striped table-bordered" id="example">
    <thead>
    <?php
        foreach ($fields as $field_name => $field_display){
             echo "<th>".$field_display."</th>";
        }
    ?>
    </thead>
    <tbody>
    <?php
        foreach ($users as $row){
        echo "<tr>";
        foreach($fields as $field_name => $field_display){
            if($field_name == 'username'){
            echo "<td><a href='#' onclick=\"edituserdata('".$row->username."')\">".$row->$field_name."</a></td>";
        }else{
            echo "<td>".$row->$field_name."</td>";
        }
        }
        echo "</tr>";
        }
    ?>
    </tbody>
    </table>

這是當我單擊一行時出現的引導程序模式:

    <!-- Modal -->
<div id="myModal1" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Update you're data</h3>
</div>
<div class="modal-body">
        <?php echo form_open('main/update'); ?>

        <?php 

        $name = array(
                'name' => 'name',
                'id' => 'name',
                'value' => set_value('name')
        );
        ?>

        <div class="control-group">
            <label class = "control-label" for="name">Name:</label>
            <div>
                <input type="text" name="name" placeholder="Name" id="name" value="">
                <?php echo form_error('name', '<span class="alert-error help-inline">', '</span>')?>
            </div>
        </div>

        <div class="control-group">
            <label class = "control-label" for="name">Username:</label>
            <div>
                <input type="text" name="username" placeholder="Username" id="username" value="">
                <?php echo form_error('username', '<span class="alert-error help-inline">', '</span>')?>
            </div>
        </div>

        <div class="control-group">
            <label class = "control-label" for="name">Password: </label>
            <div>
                <input type="password" name="password" placeholder="Password" id="password" value="">
                <?php echo form_error('password', '<span class="alert-error help-inline">', '</span>')?>
            </div>
        </div>

        <div class="control-group">
            <label class = "control-label" for="name">Retype password: </label>
            <div>
                <input type="password" name="password1" placeholder="Retype password" id="password1" value="">
                <?php echo form_error('password1', '<span class="alert-error help-inline">', '</span>')?>
            </div>
        </div>

        <div class="control-group">
            <label class = "control-label" for="name">Email address: </label>
            <div>
                <input type="email" name="email" placeholder="Email" id="email" value="">
                <?php echo form_error('email', '<span class="alert-error help-inline">', '</span>')?>
            </div>
        </div>

        <div class="control-group">
            <label class = "control-label" for="name">Phone number: </label>
            <div>
                <input type="tel" name="tel" placeholder="Phone number" id="phone_number" value="">
                <?php echo form_error('tel', '<span class="alert-error help-inline">', '</span>')?>
            </div>
        </div>

        <div class="control-group">
            <label class = "control-label" for="name">Description: </label>
            <div>
                <input type="text" placeholder="Description" name="descr" id="description" value="">
                <?php echo form_error('descr', '<span class="alert-error help-inline">', '</span>')?>
            </div>
        </div>


</div>
<div class="modal-footer">
<div class="container-fluid">
    <div class="row-fluid">
        <div class="span5" id="message_add_user_id">
        </div>

        <div class="span7">
            <button id="add_user_submit" type="button" class="btn btn-success">Update <i class="icon-white icon-check"></i> </button>
            <button class="btn" onClick="document.location.reload(true)" data-dismiss="modal" >Close</button>
        </div>          
    </div>
</div>
<?php echo form_close();?>

這是模態將出現的功能:

    function edituserdata(username){
      $('#myModal1').modal('show');
    }

當我單擊一行時,我希望該用戶的數據包含在引導程序模式的文本字段中。 我怎樣才能做到這一點?

您可能想要通過ajax進行所有操作,如下所示:

表格html頁面

<table class="table table-striped table-bordered" id="example">
    <thead>
    <?php
        foreach ($fields as $field_name => $field_display){
            echo "<th>$field_display</th>";
        }
    ?>
    </thead>
    <tbody>
    <?php
        foreach ($users as $row){
            echo "<tr>";
            foreach($fields as $field_name => $field_display){
                if($field_name == 'username'){
                    echo "<td><a href='#' onclick=\"edituserdata('".$row->username."')\">".$row->$field_name."</a></td>";
                } else {
                    echo "<td>".$row->$field_name."</td>";
                }
            }
            echo "</tr>";
        }
    ?>
    </tbody>
</table>   

<!-- Modal -->
<div id="myModal1" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Update you're data</h3>
    </div>
    <div class="modal-body">

        <!-- NOTE THE EMPTY MODAL -->

    </div>
    <div class="modal-footer">
        <div class="container-fluid">
            <div class="row-fluid">
                <div class="span5" id="message_add_user_id">
                </div>

                <div class="span7">
                    <button id="add_user_submit" type="button" class="btn btn-success">Update <i class="icon-white icon-check"></i> </button>
                    <button class="btn" onClick="document.location.reload(true)" data-dismiss="modal" >Close</button>
                </div>          
            </div>
        </div>
    </div>
</div>

Ajax頁面

<?php echo form_open('main/update'); ?>

<?php 
    // TODO get username from $_GET['username'] and then retrieve all user information from db
    // TODO echo user data into form fields

    $name = array(
            'name' => 'name',
            'id' => 'name',
            'value' => set_value('name')
    );
    ?>

    <div class="control-group">
        <label class = "control-label" for="name">Name:</label>
        <div>
            <input type="text" name="name" placeholder="Name" id="name" value="">
            <?php echo form_error('name', '<span class="alert-error help-inline">', '</span>')?>
        </div>
    </div>

    <div class="control-group">
        <label class = "control-label" for="name">Username:</label>
        <div>
            <input type="text" name="username" placeholder="Username" id="username" value="">
            <?php echo form_error('username', '<span class="alert-error help-inline">', '</span>')?>
        </div>
    </div>

    <div class="control-group">
        <label class = "control-label" for="name">Password: </label>
        <div>
            <input type="password" name="password" placeholder="Password" id="password" value="">
            <?php echo form_error('password', '<span class="alert-error help-inline">', '</span>')?>
        </div>
    </div>

    <div class="control-group">
        <label class = "control-label" for="name">Retype password: </label>
        <div>
            <input type="password" name="password1" placeholder="Retype password" id="password1" value="">
            <?php echo form_error('password1', '<span class="alert-error help-inline">', '</span>')?>
        </div>
    </div>

    <div class="control-group">
        <label class = "control-label" for="name">Email address: </label>
        <div>
            <input type="email" name="email" placeholder="Email" id="email" value="">
            <?php echo form_error('email', '<span class="alert-error help-inline">', '</span>')?>
        </div>
    </div>

    <div class="control-group">
        <label class = "control-label" for="name">Phone number: </label>
        <div>
            <input type="tel" name="tel" placeholder="Phone number" id="phone_number" value="">
            <?php echo form_error('tel', '<span class="alert-error help-inline">', '</span>')?>
        </div>
    </div>

    <div class="control-group">
        <label class = "control-label" for="name">Description: </label>
        <div>
            <input type="text" placeholder="Description" name="descr" id="description" value="">
            <?php echo form_error('descr', '<span class="alert-error help-inline">', '</span>')?>
        </div>
    </div>

<?php echo form_close();?>

表格頁面上的Javascript

<script>
    function edituserdata(username) {
        $.get('url-to-ajax-page', {username: username}, function(resp) {
            $('#myModal1 .modal-body').html(resp);
            $('#myModal1').modal('show');
        });
    }
</script>

我們在這里所做的是有一個包含用戶名的表。 當您單擊用戶名時,它將對頁面進行ajax調用,該頁面將用用戶數據填充表單數據並在模式中顯示該數據。

因此,這應該可以幫助您入門。 在提交表單時,我沒有時間寫其余的內容,但是您要做的是當他們單擊“提交”按鈕時又有一個ajax查詢,這會將數據發布到可能相同的ajax url。 如果發布的數據中有錯誤,則返回數據時,請使用ajax響應替換模型主體的內容,否則請關閉窗口。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM