SpringVark Posted October 18, 2009 Share Posted October 18, 2009 Newbie here, apologies in advance! I am trying to use the header function to forward one of any number of locations (taken from a db) based on the value of a response param: <?php include '../include/db/db-open.php'; ?> <?php $postTypeId = $_POST['postTypeId']; $postTypesResults = $mysqli->query("select id, editPageUrl from post_types where active = 1"); ?> <?php include '../include/db/db-close.php'; ?> <?php while ($postTypesResult = $postTypesResults->fetch_object()) { if ($postTypesResult->id == $postTypeId) { header("Location: ".$postTypesResult->editPageUrl); } } ?> The value of $postTypeId is always blank if echoed (explining why the condition to forward is never met). Two questions: [*]Is there anything obvious I am doing wrong causing $postTypeId to always be blank? [*]Can the header function be used in this way? Thanks for any help! Quote Link to comment https://forums.phpfreaks.com/topic/178133-reditecting-pages-based-on-logic/ Share on other sites More sharing options...
Daniel0 Posted October 18, 2009 Share Posted October 18, 2009 First of all, you should use a MySQL WHERE instead of selecting a lot and doing a manual linear search through the result set. Regarding $postTypeId, have you tried echoing it? Are you sure it's the right name? Quote Link to comment https://forums.phpfreaks.com/topic/178133-reditecting-pages-based-on-logic/#findComment-939217 Share on other sites More sharing options...
SpringVark Posted October 18, 2009 Author Share Posted October 18, 2009 Hi Yes, I have echoed $postTypeId and the result is nothing. The page it is coming from includes a select tag with name="postTypeId"; Is the MySql WHERE function you describe a php function? And if so, how would I use it? I only ask out of ignorance (my sql did include a where clause). Quote Link to comment https://forums.phpfreaks.com/topic/178133-reditecting-pages-based-on-logic/#findComment-939222 Share on other sites More sharing options...
Daniel0 Posted October 18, 2009 Share Posted October 18, 2009 I mean kind of like this (also changed some other stuff to make it look nicer). <?php include '../include/db/db-open.php'; $postTypesResults = $mysqli->query("SELECT editPageUrl FROM post_types WHERE active = 1 AND id = " . ((int) $_POST['postTypeId']) . " LIMIT 1"); include '../include/db/db-close.php'; if ($postTypesResult = $postTypesResults->fetch_object()) { header("Location: ".$postTypesResult->editPageUrl); exit; } else { echo 'Link not found'; } Yes, I have echoed $postTypeId and the result is nothing. The page it is coming from includes a select tag with name="postTypeId"; Are you sure it's submitting using POST and not GET? Quote Link to comment https://forums.phpfreaks.com/topic/178133-reditecting-pages-based-on-logic/#findComment-939224 Share on other sites More sharing options...
SpringVark Posted October 18, 2009 Author Share Posted October 18, 2009 Thanks for the page updates - I'm stoopid for having tried to loop through it. Yes, it's definitely a POST, defined as such: <form name="defaultForm" method="post" action="<?php echo $path_to_root ?>/admin/editPostForward.php"> Quote Link to comment https://forums.phpfreaks.com/topic/178133-reditecting-pages-based-on-logic/#findComment-939237 Share on other sites More sharing options...
Daniel0 Posted October 18, 2009 Share Posted October 18, 2009 Try doing var_dump($_POST); before the query. The file db-open.php wouldn't happen to mess with $_POST, right? Quote Link to comment https://forums.phpfreaks.com/topic/178133-reditecting-pages-based-on-logic/#findComment-939238 Share on other sites More sharing options...
SpringVark Posted October 18, 2009 Author Share Posted October 18, 2009 The result of the var_dump: array( { ["postTypeId"]=> string(0) "" ["publisher"]=> string(0) "" ["author"]=> string(0) "" ["title"]=> string(0) "" ["tagline"]=> string(0) "" ["content"]=> string(0) "" ["dateToPublish"]=> string(0) "" ["dateToExpire"]=> string(0) "" } Link not found Very weird as most of those other fileds are simply text boxes (some of which I'd filled in). db-open.php shouldn't cause any problems: <?php $mysqli = new mysqli('localhost','auser','password','dbname'); ?> I'm stumped. Quote Link to comment https://forums.phpfreaks.com/topic/178133-reditecting-pages-based-on-logic/#findComment-939253 Share on other sites More sharing options...
SpringVark Posted October 18, 2009 Author Share Posted October 18, 2009 I LIED in my previous post - those input text field values DID come through: array( { ["postTypeId"]=> string(0) "" ["publisher"]=> string(4) "Jill" ["author"]=> string(4) "Jack" ["title"]=> string(11) "Jack & Jill" ["tagline"]=> string(12) "Go Up a hill" ["content"]=> string(0) "" ["dateToPublish"]=> string(0) "" ["dateToExpire"]=> string(0) "" } Link not found So it looks like the problem must be with my select list... *rushes off to take another look-see* Quote Link to comment https://forums.phpfreaks.com/topic/178133-reditecting-pages-based-on-logic/#findComment-939258 Share on other sites More sharing options...
Daniel0 Posted October 18, 2009 Share Posted October 18, 2009 Try posting the HTML source of your form. Quote Link to comment https://forums.phpfreaks.com/topic/178133-reditecting-pages-based-on-logic/#findComment-939261 Share on other sites More sharing options...
SpringVark Posted October 18, 2009 Author Share Posted October 18, 2009 Here's the first bit: <form name="defaultForm" method="post" action="/skydive/admin/editPostForward.php"> <table border="0" cellpadding="5"> <thead> <tr> <th>Field</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>Post Type</td> <td> <select name="postTypeId"> <option value="1">News Item</option> <option value="2">Weekend Report</option> <option value="3">Notice</option> <option value="4">Site Page: About</option> <option value="5">Site Page: Static Line</option> </select> </td> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/178133-reditecting-pages-based-on-logic/#findComment-939267 Share on other sites More sharing options...
Daniel0 Posted October 18, 2009 Share Posted October 18, 2009 Strange. Is there another field later on with the same name? If there is it will override it. Quote Link to comment https://forums.phpfreaks.com/topic/178133-reditecting-pages-based-on-logic/#findComment-939276 Share on other sites More sharing options...
SpringVark Posted October 18, 2009 Author Share Posted October 18, 2009 You, are a genius. Rather I, am a dumbass. All this for a duplicated field name: <td>Priority</td> <td> <select name="postTypeId"> Thank you, I owe you bigtime. it now behaves as intended. Quote Link to comment https://forums.phpfreaks.com/topic/178133-reditecting-pages-based-on-logic/#findComment-939282 Share on other sites More sharing options...
SpringVark Posted October 18, 2009 Author Share Posted October 18, 2009 One remaining question... What is the best way to take the $_POST from my originating page to the destination page (i.e. where I forward to)? Currently I get this as a result of the var_dump on the destination page: array(0) { } Quote Link to comment https://forums.phpfreaks.com/topic/178133-reditecting-pages-based-on-logic/#findComment-939290 Share on other sites More sharing options...
Daniel0 Posted October 18, 2009 Share Posted October 18, 2009 I'm not sure I understand your question. If you need to use some information across multiple requests you should use sessions. http://www.phpfreaks.com/tutorial/sessions-and-cookies-adding-state-to-a-stateless-protocol Quote Link to comment https://forums.phpfreaks.com/topic/178133-reditecting-pages-based-on-logic/#findComment-939297 Share on other sites More sharing options...
SpringVark Posted October 18, 2009 Author Share Posted October 18, 2009 You understood me correctly. I thought that maybe somehow the header function would pass on the previous $_POST over the multiple requests. But I see not. Sessions it shall be then - thanks you once more. Quote Link to comment https://forums.phpfreaks.com/topic/178133-reditecting-pages-based-on-logic/#findComment-939313 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.