使用javascript将单个文本框中的文本分成2个PHP变量


separating text from a single textbox into 2 php variables using javascript

我已经为这件事绞尽脑汁几个小时了。基本上,我有一个显示来自数据库的用户信息的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();

                }               
        });

这样做会对数据库产生问题,因为我希望将姓和名放入单独的列中,但是在将其转换为文本框之后,内容被放入单个变量中。现在是问题。

我如何通过文本框解析并为名字和姓氏分配2个单独的变量?

这假设您对数据库的最终解析是使用php完成的。

下面是我用来从文本输入中获取名字和姓氏的两个函数:
//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"