RLJ Posted November 24, 2010 Share Posted November 24, 2010 Hi all, I have an html form that sends formdata to a php script called example.php with the POST method. example.php then has the following structure: <?php if(isset($_POST['var1'])) {$var1 = $_POST['var1'];} else {$var1 = 'English';} if(isset($_POST['var2'])) {$var2 = $_POST['var2'];} else {$var2 = 'French';} print" <html> <body> <form> <select id='select1'> <option>var1</option> <option>var2</option> <option>etc...</option> </select> <input type='text' id='input1'> ETC. ETC. </form> </body> </html> "; ?> So example.php has an embedded html form. How do I specify that some elements of the form should take their values from the variables retrieved at the start of the script via the POST command? For example, I want select1 to have 'var1' pre-selected and I want input1 to display var2. var1 & var2 get sent through to example.php fine, I just don't know how to get the embedded html form to use them. Please help! Thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/219701-posting-data-from-html-form-to-html-form-via-php/ Share on other sites More sharing options...
Jerred121 Posted November 24, 2010 Share Posted November 24, 2010 You question is really more of an HTML question from the sounds of it. To have a select element pre select something you just add selected="selected" to the open tag of the option you want selected... And to have $var1 and $var2 stored as the input of the two form elements you just put the variables in like so: <form> <select id='select1'> <option selected="selected">$var1</option> <option>$var2</option> <option>etc...</option> </select> <input type='text' id='input1' value='$var2'/> ETC. ETC. </form> you also needed a / added to the input element... Is this what you were trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/219701-posting-data-from-html-form-to-html-form-via-php/#findComment-1139009 Share on other sites More sharing options...
RLJ Posted November 24, 2010 Author Share Posted November 24, 2010 That's great, thanks! Just one more question though: I now have the html select menu inside example.php as follows: <select> <option selected='selected'>$var1</option> <option>English</option> <option>French</option> <option>German</option> <option>Dutch</option> <option>etc...</option> </select> But $var1 will be either English, or French, etc., depending on what was chosen in the html form that posted the data to example.php. This means that if $var1 = 'English' for example, 'English' will be listed twice in the select menu. Is there a way to avoid this? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/219701-posting-data-from-html-form-to-html-form-via-php/#findComment-1139068 Share on other sites More sharing options...
DavidAM Posted November 24, 2010 Share Posted November 24, 2010 Typically you would use an array (or database) and a loop to output the options. Then you test inside the loop to see if a each option is chosen: $langList = array('English', 'French', 'German', 'Dutch'); print('<select name="var1">'); foreach ($langList as $lang) { printf('<option %s>%s</option>', ($var1 == $lang ? 'selected="selected"' : ''), $lang); } echo '</select>'; Personally, I use printf() for this kind of work. It just seems to be easier to read. However it could be done as: echo '<option'; if ($var1 == $lang) echo ' selected="selected"'; echo '>' . $lang . '</option>'; Quote Link to comment https://forums.phpfreaks.com/topic/219701-posting-data-from-html-form-to-html-form-via-php/#findComment-1139089 Share on other sites More sharing options...
Jerred121 Posted November 24, 2010 Share Posted November 24, 2010 Yes, can $var1 only equal English or French? Or can it equal those other languages as well? There are many ways to skin this cat... the best and most complicated would be to use an array and a loop to make the selection and set all of the option values... since you seem a little noobish and arrays can be a little confusing when your just starting out, here is a quick and dirty (also array free method) way of doing this: <?php if(isset($_POST['var1'])) {$var1 = $_POST['var1'];} else {$var1 = 'English';} if(isset($_POST['var2'])) {$var2 = $_POST['var2'];} else {$var2 = 'French';} echo " <html> <body> <form> <select id='select1'> <option"; if ($var1=="English"){echo" selected='selected'";} echo ">English</option>"; echo "<option"; if ($var1=="French"){echo" selected='selected'";} echo ">French</option>"; echo "<option"; if ($var1=="Dutch"){echo" selected='selected'";} echo ">Dutch</option>"; echo "</select> <input type='text' id='input1' value='$var2' /> ETC. ETC. </form> </body> </html> "; ?> I would personally use david's method as it is much cleaner code and it would be easy to alter the potential options. Quote Link to comment https://forums.phpfreaks.com/topic/219701-posting-data-from-html-form-to-html-form-via-php/#findComment-1139096 Share on other sites More sharing options...
RLJ Posted November 25, 2010 Author Share Posted November 25, 2010 Thanks guys, I'll use the array because $var1 can be any of several languages. Quote Link to comment https://forums.phpfreaks.com/topic/219701-posting-data-from-html-form-to-html-form-via-php/#findComment-1139560 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.