Jump to content

Retaining user data in forms after page reloads - Newbie confused


Recommended Posts

Hi, I have been trying to get the below code to work but without luck for the past couple of days.

 

Can anyone help point out where i am going wrong.

 

I have a webpage that has several buttons which will eventually let users search for data stored in different tables of a postgresql database ( i have simplified the code here to just two buttons).

 

When the user clicks the Search for hostname button a form is displayed letting them type in their search criteria.

 

Upon clicking the submit button i am able to access the users search strings with no problem but i cannot get the original search form to display above the search results additonaly including the users original search strings.

 

Any help on this would be much appreciated as i am pulling my hair out, as you can probably guess i am very new to PHP/HTML :)

 

My code is

 

<? 
session_start(); 
?>

<form method="POST" action="<? echo $_SERVER['PHP_SELF'];?>">
<center>
<button type="submit" id="green_button" name="searchhostname">Search for Hostname</button>
<button type="submit" id="green_button" name="searchipaddress">Search for IP address</button>
</center>
</form>
</body>




<?			
// If user clicks the search hostname button display the additonal search form
if ($_POST['searchhostname'])
{
	echo "Search for Hostnames and IP Addresses:<br><br>";
	echo "<form method='POST' action='".$_SERVER['PHP_SELF']."'>";
	echo "Hostname: <input type='text' name='hostname' size='16' value='".$_POST['hostname']."'/>";
	echo "<br>"; 	 
	echo "Operating System: <input type='text' name='ostype' size='16' value='".$_POST['ostype']."'/>";
	echo "<br>";
	echo "<input type='submit' id='green_button' value='Search' name='runsearch'><br><br>";
	echo"</form>";


}

// If user has entered criteria to search on run the search but alos display the original search form above
// and any data the user has entered before displaying results

if ($_POST['runsearch'])
{
//In reality this is where the users search data will be compared to a postresql database....

echo "You want to search for hostname: ".$_POST['hostname']."<br>";
echo "You want to search for operating system: ".$_POST['ostype']."<br>";
}


?>

 

 

<?php?>

 

Full tags to declare a php document please, this will help you out in the longrun..

 

$_SERVER['PHP_SELF']; Try to avoid this, there are know security issues surrounding this superglobal

 

Rw

Thanks for the replies and advice.

 

However, what is the best way to ensure that when the page gets refreshed due to the user clicking the 'Search'  button, the original search form is also displayed first (with the fields still retaining the users typed in search criteria) , this is the part i am struggling with?

 

With my current code the users data is outputted fine but the original search form is not displayed as the logic to display it is such that the 'searchhostname' button is pressed and hence gets left out after the user clicks the 'search' button and the page refreshes?

 

 

I think i may have solved it!

 

I inculded an OR statement in the if clause that displays the search form, would this be the way to go?

 

<?php 
session_start(); 
?>

<form method="POST" action="<? echo $_SERVER['PHP_SELF'];?>">
<center>
<button type="submit" id="green_button" name="searchhostname">Search for Hostname</button>
<button type="submit" id="green_button" name="searchipaddress">Search for IP address</button>
</center>
</form>
</body>




<?php			
// If user clicks the search hostname button display the additonal search form
if (isset ($_POST['searchhostname']) || isset($_POST['runsearch']))
{
	echo "Search for Hostnames and IP Addresses:<br><br>";
	echo "<form method='POST' action='".$_SERVER['PHP_SELF']."'>";
	echo "Hostname: <input type='text' name='hostname' size='16' value='".$_POST['hostname']."'/>";
	echo "<br>"; 	 
	echo "Operating System: <input type='text' name='ostype' size='16' value='".$_POST['ostype']."'/>";
	echo "<br>";
	echo "<input type='submit' id='green_button' value='Search' name='runsearch'><br><br>";
	echo"</form>";


}

// If user has entered criteria to search on run the search but also display the original search form above
// and any data the user has entered before displaying results

if (isset ($_POST['runsearch']))
{
//In reality this is where the users search data will be compared to a postresql database....

echo "You want to search for hostname: ".$_POST['hostname']."<br>";
echo "You want to search for operating system: ".$_POST['ostype']."<br>";
}


?>

You can use the hidden field to act a a validation that the form you are trying to process has the values in it that you are expecting, I use that same method in my upload script to determine whether or not it is a single file upload or a zip file.

 

And yes, the || clause will do exactly as you ask, but I would go the extra mile and put this:-

if ((isset ($_POST['searchhostname']) && !empty($_POST['searchhostname'])) || (isset($_POST['runsearch']) && !empty($_POST['runsearch']))){

 

Just that little extra security.

 

Rw

Having now cracked it for retaining user data in the text feilds afetr a page releoad the next brick wall i have hit is retaing the users choice from a drop down menu after reload.

 

Given the below code, how can it be changed so that when the user click search their selection from the drop down menu is retained when the results are displayed?

 


<?php 
session_start(); 
?>

<form method="POST" action="<? echo $_SERVER['PHP_SELF'];?>">
<center>
<button type="submit" id="green_button" name="searchhostname">Search for Hostname</button>
<button type="submit" id="green_button" name="searchipaddress">Search for IP address</button>
</center>
</form>
</body>




<?php			
// If user clicks the search hostname button display the additonal search form
if (isset ($_POST['searchhostname']) || isset($_POST['runsearch']))
{
	echo "Search for Hostnames and IP Addresses:<br><br>";
	echo "<form method='POST' action='".$_SERVER['PHP_SELF']."'>";
                echo "<input type='hidden' name='searchhostname' value='test'>"; // Added to ????????
	echo "Hostname: <input type='text' name='hostname' size='16' value='".$_POST['hostname']."'/>";
	echo "<br>"; 	 
	echo "Operating System: <select name='ostype'><option value='all'>all types</option><option value='windows'>windows</option><option value='linux'>linux</option></select>"; 
	echo "<br>";
	echo "<input type='submit' id='green_button' value='Search' name='runsearch'><br><br>";
	echo"</form>";


}

// If user has entered criteria to search on run the search but also display the original search form above
// and any data the user has entered before displaying results

if (isset ($_POST['runsearch']))
{
//In reality this is where the users search data will be compared to a postresql database....

echo "You want to search for hostname: ".$_POST['hostname']."<br>";
echo "You want to search for operating system: ".$_POST['ostype']."<br>";
}


?>

Change to this:-

echo "Hostname: <input type='text' name='hostname' size='16' value='".(isset($_POST['hostname']) ? $_POST['hostname'] : '')."'/>";

 

And drop downs employ the same sort of logic, remember the value="" attribute in the select tag... Go forth and have fun!

 

Rw

That didnt seem to work out :(

 

The problem is that although i can track the users selection with no problem, when the page  is reloaded the selection inside the drop down box always returns to be All Types despite the user haveing selected a different option. It doesnt seem to be as clear cut as when pointing the value of a text box to the $_POST['xxxxx'] variable.....

I think i have managed to find a way to do this, if there is however an easier way i would like to know as the way i have come up with seems like a lot of work to track user selection in drop down menus after a page refresh.

 

My solution was to create a seperate function that handles the creation of the drop down menu.

 

main page code:

 

<?php 
session_start(); 

include 'test_handleDropDowns.php';

       
?>

<form method="POST" action="<? echo $_SERVER['PHP_SELF'];?>">
<center>
<button type="submit" id="green_button" name="searchhostname">Search for Hostname</button>
<button type="submit" id="green_button" name="searchipaddress">Search for IP address</button>
</center>
</form>
</body>




<?php			
// If user clicks the search hostname button display the additonal search form
if (isset ($_POST['searchhostname']) || isset($_POST['runsearch']))
{
	echo "Search for Hostnames and IP Addresses:<br><br>";
	echo "<form method='POST' action='".$_SERVER['PHP_SELF']."'>";
	echo "Hostname: <input type='text' name='hostname' size='16' value='".$_POST['hostname']."'/>";
	echo "<br>";


	$menuname="ostype"; // set dropdown menu name
	$userselectedoption=$_POST['ostype']; // save any previous user selected option in this variable for passing to the test_handleDropDowns function

	// call function to create the ostype drop down menu
	test_handleDropDowns($menuname, $userselectedoption);

	echo "<br>";
	echo "<input type='submit' id='green_button' value='Search' name='runsearch'><br><br>";
	echo"</form>";


}

// If user has entered criteria to search on run the search but also display the original search form above
// and any data the user has entered before displaying results

if (isset ($_POST['runsearch']))
{
//In reality this is where the users search data will be compared to a postresql database....
       
        

echo "You want to search for hostname: ".$_POST['hostname']."<br>";
echo "You want to search for operating system: ".$_POST['ostype']."<br>";
}


?>

 

Function code:

 


<?php
function test_handleDropDowns($menuname, $userselectedoption)
{
    
  

   // if $menuname is passed as 'ostype':


		if ($menuname == "ostype")
		{
			//start echoing the html code to create the dropdown menu
			echo "Operating System: <select name='ostype'>";


			// Create drop down boxes.

			if ($userselectedoption == 'all' || $userselectedoption == null)
			{
				echo "<option value='all' selected=selected>All Types</option>";
			}
			else
			{
				echo "<option value='all'>All Types</option>";
			}


			if ($userselectedoption == 'windows')
			{
				echo "<option value='windows' selected=selected>windows</option>";
			}
			else
			{
				echo "<option value='windows'>windows</option>";
			}


			if ($userselectedoption == 'linux')
			{
				echo "<option value='linux' selected=selected>linux</option>";
			}
			else
			{
				echo "<option value='linux'>linux</option>";
			}



			// finsh off by echoing the html select tag
			echo "Operating System: </select>";


		}

}


?>

<?php
function test_handleDropDowns($menuname, $userselectedoption) {
    if ($menuname == "ostype") {
        //start echoing the html code to create the dropdown menu
echo "Operating System: <select name='ostype'>";
// Create drop down boxes.
        if ($userselectedoption == NULL) $userselectedoption = 'all';
        $options = array('all' => 'All Types', 'windows' => 'Windows', 'linux' =>'Linux');
        foreach ($options as $v => $option) echo "<option value='$v'", ($v == $userselectedoption) ? " selectede='selected'" : "",">$option</option>";
        echo "</select>";
        }
}
?>

Ga! Thankfully I have had some 'ale to make my previous mistake clear to me

And drop downs employ the same sort of logic, remember the value="" attribute in the select tag

I should have said:-

And drop downs employ the same sort of logic, remember the value="" attribute in the option tag

 

I have just checked some code I have on my site that handles my timezones, and I have the 'selection catcher' in the option tag

<select name="Aname" class="Aclass">
<option value="<?php echo ((isset($_POST['someValue']) && !empty($_POST['someValue'])) ? $_POST['someValue'] : '' );?>">
<?php echo ((isset($_POST['someValue']) && !empty($_POST['someValue'])) ? $_POST['someValue'] : '' );?>	
</option>
</select>

(Not sure why the highlighting says there is an error there at all, all the parenthesis are in the right places)

 

That does the trick, now off to finish my pint...

 

Rw

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.