简体   繁体   English

如何将字符串附加到表单字段中的数据

[英]How can I append a string to the data from a form field

I have a form and I need a way of ensuring that only people with a particular email address can successfully signup. 我有一个表格,并且我需要一种方法来确保只有具有特定电子邮件地址的人才能成功注册。 For example the only users allowed are from foo.com, so every email address should be like myemail@foo.com. 例如,唯一允许的用户来自foo.com,因此每个电子邮件地址都应类似于myemail@foo.com。 Now I want the user to only have to enter the email user name and after the form is submitted I add the @foo.com using php. 现在,我希望用户仅需输入电子邮件用户名,并在提交表单后,使用php添加@ foo.com。 How would I go about implementing such a feature and if there is a better way to achieve the desired result where can I find such information . 我将如何实现这样的功能,以及是否有更好的方法来实现所需的结果,在哪里可以找到此类信息。 Thanks. 谢谢。

Easy, name the form field username for example. 轻松,例如,命名表单字段用户名。 Name your submit button submit for example 例如,将您的“提交”按钮命名为“提交”

then on form submit 然后在表单上提交

if(isset($_POST['submit'])){
$username = $_POST['username'];
$username .= "@foo.com"   // jacksperro@foo.com

// rest of your code
}

On a note, I'd put a label after the input box saying '@foo.com' to let them know to only type their username 在笔记上,我会在输入框后贴上一个标签,说“ @ foo.com”,让他们知道只输入他们的用户名

This is basic string concatenation. 这是基本的字符串连接。

$suffix = '@foo.com';
$username = 'test';
$email = $username . $suffix;

Try this, it should do what you need and more: 尝试此操作,它应该可以满足您的需求以及更多:

<?php 
if(isset($_POST['submit'])){

$user       = trim(htmlspecialchars($_POST['username'])); // a bit of security
$final_user = sprintf("%s@foo.com", $user);   // user@foo.com

echo $final_user;

}

?>

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

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