簡體   English   中英

如何根據其他表單字段自動從數據庫中為輸入字段設置值。 使用PHP?

[英]How to set the input field with the value automatically from the database on the basis of other form field. using php?

一旦他們以相同的形式輸入電子郵件,我將嘗試從數據庫中自動填寫諸如姓名和地址之類的表單字段。 我能夠從該數據庫中獲取該特定電子郵件的值,但是無法成功將該值放入“名稱”和“地址”字段中。 以下是代碼示例:

Index.php

<div class="form-group">
   <label>Email*</label>
   <input type="text" name="email" required/>
</div>

<div class="form-group">
    <label>Name</label>
    <?php echo $model->Form->textBoxFor('name',['required'=>'required']); ?>
</div>
<div class="form-group">
    <label>Address</label>
    <?php echo $model->Form->textBoxFor('address',['required'=>'required']); ?>
</div>

在同一文件中,我的jQuery部分如下:

$("input[name=email]" ).on( "focusout", function(){
    // Getting the email value
    var curr_email = $(this).val();
    // Making the request & passing the current email to the request.
    $.post( "/subscribe/getDetails", { email: curr_email}, function( data ) {
        // Updating the fields with their values.
        $("input[name='name']").val( data['name'] );
        $("input[name='address']").val( data['address'] );
    });
});

“getDetails”功能是subscribeController.php。 功能如下:

public function getDetails(){
    $email = $_POST['email'];
    $result= [];
    $details = \Model\User::getList(['where'=>"email = '{$email}'"]);
    if($details){
        $result = ['name' => $details[0]->name, 'address' => $details[0]->address];
    } 
}

我成功地在“數據”中獲取了詳細信息,但無法在其各自的輸入字段中設置該值。 我知道我在jQuery部分中錯誤地寫了一些東西,在那里我將值設置為該字段。 感謝幫助。 提前致謝。

從外觀上看,您將在控制器中獲得結果,但不會將結果發送出去。

嘗試這個:

<?php
public function getDetails(){
    $email = $_POST['email'];
    $result= [];
    $details = \Model\User::getList(['where'=>"email = '{$email}'"]);
    if($details){
        $result = ['name' => $details[0]->name, 'address' => $details[0]->address];
        echo json_encode($result);  // this is the part you're missing
    }
}

暫無
暫無

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

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