Jump to content

ucwords in foreach($_POST as $key => $value)


cliffem

Recommended Posts

new account form has the usual; firstname, last name company, street address etc.

 

Talents write in ALL uppercase, lowercase everything but proper case.

Don't know where to apply ucwords and strtolower to convert values into proper case.

 

Code as is:

 

<form action="https://www.website.com/?page=step3" method="POST" name=myForm  id="commentForm"  class="cmxform">
 
    <?php foreach($_POST as $key => $value){ ?> <input name="<?php echo $key ?>" type="hidden" value="<?php echo $value ?>" />
    <?php } ?>
     
<hr>
<div class="linea">
        <div class="itemlabel">Nombre: <br>Name: </div>
<div class="cajita"><input type="text" class="required" name="nombre" /></div>
</div>
 
Thank you for any solutions/suggestions
Link to comment
Share on other sites

Many different ways; none work.

 

Guessing the way is to apply the operation BEFORE $key => $value; but can't figure it out.

 

This doesn't produce results; quite the opposite, page will not display.   value="<?php echo ucwords($value); ?>"

 

or after this code:   <div class="cajita"><input type="text" class="required" name="nombre" /></div>

 

Can also code so that data retrieval is "cased" the right way, but that is sloppy work. Need the user's input to be cased properly when added to db.

Link to comment
Share on other sites

You should learn how to create your output in an easier to read (and write) way.

 

 

foreach($_POST as $k=>$v)
{
   $ucv = ucase($v);
   echo "<input name=$k type='hidden' value='$v'>";
}

 

See how easy that is to read?  And how easy to type? 

Although the whole exercise seems odd.  You're grabbing a set of inputs and then creating html form output?  Uhhh - don't you want to USE the post values for something rather than write them back out?

 

Also - you have other errors in your posted code.  Turn on error checking to see.  (See my signature)

Link to comment
Share on other sites

Try

$data = array (
        'firstname' => 'JOHN',
        'lastname'  => 'SMIth',
        'address'   => '2 EFFIN ClOse'
    );
    
foreach ($data as $key => &$value) {
    $value = ucwords(strtolower($value));
}

View results

echo '<pre>',print_r($data, true),'</pre>';

Array
(
[firstname] => John
[lastname] => Smith
[address] => 2 Effin Close
)
Link to comment
Share on other sites

Be careful about putting too much logic to try an interpret correct case for people's names. There are plenty of names that have uppercase letters within the name (as opposed to just the first letter). E.g. "Beverly D'Angelo". And there are valid names that start with a lower case character. Any logic you use is bound to screw up someone's name.

 

Perhaps, you can test each name. if it is all caps or all lower case - then modify it. But, even that won't be perfect.

Link to comment
Share on other sites

You should learn how to create your output in an easier to read (and write) way.

foreach($_POST as $k=>$v)
{
   $ucv = ucase($v);
   echo "<input name=$k type='hidden' value='$v'>";
}

See how easy that is to read?  And how easy to type? 

Although the whole exercise seems odd.  You're grabbing a set of inputs and then creating html form output?   NO

 

Uhhh - don't you want to USE the post values for something rather than write them back out?  NO. Not writing back out; saving them to table.

 

Also - you have other errors in your posted code.  Turn on error checking to see.  (See my signature)

 

 

1- Was coded for me. I am trying to Proper case the data BEFORE it enters the table. 

 

2- There are over 30 inputs to the form. Names, address, phones (taken care of), URLS, etc.

Link to comment
Share on other sites

here is a decent function that may help you out

http://php.net/manual/en/function.ucwords.php#96179

 

Thank you. Aware of it years ago.  Use it elsewhere on site.

 

The problem is WHERE do I apply it in this code:  Need to ucwords() the values (i.e. "nombre") before they go into database. 

 

If I could   - foreach($_POST as $key => ucwords($value))  that would be the simplest solution.  Doesn't work.

 

I am sure it is a matter of my ignorance and just a matter of someone that has come across this issue before.

<form action="http://www.ainmpr.com/?page=step3" method="POST" name=myForm  id="commentForm"  class="cmxform">

<?php foreach($_POST as $key => $value){ ?> <input name="<?php echo $key ?>" type="hidden" value="<?php echo $value ?>" />  <?php } ?>

<div class="linea">
        <div class="itemlabel">Nombre: <br>Name: </div>
<div class="cajita">
<input type="text" class="required" name="nombre" />
</div>
</div> 

      <div class="linea">
        <div class="itemlabel">Segundo Nombre: <br>Middle Name: </div>
   <div class="cajita">
<input type="text" name="nombre2" />
</div>
</div>
Link to comment
Share on other sites

Be careful about putting too much logic to try an interpret correct case for people's names. There are plenty of names that have uppercase letters within the name (as opposed to just the first letter). E.g. "Beverly D'Angelo". And there are valid names that start with a lower case character. Any logic you use is bound to screw up someone's name.

 

Perhaps, you can test each name. if it is all caps or all lower case - then modify it. But, even that won't be perfect.

 

That, Psycho, is the issue:  Where do I modify the value?

 

I have no problem with occasional "D'Angelo". Can be corrected manually or tested as you say. If a character does not belong to an array of acceptable characters, then, apply different rules or flag for inspection.

 

My issue is that I get ALL CAPS, no caps, a mixed case of entries. Then when the data is used to create reports or even their Headshot Resume for movie directors it is GIGO.

Link to comment
Share on other sites

 

<?php

$data = array (
  'firstname' => 'JOHN',
  'lastname'  => 'SMIth',
  'address'   => '2 EFFIN ClOse'
);
 
function properCase($val) {
  // Convert to lower, then proper-case it.
  return ucwords(strtolower($val));
}
 
// Apply the above function to each value in the array.
$tada = array_map('properCase', $data);
// Print the array.
foreach($tada as $key => $value) {
  print "$key => $value\n";
}

 

Results in:

 

firstname => John
lastname => Smith
address => 2 Effin Close
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.