Jump to content

posting data from html form to html form via PHP


RLJ

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.