Link to home
Start Free TrialLog in
Avatar of edavo
edavoFlag for United States of America

asked on

Combine form values to populate a field in MySQL

I want to combine several posted fields and make it the value for a single field. Below is what I have tested with no luck.

<tr>
    	<td style="padding:10px;border:1px solid"><label>* Gender</label></td>
        <td style="padding:10px;border:1px solid"><input type="text" name="gender" value="<?php echo @$user['gender']?>"></td>
   	</tr>
    <tr>
    	<td style="padding:10px;border:1px solid"><label>* Year of Birth</label></td>
        <td style="padding:10px;border:1px solid"><input type="text" name="dob" value="<?php echo @$user['dob']?>"></td>
   	</tr>
    <tr>
    	<td style="padding:10px;border:1px solid"><label>* Country</label></td>
        <td style="padding:10px;border:1px solid"><input type="text" name="country" value="<?php echo @$user['country']?>"></td>
   	</tr>
    <tr>
    	<td style="padding:10px;border:1px solid"><label>* Postal Code</label></td>
        <td style="padding:10px;border:1px solid"><input type="text" name="zip" value="<?php echo @$user['zip']?>"></td>
   	</tr>

<? $mpxid = @$user['dob'].@$user['country'].@$user['zip']; ?>

<input type="hidden" name="combine" value="<?php echo $mpxid; ?>">

Open in new window

Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

When you put the '@' in front of something like '@$user' in PHP, errors are not reported but they are Not prevented.  You should remove all of the '@' in your code and put 'error_reporting(E_ALL);' at the top of the page so you can see what errors are occurring so you can fix them.

And you are using '<?' in one place and '<?php' in another place for opening tags.  I recommend that you be consistent and use '<?php' in all places.
+1 for Dave's comment about error_reporting() and the short-open tag.
PHP also allows for short open tag <? which is discouraged since it is only available if enabled...
The safe way is to always use the explicit tag, and always avoid the close-php tag and other anti-practices, wherever possible.

See also: http://php.net/manual/en/ini.core.php#ini.short-open-tag

HEREDOC notation is useful for templates like this.

Before you spend too long on this, make sure you have the variable values you expect.  You can use PHP var_dump($user) to print out the $user array.

The timeline of the request and response may be in play here, too.  When does $user get populated with its data?
ASKER CERTIFIED SOLUTION
Avatar of William Nettmann
William Nettmann
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of edavo

ASKER

Used Javascript and it worked. Pulled values from forms, did a simple "combined = a+b+c."

xmpxid = a + b +  c + d;
document.getElementById('xmpxid').value = xmpxid;
then......
<input type="hidden" name="mpxid" id="xmpxid">

Thank You all - dM