How to use ucfirst in a PHP form [closed]

1



How to use ucfirst in the "name" of a PHP form?

I need to receive (yes or yes), the names and surnames with the first letter in upper case, and we know that today x those who complete their data in a web form from a mobile phone do not always "bother" to start their names and surnames in capital, and I want to to force it from PHP itself.

Where does ucfirst (only in "name") ...?

<?php

$nombre .= $_POST['nombre'];
$mail .= $_POST['email'];
$asunto .= $_POST['asunto'];
$mensaje .= $_POST['mensaje'];

$header .= 'From: ' . $mail . " \r\n";
$header .= "X-Mailer: PHP/" . phpversion() . " \r\n";
$header .= "Mime-Version: 1.0 \r\n";
$header .= "Content-Type: text/plain";

$cuerpo_mensaje .= "Enviado por: " . $nombre . " \r\n";
$cuerpo_mensaje .= "E-Mail: " . $mail . " \r\n\r\n";
$cuerpo_mensaje .= "Asunto: " . $asunto . " \r\n\r\n";
$cuerpo_mensaje .= "" . $mensaje . " \r\n\r\n";

?>

I will explain some exemplary code, because PHP is not my thing ...

:(

==================================================
==================

EDITO (solution)

Who can I serve, guiding me? by @Lixus, I did it like this:

$cuerpo_mensaje .= "Enviado por: " . ucwords($nombre) . " \r\n";

And that was it. Now even if visitors to the site write their names in lowercase (it happens in + 50% of cases, despite the measures taken in the development and programming, because many mobile keyboards and tablets ignore automatic capitalize), from now on with a simple ucwords in $ name I am already receiving the information as I needed.

And if you want to force more:

<input id="nombre" type="text" name="nombre" class="capitales" required>

And in the CSS:

.capitales {text-transform: capitalize}

And automatically everything happens from this == > Juan Perez to this == > J uan P erez

Greetings

;)

---------

    
asked by 20.04.2017 в 01:52
source

2 answers

3

Taking your original code may be like this:

<?php
    $nombre .= $_POST['nombre'];
    $mail .= $_POST['email'];
    $asunto .= $_POST['asunto'];
    $mensaje .= $_POST['mensaje'];

    $header .= 'From: ' . $mail . " \r\n";
    $header .= "X-Mailer: PHP/" . phpversion() . " \r\n";
    $header .= "Mime-Version: 1.0 \r\n";
    $header .= "Content-Type: text/plain";

    $cuerpo_mensaje .= "Enviado por: " . ucwords($nombre) . " \r\n";
    $cuerpo_mensaje .= "E-Mail: " . $mail . " \r\n\r\n";
    $cuerpo_mensaje .= "Asunto: " . $asunto . " \r\n\r\n";
    $cuerpo_mensaje .= "" . $mensaje . " \r\n\r\n";
?>

Now that if $nombre has both the name and the last name together, instead of taking out substrings, I recommend you divide it in $nombre $apellido so that you do a ucfirst() to both, to make sure that both name as last name have the first letter capitalized.

$nombre .= $_POST['nombre'];
$apellido .= $_POST['apellido'];

$cuerpo_mensaje .= "Enviado por: " . ucfirst($nombre) . " " . ucfirst($apellido) . "\r\n";
    
answered by 20.04.2017 в 01:58
1
<?php

$nombre .= $_POST['nombre'];
$mail .= $_POST['email'];
$asunto .= $_POST['asunto'];
$mensaje .= $_POST['mensaje'];

$header .= 'From: ' . $mail . " \r\n";
$header .= "X-Mailer: PHP/" . phpversion() . " \r\n";
$header .= "Mime-Version: 1.0 \r\n";
$header .= "Content-Type: text/plain";

$cuerpo_mensaje .= "Enviado por: " . ucwords(mb_strtolower($nombre,'UTF-8')) . " \r\n";
$cuerpo_mensaje .= "E-Mail: " . $mail . " \r\n\r\n";
$cuerpo_mensaje .= "Asunto: " . $asunto . " \r\n\r\n";
$cuerpo_mensaje .= "" . $mensaje . " \r\n\r\n";

?>

This is how it should work. Greetings.

    
answered by 20.04.2017 в 20:06