0

私はこれについて数時間頭を悩ませてきました。基本的に、データベースからユーザー情報を表示する div があります。

if (isset($_SESSION['email']) && ($_SESSION['userID'])) {

$sql = "SELECT * FROM user_accounts WHERE email='" . $_SESSION['email'] . "' AND user_id=" . $_SESSION['userID'] ;
$userRecordSet = $_dbConn->query($sql);

if ($userRecordSet != NULL) {
    $userRow = $userRecordSet->getrow();
    $firstName = $userRow['first_name'];
    $lastName = $userRow['last_name'];

    }

}

次に、姓名が div に配置され、ユーザーに表示されます。

<div id="nameChange" title="Click to edit"><?=$firstName?> <?=$lastName?></div>

ユーザーがこの DIV 要素をクリックすると、テキスト ボックスに変換され、DIV から取得したユーザーの姓名の内容が表示されます。上記をサポートするJqueryは次のとおりです。

//execute when nameChange DIV is clicked
        $("#nameChange").click(function(){  

                //check if there are any existing input elements
                if ($(this).children('input').length == 0){


                    $("#save").css("display", "inline");

                    //variable that contains input HTML to replace
                    var inputbox = "<input type='text' class='inputbox' name='userName' value=\""+$(this).text()+"\">"; 
                    //insert the HTML intp the div
                    $(this).html(inputbox);         

                    //automatically give focus to the input box     
                    $("this .inputbox").focus();


                }               
        });

これを行うと、データベースに問題が発生します。名前と姓を別々の列に入れたいのですが、テキスト ボックスに変換された後、内容が 1 つの変数に配置されるためです。今質問のために。

テキスト ボックスを解析して、姓名に 2 つの個別の変数を割り当てるにはどうすればよいですか?

4

1 に答える 1

1

これは、データベースへの最終的な解析が で行われることを前提としていますphp

テキスト入力から姓名を取得するために使用する 2 つの関数を次に示します。

//returns first name form full name input
public function FirstName($input)
    {   
        $name = explode(" ",$input);
        $howmany = count($name);
        if($howmany != '1')
            {
            if($howmany == '2')
                {   
                    $firstname = $name[0];
                    return $firstname;
                }
                    if($howmany == '3')
                {   
                    $firstname = $name[0]." ".$name[1];
                    return $firstname;
                }
            }

        return false;   
    }
//returns last name from full name input
public function LastName($input)
    {   
        $name = explode(" ",$input);
        $howmany = count($name);
        if($howmany != '1')
            {   
                $last = $howmany -1;
                $lastname = $name[$last];
                return $lastname;
            }
        return false;   
    }

これは、「mary lynn」のように名前が 2 つある人を考慮に入れています。

于 2013-01-14T02:48:03.440 に答える