gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:06:09 25 4
gpt4 key购买 nike

我已经为此绞尽脑汁几个小时了。基本上我有一个 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”

关于php - 使用 javascript 将文本从单个文本框分离为 2 个 php 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14311706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com