Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

Add Data to input

If you have an input field on a form, can you add additional data to the input field to pass along info without messing anything up?

For example:

<input type="text" name="FirstName" id="FirstName" data="<?php echo $additional_Data; ?>" />

Open in new window

Avatar of Big Monty
Big Monty
Flag of United States of America image

yes you can, but you need to use the VALUE attribute:

<input type="text" name="FirstName" id="FirstName" value="<?php echo $additional_Data; ?>" />

alternatively, if you want to use javascript, you can:

document.getElementById("FirstName").value = "<?php echo $additional_Data; ?>";
I disagree, @rgranlund is asking about his 'data' attribute.  Whatever is in the 'value' attribute will be passed along to PHP in the 'name/'value' format.  The 'data' you show above will not be sent to the PHP page as it is.  You would have to use javascript to create another 'name/value' pair to send it.  Or you could put the data in a 'hidden' field which would be sent with the form.
An easier way might be to send a second, hidden, form field value with the additional information, and then do the concatenation in the processing script.


<input type="text" name="FirstName" id="FirstName"  />
<input type="hidden" name="AddlData" value="<?php echo $additional_Data; ?> />
@Dave Great minds think alike :)
<input type="text" name="FirstName" id="FirstName"  />
<input type="hidden" name="AddlData" value="<?php echo $additional_Data; ?>" />
Hmmm I took the question as just sending data from a field, but you may be right. Hopefully the OP can clarify what they need
@yodercm, of course they do!
Avatar of Robert Granlund

ASKER

I need to pass along more info than just name, ID and value. I have one more piece that I would like to pass along.
For example:

<select name="names" id="names">
<option value="Tony" (additional Info that goes with Tony)>Tony</option>
<option value="Brom" (additional Info that goes with Brom)>Brom</option>
<option value="Niel" (additional Info that goes with Niel)>Niel</option>
</select>

Open in new window

Does that make it more clear?
No, that still Won't work.  All that a form sends is in the format of 'name=value' where name is the 'name of the element ("names" above) and the selected or entered value.  In a <select> element, the 'value' is not visible so any 'additional info' can be added to the 'value' without changing the display.  But if it is not in the 'value' attribute, it will not be sent.
Using select/option is a whole different problem than using input.

If you already know the names that will be in the select list (either hardcoded or pulled from database), then you can still make hidden inputs for each.

<select name="names" id="names">
<option value="Tony" (additional Info that goes with Tony)>Tony</option>
<option value="Brom" (additional Info that goes with Brom)>Brom</option>
<option value="Niel" (additional Info that goes with Niel)>Niel</option>
</select>
<input type="hidden" name="AddlInfoTony" value="(additional Info that goes with Tony)">
<input type="hidden" name="AddlInfoBrom" value="(additional Info that goes with Brom)">
<input type="hidden" name="AddlInfoNiel" value="(additional Info that goes with Niel)">



Then in your processing script,first get the value of names, then use it to construct the name of the hidden field to get the additional info for that name.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America 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
Another approach you could use if hidden fields isn't an option you want to use is to use some kind of delimeter in each options value attribute and then split the delimited string on the server side. For example say your option value is like:

<option value="someName@@someID@@someValue">
Surrey hit submit before I was done :)

Once you post that data, you should be able to split those values and store them in an array. I'm not that we'll versed in pop but it's a standard method in most scripting languages.

Other than that I would recommend using hidden fields as previously mentioned
Pretty Cool.  Thanks Ray
I also like Big Monty's solution.  Both solutions would require some sanity checks on the server, just to make sure you're getting predictable and acceptable input values.
I find it a bit strange that you give all the points to someone who copy/pasted my previous solution, and none to me.
@yodercm: I didn't copy/paste your "previous solution."  I wrote my own code (as I always do) and tested it prior to posting it here.  You can click the link I posted to see it in action.  Or you can just copy/paste my code snippet, install it on your own server and run it to see it in action.  Please don't be so touchy!