繁体   English   中英

如何在PHP中将ucwords()添加到HTML表单值?

[英]How To Add ucwords() in PHP To HTML Form Value?

我的网站上有一个基本的联系表单,我正在尝试将PHP的PHP ucwords()函数添加到用户的first_name和last_name字段的表单中,以便它们正确将首字母大写。 如何将其添加到实际的HTML表单中?

编辑:我希望仅在用户提交表单后才应用这些更改。 我不太在乎用户输入的内容。我只需要一个人来实际演示一个例子。

就像我如何将PHP ucwords()代码添加到这种简单形式中?

<!DOCTYPE html>
<html>
<body>

<form action="www.mysite.com" method="post">
First name: <input type="text" name="first_name" value="" /><br />
Last name: <input type="text" name="last_name" value="" /><br />
<input type="submit" value="Submit" />
</form>

</body>
</html>

我假设我做类似value='<php echo ucwords() ?>'事情,但是我不知道怎么做?

谢谢!

当用户提交表单时,您可以通过PHP的$ _POST变量[因为method =“ post”]访问提交的信息,并且实际上,您必须指定实际页面,在该页面中您需要进一步处理提交的信息

<?php
// for example action="signup_process.php" and method="post" 
// and input fields submitted are "first_name", "last_name" 
// then u can access information like this on page "signup_process.php"

// ucwords() is used to capitalize the first letter 
// of each submit input field information

$first_name = ucwords($_POST["first_name"]);
$last_name = ucwords($_POST["last_name"]);
?>

PHP教程

假设启用了短标签:

$firstName = 'Text to go into the form';
<input type="text" name="first_name" value="<?=ucwords($firstName)?>" />

否则如你所说

<input type="text" name="first_name" value="<?php echo ucwords($firstName); ?>" />

假设您要在不刷新页面的情况下进行操作,则需要使用Javascript。 最简单的方法是在输入字段中添加onkeyup事件,并模拟PHP的ucwords函数,其外观类似于...

function ucwords(str) {
    return (str + '').replace(/^([a-z])|\s+([a-z])/g, function ($1) {
        return $1.toUpperCase();
    });
}

编辑:响应您的编辑,如果您想获得应用了ucwords发送的值,您所要做的就是$newVal = ucwords($_POST['fieldName']);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM