kartul Posted August 10, 2009 Share Posted August 10, 2009 ok, i'm trying to make a thing that users can choose how many results they want to see. idea is: default it shows 25. there is select option to select how many results display per page. when user selects something, it submits the form and sets cookie with selected number. now, i have a problem. I don't want submit button but now, how do i check if the form is submitted with php? heres form: <form action="index.php" method="post" name="display"> Display <select name="show" onchange="this.form.submit()"> <option value="25">25</option> <option value="30">30</option> </select> </form> and here's php code to set cookie: function give_cookie($how_many) { setcookie("display", $how_many, time()+60*60*24*30*365*5); } if(isset($_POST['display'])) { $show = mss($_POST['show']); if(is_int($show)) { give_cookie($show); } } right now, when I select example 30, it submits the form, refreshes the page but nothing else happens. one more thing, if I set display cookie to 25 at the top of the page and then later want to use it, it's blank, and when i refresh the page, then it ok. if(!isset($_COOKIE['display'])) { setcookie("display", "25", time()+60*60*24*30*365*5); } Quote Link to comment Share on other sites More sharing options...
alexdemers Posted August 10, 2009 Share Posted August 10, 2009 What does the mms() function do? Does it really return the value submitted? Quote Link to comment Share on other sites More sharing options...
alexdemers Posted August 10, 2009 Share Posted August 10, 2009 Also, if (isset($_POST['display'])) might not work because the actual form element will not send data to the server. It's its elements inside that will. So, you can just check isset($_POST['show']) and it will work. Edit-Adding: Naming a form element's purpose is mostly just for JavaScript. You shouldn't name it. It might work in IE (haven't tried it) but I'm guessing future browser might not work. Quote Link to comment Share on other sites More sharing options...
kartul Posted August 10, 2009 Author Share Posted August 10, 2009 What does the mms() function do? Does it really return the value submitted? mss() is security function. just in case function mss($value) { return mysql_real_escape_string(trim(strip_tags(stripslashes(htmlentities(htmlspecialchars($value)))))); } Quote Link to comment Share on other sites More sharing options...
kartul Posted August 10, 2009 Author Share Posted August 10, 2009 Also, if (isset($_POST['display'])) might not work because the actual form element will not send data to the server. It's its elements inside that will. So, you can just check isset($_POST['show']) and it will work. Edit-Adding: Naming a form element's purpose is mostly just for JavaScript. You shouldn't name it. It might work in IE (haven't tried it) but I'm guessing future browser might not work. ok, i changed $_POST['display'] to $_POST['show']. but it's still just refreshes page and cookie value is still default - 25. maybe problem is somewhere else? you mean <form action="index.php" method="post" name="display"> or <select name="show" onchange="this.form.submit()">? Quote Link to comment Share on other sites More sharing options...
alexdemers Posted August 10, 2009 Share Posted August 10, 2009 It's caused with is_int(); it's returning false because a string is returned from mms(). So the best bet is to do if ($show > 0). PHP: is_int - Manual will return true ONLY if the type of the variable is an integer; not if it can be parsed as integer. Quote Link to comment Share on other sites More sharing options...
kartul Posted August 10, 2009 Author Share Posted August 10, 2009 ok, i tried following: just to be sure. this doesn't work. if(isset($_POST['show'])) { $show = $_POST['show']; if(is_int($show)) { //give_cookie($show); setcookie("display", "30", time()*60*60); } } now I tried this: if(isset($_POST['show'])) { $show = mss($_POST['show']); if($show > 0) { give_cookie($show); } } ok, this works perfectly now. thank you! and it should be sql injection proof too? anyway, now I have another issue. default is 25. when I select 30. it submits the form and does it's thing, but i need to refresh the page to make it display 30 results per page.. how do change that so it displays 30 right after submitting the form? Quote Link to comment Share on other sites More sharing options...
alexdemers Posted August 10, 2009 Share Posted August 10, 2009 ok, i tried following: just to be sure. this doesn't work. if(isset($_POST['show'])) { $show = $_POST['show']; if(is_int($show)) { //give_cookie($show); setcookie("display", "30", time()*60*60); } } now I tried this: if(isset($_POST['show'])) { $show = mss($_POST['show']); if($show > 0) { give_cookie($show); } } ok, this works perfectly now. thank you! and it should be sql injection proof too? anyway, now I have another issue. default is 25. when I select 30. it submits the form and does it's thing, but i need to refresh the page to make it display 30 results per page.. how do change that so it displays 30 right after submitting the form? I'm guessing that you do the cookie change after the parsing of the page. Try putting the if(isset($_POST['show'])) block before you echo out all of the items/articles/results. Quote Link to comment Share on other sites More sharing options...
kartul Posted August 10, 2009 Author Share Posted August 10, 2009 ok, i tried following: just to be sure. this doesn't work. if(isset($_POST['show'])) { $show = $_POST['show']; if(is_int($show)) { //give_cookie($show); setcookie("display", "30", time()*60*60); } } now I tried this: if(isset($_POST['show'])) { $show = mss($_POST['show']); if($show > 0) { give_cookie($show); } } ok, this works perfectly now. thank you! and it should be sql injection proof too? anyway, now I have another issue. default is 25. when I select 30. it submits the form and does it's thing, but i need to refresh the page to make it display 30 results per page.. how do change that so it displays 30 right after submitting the form? I'm guessing that you do the cookie change after the parsing of the page. Try putting the if(isset($_POST['show'])) block before you echo out all of the items/articles/results. i'm changing the cookie before i echo out any result. it's on the top of the page. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.