Forum Moderators: coopster

Message Too Old, No Replies

A little contrib: Keep selected value in a form

"How to:" complete example

         

henry0

12:47 pm on Apr 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is a frequent topic so while I was updating a script I selected a sample and post it for PHP newcomer benefits

Next: Anyone among us to post an “How to” preserve selected values in a multi choices form environment

A simple “How to” preserve in a form the selected field

The form:
<div class="p1_bold"> <!--For example about marketing.. ->
</div>
</td></tr>
<td><select name="market_web">
<option value="no">No</option>
<option value="yes">Yes</option>
</select>

Destination file:
<?
/* #################
Here we are verifying that data from the form is "clean"
Do not trust any source when any data travels!
The switch allows verifying that the data is the expected data
################### */

$market_web= htmlentities($_POST['market_web']);
$clean_market_web = array();
switch ($_POST['market_web'])
{
case 'yes':
case 'no':
$clean_market_web['market_web'] = htmlentities($_POST['market_web']);
break;
}
$market_web=$clean_market_web['market_web'];
if ($market_web!=$clean_market_web['market_web'])
{
echo" <h1>Alert! we are aware of the tentative intrusion</h1><br>";
Exit();
}

// now we will feed the DB

// Last checking
if (isset ($market_web) &&!empty ($market_web))

{
$sql = "update aaaaaa
set username= '$username',
market_web= '$market_web'
where password='$password' and biz_name='$biz_name' ";
$result = mysql_query($sql, $conn);
}
if (isset ($result) &&!empty ($result))
{
// do something etc...
?>

<?
// now the most important: allowing to see the result and possibly editing
// Note conn.php points to the real conn script hidden below root so no one else but you can access it
// I believe the following to be one of the easier, shorter script to allow a selected choice to be persistent in your form
require_once($_SERVER['DOCUMENT_ROOT']."/conn.php");

$conn = db_connect(); // a function to connect (the one hidden below root)

$sql= "select * from aaaaaa where username='$username' and password='$password' ";
$result = mysql_query($sql,$conn);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i <$num)
{
$market_web= mysql_result($result,$i,"market_web");

?>

<table bgcolor="#cccccc" border="4" width="450" align="center">

<tr>
<td align="center" valign="top" width="450">
<form action ="../update/update_whatever.php" method="post">
<?
echo "<a href=\"../../index.php\"><b>Back Home</b></a><br>";
?>

<b> Review or Edit your Previous Answers.
</b></font>
</tr>

<tr>
<td>

<? echo"<select name=\"market_web\">";
echo"<option selected value=$market_web>$market_web</option>";
echo"<option value=\"yes\">Yes</option>";
echo"<option value=\"no\">No</option>";
echo"</select>";?>

</td>
</tr>
<?
++$i;
}
?>

<?
}
else {
header("Location:index2.php?error=intru"); // redirection en cas d'echec

}
?>

Habtom

1:35 pm on Apr 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am not a new comer but I didn't know about htmlentities :)

Hab

Tastatura

1:55 pm on Apr 17, 2006 (gmt 0)

10+ Year Member



see here
[php.net ]

Habtom

2:09 pm on Apr 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



:) I checked that the moment I read your post.

Thanks though.

Hab

henry0

3:17 pm on Apr 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No taker for: Multiple choices?

coopster

5:32 pm on Apr 18, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



One way to incorporate multiples is to use in_array().
[webmasterworld.com...]

henry0

6:10 pm on Apr 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Now we have it thanks to coopster :)

You need to loop through the result set, not just read the first row. We typically use while loops for this. Think of the result set as a spreadsheet of data coming back to you, rows and columns. You can't just read the first one, you need to loop through and read them all. Let me show you one way to do this:
<<<
$resultID = mysql_query("SELECT contentpage FROM contentpages WHERE username = '$username'", $linkID);
$user_options = array(); // initialize
while ($row = mysql_fetch_assoc($resultID)) {
// build option list
$user_options[] = $row['contentpage'];
}
>>>
Now you have all the options for this user in an array. Much easier to check against using in_array(). Also, in this case, it seems you have a pre-defined list of options. I'll often load them into an array and use them to build the list as well. Sure makes it easier to expand upon later!

<<<<
$valid_options = array(
'Engineering',
'Home',
'News',
'Employee of the Month',
'Business Office',
'Sales',
'Programming',
'Promotions'
);
echo '<select name="assigned_locations[]" size="8" multiple="multiple">';
foreach ($valid_options as $v) {
echo '<option'.(in_array($v, $user_options)? ' selected="selected"' : '').'>'.htmlentities($v).'</option>';
}
echo '</select>';

>>>>