Jump to content

Recommended Posts

Hello everyone,

 

I created a simple userform to test.  What I wanted to do is have the user input a field,

and then click on the submitted button once the submitted button is click

the page refresh and still retain what the user input (it is not lost). 

 

I know that to do this, we have to use the isset function, which I did, but it didn't work.

 

Here are my codes, please provide some suggestion and code examples.

 

<html>
<head>
<title></title>
</head>

<body>

<form method ="post" action="">   

<table border = 1 > 

<tr> 
<td>


		<?php session_start();  $firstname = (isset($_POST['firstname'])); 
		$lastname = (isset($_POST[lastname]));?>
  First Name:      	<input name="firstname" type="text" value='<?php       $firstname  ?>'/></td>
</td>
</tr>


<tr> 
<td>
  Last Name:      	<input name="lastname" type="text" value="' . $lastname . '"/></td>
</td>
</tr>

<tr> 
<td>         
       <input name="submit" type="submit" value="Enter the information">

</tr> 
</td>

 

 

thanks,

Link to comment
https://forums.phpfreaks.com/topic/113833-retain-what-was-inputted/
Share on other sites

 

 

First Name:      	<input name="firstname" type="text" value='<?php echo $firstname;  ?>'></td>
</td>
</tr>


<tr> 
<td>
  Last Name:      	<input name="lastname" type="text" value='<?php echo $lastname; ?>'></td>
</td>
</tr>

Just missing the echo statements really. Also need the php tags in each area as I edited.

I have put the echo in and still no result.  didn't work.

That's because when you're using ISSET, you're storing the value 1 (as in, it is set), not the variable itself. You need remove the ISSET function.

 

The whole code would be:

<html>
<head>
<title></title>
</head>

<body>
<form method ="post" action="">   

<table border = 1 > 

<tr> 
<td>

  First Name:      	<input name="firstname" type="text" value="<?php echo $_POST['firstname'];  ?>" /></td>
</td>
</tr>


<tr> 
<td>
  Last Name:      	<input name="lastname" type="text" value="<?php echo $_POST['lastname']; ?>" /></td>
</td>
</tr>

<tr> 
<td><input name="submit" type="submit" value="Enter the information">

</tr> 
</td>

 

Isset() just returns 1(True) 0(false). So that's what you were storing in your field values.

 

If your goal with the ISSET function is to ensure the form was used, you actually want to check that the submit button "is set".

 

You would use

<?php

If (isset($_POST['submit'])) {

 

// the actions taken on the information from the form

 

}

?>

Ok, that works.

 

So how would you do this for a option field, where user selects one and

code retain the one selected.

 

</td>	 	<td><select name="gender">
			<option value=""  selected>- Select One -</option>
				<option value="Male" >Male</option>
				<option value="Female" >Female</option></select><br></td>

I created an array to loop user's selection, but when the user select an item and click on the button it retains

only the item (on the list) after.

 

does someone know why, I assume it that when you select one item, then when you click the submit button

the for loop runs again and select the next item on the list, retaining the wrong item.

 

<tr><!--I LIVE IN----------->
<td align="right">    <?php print"$Live";   
?>

       </td> 
			<td><select name="country">
                     

         <option value=""<?php if ($_POST['country'] == '') echo ' selected="selected"'; ?>>- Select One -</option>



<?php
$countries = array('Afghanistan','Aland Islands','Albania','Algeria','American Samoa','Andorra','Angola','Anguilla','Antarctica',
'Antigua and Barbuda','Argentina','Armenia','Aruba','Australia','Austria','Azerbaijan','Bahamas','Bahrain','Bangladesh','Barbados',
'Belarus','Belgium','Belize','Benin','Bermuda','Bhutan','Bolivia','Bosnia and Herzegovina','Botswana','Bouvet Island','Brazil',
'British Indian Ocean Territory','British Virgin Islands','Brunei','Bulgaria','Burkina Faso','Burundi','Cambodia','Cameroon','Canada',
'Cape Verde','Cayman Islands','Central African Republic','Chad','Chile','China','Christmas Island','Cocos (Keeling) Islands','Colombia',
'Comoros','Congo','Cook Islands','Costa Rica','Croatia','Cyprus','Czech Republic','Democratic Republic of Congo','Denmark','Disputed Territory',
'Djibouti','Dominica','Dominican Republic','East Timor','Ecuador','Egypt','El Salvador','Equatorial Guinea','Eritrea','Estonia','Ethiopia',
'Falkland Islands','Faroe Islands','Federated States of Micronesia','Fiji','Finland','France','French Guyana','French Polynesia',
'French Southern Territories','Gabon','Gambia','Georgia','Germany','Ghana','Gibraltar','Greece','Greenland','Grenada','Guadeloupe','Guam','Guatemala',
'Guinea','Guinea-Bissau','Guyana','Haiti','Heard Island and Mcdonald Islands','Honduras','Hong Kong','Hungary','Iceland','India','Indonesia','Iraq',
'Iraq-Saudi Arabia Neutral Zone','Ireland','Israel','Italy','Ivory Coast','Jamaica','Japan','Jordan','Kazakhstan','Kenya','Kiribati','Kuwait',
'Kyrgyzstan','Laos','Latvia','Lebanon','Lesotho','Liberia','Libya','Liechtenstein','Lithuania','Luxembourg','Macau','Macedonia','Madagascar',
'Malawi','Malaysia','Maldives','Mali','Malta','Marshall Islands','Martinique','Mauritania','Mauritius','Mayotte','Mexico','Moldova','Monaco','Mongolia',
'Montserrat','Morocco','Mozambique','Myanmar','Namibia','Nauru','Nepal','Netherlands','Netherlands Antilles','New Caledonia','New Zealand','Nicaragua',
'Niger','Nigeria','Niue','Norfolk Island','North Korea','Northern Mariana Islands','Norway','Oman','Pakistan','Palau','Palestinian Occupied Territories',
'Panama','Papua New Guinea','Paraguay','Peru','Philippines','Pitcairn Islands','Poland','Portugal','Puerto Rico','Qatar','Reunion','Romania','Russia',
'Rwanda','Saint Helena and Dependencies','Saint Kitts and Nevis','Saint Lucia','Saint Pierre and Miquelon','Saint Vincent and the Grenadines','Samoa',
'San Marino','Sao Tome and Principe','Saudi Arabia','Senegal','Serbia and Montenegro','Seychelles','Sierra Leone','Singapore','Slovakia','Slovenia',
'Solomon Islands','Somalia','South Africa','South Georgia and South Sandwich Islands','South Korea','Spain','Spratly Islands','Sri Lanka','Suriname',
'Svalbard and Jan Mayen','Swaziland','Sweden','Switzerland','Syria','Taiwan','Tajikistan','Tanzania','Thailand','Togo','Tokelau','Tonga','Trinidad and Tobago',
'Tunisia','Turkey','Turkmenistan','Turks And Caicos Islands','Tuvalu','Uganda','Ukraine','United Arab Emirates','United Kingdom','United Nations Neutral Zone',
'United States','United States Minor Outlying Islands','Uruguay','US Virgin Islands','Uzbekistan','Vanuatu','Vatican City','Venezuela','Vietnam',
'Wallis and Futuna','Western Sahara','Yemen','Zambia','Zimbabwe');


foreach ($countries as $value) {


   




$sel = ($_POST['country'] == $value) ? ' selected="selected' : '';



print'<option value="'.$value.'"'.$sel.'>'.  $value. '</option>\n';







}

?>  

Try replacing your foreach loop with this:

 

foreach ($countries as $value) {

if($sel == ""){
$sel = ($_POST['country'] == $value) ? ' selected="selected' : '';
}

}

print'<option value="'.$value.'"'.$sel.'>'.  $value. '</option>\n';

Try this... Sorry I probably made it worse for you before. :D

 

foreach ($countries as $value) {

$sel = ($_POST['country'] == $value) ? "'selected'='selected'" : "";
echo "<option value='{$value}' {$sel}>{$value}</option>";

}

 

I'm kind of sleepy. :P

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.