-1

So this is my code for the first_name. I have no clue why it does not work whenever I add an entry.

First Name: <input type="text" name="first_name" value="<?php function convertString($first_name){
$first_name=htmlentities(strip_tags($first_name));
$lowercaseFName = strtolower($first_name);
$ucFName = ucwords($lowercaseFName);
return $ucFName;} ?>"/> // I changed it with echo but nothing changed.

My code doesn't change the format of the text.I tried inputting in all caps and it will show as is. Am I missing something or doing it wrong?

The inputted text if all in capital must still be shown as Camel Case format in the table, like this:

First Name: MARIA YLONA (in the form)

First Name: Maria Ylona (in the table) - this is another .php file for viewing of the data entries

Pam
  • 73
  • 1
  • 9
  • Don't declare the function in the value. Declare it above, and then echo the returned value into the value. – aynber Feb 09 '16 at 18:54
  • Sorry, it should be *Data entry (on the title). – Pam Feb 09 '16 at 18:54
  • Do you execute `convertString`? The PHP is only available before the page loads (server side), maybe you mean to do this in JS (or on the form processing script)? – chris85 Feb 09 '16 at 18:55
  • I will try doing that, aynber. I hope it works this time. – Pam Feb 09 '16 at 18:55
  • you know this won't work for all names, right? `McDonald's -> mcdonald's -> Mcdonald's` – Marc B Feb 09 '16 at 19:02
  • 1
    what about MacDonald's? Oh, different "clan". – Funk Forty Niner Feb 09 '16 at 19:03
  • Welcome to stackoverflow. Please try and add more code to your question, as well as what you put in and what came out. In general, please go through the tour and the faq. This will greatly increase the chance someone will answer you question (correctly). – Roy Falk Feb 09 '16 at 19:05
  • http://php.net/manual/en/tutorial.php spend a day learning the basics first – Steve Feb 09 '16 at 19:08
  • Well thanks? I hope learning the basics first will immediately solve my particular problem here. I'd be glad if someone can help point out my mistake on the code. – Pam Feb 09 '16 at 19:17
  • *"Yes, it has a submit button that you need to click in order to insert the data into the table."* - You need to post your full code then as to where the form tags are, or if you're using JS/Ajax/jQuery and haven't told us. What I tested worked. – Funk Forty Niner Feb 09 '16 at 19:21
  • Remember PHP runs on the server side. So you have to process the data that's **returned** in the POST variables when the form is submitted. You can't do anything with PHP on the client side, when the user presses the submit button. – BryanT Feb 09 '16 at 19:43
  • Thank you for your help! It's working now. – Pam Feb 10 '16 at 06:08

2 Answers2

0

To clarify what aynber meant in the comments:

<?php
function convertString($first_name){
  $first_name=htmlentities(strip_tags($first_name));
  $lowercaseFName = strtolower($first_name);
  $ucFName = ucwords($lowercaseFName);
  return $ucFName;
}

// here initialize $first_name to something meaningful
?>
First Name: <input type="text" name="first_name" value="<?php echo(convertString($first_name)) ?>"/>
Sasha Pachev
  • 5,162
  • 3
  • 20
  • 20
  • I hope you understand this can only execute on the server after you click a button or something. It's not going to change the case real-time as you type. You'd have to use Javascript to do that. – Joseph James Feb 09 '16 at 19:04
  • Yes, it has a submit button that you need to click in order to insert the data into the table. Btw, I did this but it still doesn't convert into camel case. I think I got the wrong code for this? – Pam Feb 09 '16 at 19:09
  • @JosephJames what does JS / realtime have to do here? There's no mention of that in the question. – Funk Forty Niner Feb 09 '16 at 19:09
  • that's my point Pamela. The question leaves me wondering what will trigger this case conversion. For PHP it would have to be triggered by a button click, in which case you need to read the content of the form field, change the case, then echo it back into the form. – Joseph James Feb 10 '16 at 03:21
0

Seeing you didn't post your html form or how it's used, am submitting the following as a successful piece of code and using !empty() against a POST array with form tags and an isset() for the submit input.

Btw, functions and variables assignments should be used seperately than in inputs/form elements.

Create a function, then pass it inside the input with the parameter.

<?php
function convertString($first_name){
  $first_name=htmlentities(strip_tags($first_name));
  $lowercaseFName = strtolower($first_name);
  $ucFName = ucwords($lowercaseFName);
  return $ucFName;
}


if(isset($_POST['submit'])){

    if(!empty($_POST['first_name'])){

       $first_name = $_POST['first_name'];

    }

}

// here initialize $first_name to something meaningful
?>

<form method="post">
First Name: <input type="text" name="first_name" value="<?php echo(convertString($first_name)) ?>"/>

<input type="submit" name="submit" value="Submit">

</form>

Footnotes:

In regards to what Marc mentioned about McDonald's -> mcdonald's -> Mcdonald's and my MacDonald in comments...

Consult the following here on Stack which may prove to be useful:

You mention the use of a database, but haven't posted relative code and would be beyond the scope of the question.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141