Forum Moderators: coopster

Message Too Old, No Replies

setting variable using drop down box

variable, string, dropdown box,

         

jonk

8:48 am on Aug 6, 2007 (gmt 0)

10+ Year Member



Hi, 1st post - I'm fairly new to PHP (as everyone always says!) and I have a news page that selects data from an sql db, and displays using 'select x from x ORDER BY x DESC LIMIT x' which works fine.
I want the user to select the number of items to display using a drop down box (eg. show 1, 2, 3, 4 or 5 entries) by replacing LIMIT x with LIMIT $whatever. $whatever needs to be set using drop down. Any ideas please? Should be fairly simple.

Habtom

8:59 am on Aug 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jonk, Welcome to Webmasterworld

The dropdown
<form action=$_SERVER['PHP_SELF'] name=limitnews>
<select name="limit_option" onchange="document.limitnews.submit();return false;"> <option value=5> 5 News </option><option value=10> 10 News </option> . . . . </select>
</form>

The $whatever can be retrieved from
$_REQUEST['limit_option']

I hope this helps, it might need some modifications.

jonk

9:03 am on Aug 6, 2007 (gmt 0)

10+ Year Member



Thanks for the quick reply...

I've tried

<?php

//code to show number of news items.

if ($_SERVER['REQUEST_METHOD']!= 'POST')

{
$rowsPerPage = $_post['choice'];

echo

'<form name="choice" method="post" action="post">

<select name="choice">

<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>

</select>

<input type="submit" name="choice" value="submit"/>

</form>';}?>

Would it work?

I'll try your idea later tonight at home...

jonk

9:18 am on Aug 6, 2007 (gmt 0)

10+ Year Member



Actually, looking at it, your idea seems simpler, so it now looks like this....

<?php

//code to show number of news items.

echo

'<form action=$_SERVER['PHP_SELF'] name=limitnews>

<select name="limit_option" onchange="document.limitnews.submit();return false;">

<option value=1>1 News</option>
<option value=2>2 News</option>
..
..
..
<option value=10>10 News</option>

</select>

</form>';

?>

Do I set the variable in the display code like this?

// how many rows to show per page
$rowsPerPage = 'limitnews';

instead of

// how many rows to show per page
$rowsPerPage = x;

jonk

9:52 am on Aug 6, 2007 (gmt 0)

10+ Year Member



It's ok, I've got it working....

For those interested, this is my drop down form

<?php

//code to show number of news items.

?>

<form action=all_news.php name='limitnews'>;

<select name="limit_option" onchange="document.limitnews.submit();return false;">

<option value=1>1 News</option>
..
..
..
<option value=10>10 News</option>

</select>

</form>

And my 'display number of' code is this...

include 'display_number.php';

// how many rows to show per page

$rowsPerPage = $_REQUEST['limit_option'];

Habtom

10:38 am on Aug 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you got it working.

$rowsPerPage = $_REQUEST['limit_option'];

Don't trust data collected from forms, and you should be able to clean up and all kinds of SQL injections and similar problems.

Habtom

jonk

10:49 am on Aug 6, 2007 (gmt 0)

10+ Year Member



Just one more thing I can't figure out yet....

When I look at my news page, I get a db query error because the value for $rowsPerPage has not been set by the drop down. What's the easiest way to have this variable set as 4 for example, becasue setting it as $rowsPerPage=4 or whatever doesn't work? Is this because setting it, then looking for a drop down change causes it to revert to nothing?

Habtom

10:58 am on Aug 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I get a db query error because the value for $rowsPerPage has not been set by the drop down.

Is this happening when you submit the form? Or before submitting the form.

If it is before submitting the form, it could be because you are just including "include 'display_number.php';" even when the form is not submitted leading the $rowsPerPage & $_REQUEST['limit_option'] have empty values.

The code in display_number should be used after checking that the form is indeed submitted:

if ($_REQUEST['limit_option']) {
// Proceed with the query and other lines
}

jonk

11:00 am on Aug 6, 2007 (gmt 0)

10+ Year Member



it was before the form was submitted....

Had a sniff about and I'm using this..

if(!isset($_REQUEST["limit_option"])) $rowsPerPage = "4";
else $rowsPerPage = $_REQUEST["limit_option"];

This works fine.

What was your warning about not trusting $rowsPerPage = $_REQUEST["limit_option"];?

I'm still learning!

Habtom

11:09 am on Aug 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should at least the function htmlentities($_REQUEST[limit]) while adding data to the database.

Habtom