Solved

Add Data to input

Posted on 2014-09-05
17
167 Views
Last Modified: 2014-09-06
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

0
Comment
Question by:rgranlund
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +2
17 Comments
 
LVL 33

Expert Comment

by:Big Monty
ID: 40306740
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; ?>";
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40306843
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.
0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 40306845
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; ?> />
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 40306847
@Dave Great minds think alike :)
0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 40306852
<input type="text" name="FirstName" id="FirstName"  />
<input type="hidden" name="AddlData" value="<?php echo $additional_Data; ?>" />
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 40306855
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
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40306862
@yodercm, of course they do!
0
 
LVL 7

Author Comment

by:rgranlund
ID: 40306893
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?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40306924
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.
0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 40306946
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.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40306960
http://iconoun.com/demo/temp_rgranlund.php

<?php // demo/temp_rgranlund.php
error_reporting(E_ALL);

// DUMP THE REQUEST VARIABLES
if (!empty($_POST))
{
    // ACTIVATE THIS TO SHOW THE ENTIRE REQUEST
    // var_dump($_POST);

    // GET THE SELECTED NAME
    $nom = $_POST['names'];

    // USING THE SELECTED NAME VALUE, GET THE ADDITIONAL INFORMATION
    $add = $_POST[$nom];

    // SHOW THE WORK PRPODUCT
    echo PHP_EOL . "$nom $add";
}

// CREATE THE HTML DOCUMENT
$htm = <<<EOD
<form method="post">
<select name="names">
<option value="Tony">Tony</option>
<option value="Brom">Brom</option>
<option value="Niel">Niel</option>
</select>
<input type="hidden" name="Tony" value="(additional Info that goes with Tony)" />
<input type="hidden" name="Brom" value="(additional Info that goes with Brom)" />
<input type="hidden" name="Niel" value="(additional Info that goes with Niel)" />
<input type="submit" />
</form>
EOD;

echo $htm;

Open in new window

HTH, ~Ray
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 40306977
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">
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 40306981
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
0
 
LVL 7

Author Closing Comment

by:rgranlund
ID: 40307871
Pretty Cool.  Thanks Ray
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40307953
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.
0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 40308044
I find it a bit strange that you give all the points to someone who copy/pasted my previous solution, and none to me.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40308059
@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!
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question