Change Text Format in a PHP form before sending

Hi Everyone
I have the following which is a form that will add data to my Database when sent.

if(substr(basename($_SERVER['PHP_SELF']), 0, 11) == "imEmailForm") {
	include '../res/x5engine.php';
	$form = new ImForm();
	$form->setField('Name', @$_GET['Name'], 'Name', false);
	$form->setField('Job:', @$_GET['Sire'], 'Job', false);
	$form->setField('', @$_GET['imObjectForm_6_5'], '', true);
	$form->setField('Sex', @$_GET['Sex'], 'Sex', false);
	$form->setField('Date Of Birth ', @$_GET['DOB'], 'DOB', false);

	if(@$_GET['action'] != 'check_answer') {
		if(!isset($_GET['imJsCheck']) || $_GET['imJsCheck'] != '7E2ACB592B4423AFFD09BDD9C' || (isset($_GET['imSpProt']) && $_GET['imSpProt'] != ""))
			die(imPrintJsError());
		$db = getDbData('61ekzeq4');
		if (!$db)
			die("Cannot find db");
		$db = ImDb::from_db_data($db);		if (!$form->saveToDb($db, 'people))
			die("Unable to connect to db");
		@header('Location: ../page-123.php');
		exit();
	} else {
		echo $form->checkAnswer(@$_GET['id'], @$_GET['answer']) ? 1 : 0;
	}
}

This works fine but i am trying to add something
What i need is something that will change the format of the text that is entered before sending.
So if text entered is “BOB THE SPONGE” will become “Bob The Sponge”
So when it gets to the Database is not "BOB THE SPONGE but “Bob The Sponge”
Can anyone tell me what needs to be added to the code to do this, Thanks
P.S just found that “ucwords” would probably work just don’t know how to build it into the form.

1.) you have a syntax error (missing end single quote)

'people
vs
'people'

2.) What value(s) are you trying to convert using ucwords?

Not familiar with ‘setField()’ or what that does or what class you are using? (or why not using $_POST values?)

$covertedName = ucwords(`$_GET['Name'])

Then you would use reference to $convertedName instead of the direct $_GET[] value…

@whispers, thanks for the reply, i saw the missing ’ forgot to edit in the post.
I’ve got no idea what setField is either it’s just the program i use that puts that in all the form files.
I am trying to convert the value of ‘Name’ entered into the form by adding ucwords somewhere.
so basically you are saying change this

$form->setField('Name', @$_GET['Name'], 'Name', false);

to this
$form->setField('Name', $covertedName = ucwords(`$_GET['Name'], false); ??

Perhaps this:

$covertedName = ucwords($_GET['Name']);
$form->setField('Name', $covertedName, 'Name', false);

or this:

$form->setField('Name', ucwords($_GET['Name'], 'Name', false);

This code should not be using any @ error suppressors. Because it does, it indicates the code is lacking in validation logic, meaning it is probably resulting in a bad user experience, i.e. not telling the user what they did/didn’t do that’s causing problems.

Ignoring that, you should actually apply strtolower() first, then ucwords(), to achieve the stated result.

Also, because the code is using the @ error suppressor on the $_GET['name'] variable, this code will produce follow-on php errors when you call the function(s) using it as their input. The code should use the Null Coalescing Operator ?? so that a default value gets used, e.g. an empty string, when the variable doesn’t exist.

$form->setField('Name', ucwords(strtolower($_GET['Name'] ?? '')), 'Name', false);

@phdr Thanks i will try that.

Sponsor our Newsletter | Privacy Policy | Terms of Service