Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript将单个文本框中的文本分离为2个php变量_Php_Javascript_Jquery_Mysql - Fatal编程技术网

使用javascript将单个文本框中的文本分离为2个php变量

使用javascript将单个文本框中的文本分离为2个php变量,php,javascript,jquery,mysql,Php,Javascript,Jquery,Mysql,我绞尽脑汁想了几个小时。基本上,我有一个div,用于显示数据库中的用户信息: if (isset($_SESSION['email']) && ($_SESSION['userID'])) { $sql = "SELECT * FROM user_accounts WHERE email='" . $_SESSION['email'] . "' AND user_id=" . $_SESSION['userID'] ; $userRecordSet = $_dbConn->

我绞尽脑汁想了几个小时。基本上,我有一个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();


                }               
        });
//单击nameChange DIV时执行
$(“#名称更改”)。单击(函数(){
//检查是否存在任何现有的输入元素
if($(this).children('input')。长度==0){
$(“保存”).css(“显示”、“内联”);
//包含要替换的输入HTML的变量
var输入框=”;
//在div中插入HTML intp
$(this).html(inputbox);
//自动为输入框提供焦点
$(“this.inputbox”).focus();
}               
});
这样做会给数据库带来问题,因为我希望名字和姓氏进入单独的列中,但在将其转换为文本框后,内容将被放入单个变量中。现在回答问题


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

这假设您对数据库的最终解析是使用
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”

你的意思是在单击“保存”时解析它?如果名字或姓氏有空格,你会如何处理这种情况?好的,massimo,但是如果我的名字由两个词或姓氏组成呢?是的,你们是对的。我想得越多,我就越觉得最好只显示单独的输入元素。!!这将是唯一的解决办法。我想。或者,您可以告诉用户遵循特定的格式,如姓氏、名字,然后您可以执行解析解决方案。如果姓氏是由两个单词组成的,比如Dela Cerna或其他什么,那么你只能得到最后一个单词。@dunli Yep,他必须做出最适合他的假设。可以是2、3、4或5部分名称。这对我来说效果很好,可以很容易地适应做更多我认为。但你说得有道理。