stylemessiah Posted October 8, 2009 Share Posted October 8, 2009 okay, so i have a search form that has some optional fields, so when the form is submitted using the GET method to its processing php page, the resulting URL may have blank values in it, and it gets quite long. What id like to know is is there a way to either get the form to ignore the blank form fields when you submit the form, or to have the form processing page rewrite the URL to remove the blank key/value pairs? I would use POST except that i need to paginate the results, and using POST makes this impossible AFAIK (as the minute you go to say page 2 the POST variables are lost) without using SESSION variables or a cookie...not great either when users may want to bookmark the results pages Any help would be greatly appreciated Link to comment https://forums.phpfreaks.com/topic/176967-post-methodget-empty-values-in-url/ Share on other sites More sharing options...
Glenugie Posted October 8, 2009 Share Posted October 8, 2009 If I understand what you're looking for, I use something similar in a few of my web sites: if (empty($_GET['To'])) { $To='Null'; } else { $To=$_GET['To']; } Hope this helps ~Glenugie~ Link to comment https://forums.phpfreaks.com/topic/176967-post-methodget-empty-values-in-url/#findComment-933063 Share on other sites More sharing options...
PFMaBiSmAd Posted October 8, 2009 Share Posted October 8, 2009 You can pass GET parameters on the end of the URL (your pagination values) and use POST variables for your form data at the same time. Link to comment https://forums.phpfreaks.com/topic/176967-post-methodget-empty-values-in-url/#findComment-933069 Share on other sites More sharing options...
Garethp Posted October 8, 2009 Share Posted October 8, 2009 $i = 0; foreach($_GET as $k=>$v) { if(!preg_match('~[\S]~', $v)) { unset($_GET[$k]); $i = 1; } } if($i == 1) { $URL = $_SERVER["SCRIPT_NAME"]; $URL .= "?"; foreach($_GET as $k=>$v) { $URL .= $k . "=" . $v . "&"; } $URL = substr_replace($URL,"",-1); header("Location: " . $URL); } Link to comment https://forums.phpfreaks.com/topic/176967-post-methodget-empty-values-in-url/#findComment-933076 Share on other sites More sharing options...
stylemessiah Posted October 8, 2009 Author Share Posted October 8, 2009 thanks for the quick replies people ill look into your suggestions after a bit of a snooze, i just wanted to say a quick thanks for the very quick suggestions Link to comment https://forums.phpfreaks.com/topic/176967-post-methodget-empty-values-in-url/#findComment-933093 Share on other sites More sharing options...
stylemessiah Posted October 8, 2009 Author Share Posted October 8, 2009 $i = 0; foreach($_GET as $k=>$v) { if(!preg_match('~[\S]~', $v)) { unset($_GET[$k]); $i = 1; } } if($i == 1) { $URL = $_SERVER["SCRIPT_NAME"]; $URL .= "?"; foreach($_GET as $k=>$v) { $URL .= $k . "=" . $v . "&"; } $URL = substr_replace($URL,"",-1); header("Location: " . $URL); } Thanks, but this chokes on arrays in the URL, for example: page.php?test[]=1&test[]=2 Link to comment https://forums.phpfreaks.com/topic/176967-post-methodget-empty-values-in-url/#findComment-933126 Share on other sites More sharing options...
Garethp Posted October 8, 2009 Share Posted October 8, 2009 function Check($Input) { foreach($Input as $k=>$v) { if(is_array($v)) { $Input[$k] = Check($v); } else { if(!preg_match("~\S~", $v)) { unset($Input[$k]); } } } return $Input; } $GET2 = Check($_GET); if($GET2 != $_GET) { $URL = $_SERVER["SCRIPT_NAME"]; $URL .= "?"; foreach($GET2 as $k=>$v) { $URL .= $k . "=" . $v . "&"; } $URL = substr_replace($URL,"",-1); header("Location: " . $URL); } Link to comment https://forums.phpfreaks.com/topic/176967-post-methodget-empty-values-in-url/#findComment-933135 Share on other sites More sharing options...
Garethp Posted October 8, 2009 Share Posted October 8, 2009 After testing I realised that it messed up in the redirect thing. This will work, for multidimensional arrays aswell <?php function Check($Input) { foreach($Input as $k=>$v) { if(is_array($v)) { $Input[$k] = Check($v); } else { if(!preg_match("~\S~", $v)) { unset($Input[$k]); } } } return $Input; } function Make2($Input, $Start) { $Output = ""; foreach($Input as $k=>$v) { if(is_array($v)) { $S = $Start . "[" . $k . "]"; $Output .= Make2($v, $S); } else { $Output .= $Start . "[" . $k . "]=" . $v . "&"; } } //$Output = substr_replace($Output,"",-1); return $Output; } function Make($Input) { $Output = ""; foreach($Input as $k=>$v) { if(is_array($v)) { /* foreach($v as $k2=>$v2) { $Output .= $k . "[" . $k2 . "]=" . $v2 . "&"; } */ $Output .= Make2($v, $k); } else { $Output .= $k . "=" . $v . "&"; } } $Output = substr_replace($Output,"",-1); return $Output; } $GET2 = Check($_GET); if($GET2 != $_GET) { $URL = $_SERVER["SCRIPT_NAME"]; $URL .= "?"; $GET2 = Make($GET2); $URL .= $GET2; header("Location: " . $URL); } ?> Link to comment https://forums.phpfreaks.com/topic/176967-post-methodget-empty-values-in-url/#findComment-933148 Share on other sites More sharing options...
stylemessiah Posted October 10, 2009 Author Share Posted October 10, 2009 After testing I realised that it messed up in the redirect thing. This will work, for multidimensional arrays aswell <?php function Check($Input) { foreach($Input as $k=>$v) { if(is_array($v)) { $Input[$k] = Check($v); } else { if(!preg_match("~\S~", $v)) { unset($Input[$k]); } } } return $Input; } function Make2($Input, $Start) { $Output = ""; foreach($Input as $k=>$v) { if(is_array($v)) { $S = $Start . "[" . $k . "]"; $Output .= Make2($v, $S); } else { $Output .= $Start . "[" . $k . "]=" . $v . "&"; } } //$Output = substr_replace($Output,"",-1); return $Output; } function Make($Input) { $Output = ""; foreach($Input as $k=>$v) { if(is_array($v)) { /* foreach($v as $k2=>$v2) { $Output .= $k . "[" . $k2 . "]=" . $v2 . "&"; } */ $Output .= Make2($v, $k); } else { $Output .= $k . "=" . $v . "&"; } } $Output = substr_replace($Output,"",-1); return $Output; } $GET2 = Check($_GET); if($GET2 != $_GET) { $URL = $_SERVER["SCRIPT_NAME"]; $URL .= "?"; $GET2 = Make($GET2); $URL .= $GET2; header("Location: " . $URL); } ?> Thanks for that, its perfect. I was going to write my own code to do similar, but relatively new to php, so appreciate you taking the time to write and share it. Ive fully commented as i went along, so should be easy to follow. Sorry for the formatting, it was well laid out but as usual i had to rejig it as long lines get mangled in forum posts By way of sharing with the readers her and in thanks for the code, i thought id post the code i wrote that processes the GET elements into a usable WHERE clause ofr a mysql query. GET to WHERE clause function: function createwhereclause( $array ) { //split the array into key=>value pairs foreach( $array as $key => $value ) { //if there is a get key, and its not empty or NULL, then process it //although the above rewrite code eliminates blank entries, i like //to be comprehensive. but for people not using the rewrite code //above its valid. If (isset($_GET[$key]) && !empty($value) || ($value==!NULL)){ //check is the value of the current key is an array, if so process it //ARRAY SECTION if ( is_array($value) ) { //iterate through the key=>value pairs of the array foreach ($value as $key2 => $newvalue){ //for the first element of the array, we need the operator to be and //AND, in case this //array key=>value pair is appended to an exisiting WHERE clause //generated by an non array key=>value pair if ($key2==0){ $operator =" AND "; }else{ //for all key=>value pairs in the array, we need the operator to be //OR $operator =" OR "; } //create the WHERE clause, its cumulative, if no WHERE clause //exists, we start one //the first line opens the WHERE clause //the second line inserts the first key=>value pair //the last line appends any remaining key=>value pairs, seperated //by the OR operator $where .= ($where == "") ? "where ".$key ."='".addslashes($newvalue)."'" : $operator.$key ."='".addslashes($newvalue)."'"; } } else{ //NON-ARRAY SECTION //not a lot of processing to do here //set the operator to AND $operator =" AND "; //create the WHERE clause, its cumulative, if no WHERE clause //exists, we start one //the first line opens the WHERE clause //the second line inserts the first key=>value pair //the last line appends any remaining key=>value pairs, seperated //by the AND operator $where .= ($where == "") ? "where ".$key ."='".addslashes($value)."'" : $operator.$key ."='".addslashes($value)."'"; } }else{ } } //return the WHERE clause for inclusion into a mysql SELECT query return $where; } Its used on the site im building to list real estate properties, so it searches on columns iin the database as opposed to a keyword search. And as such ive created a form with the suburbs dynamically read form the db and presented as checkboxes for multiple selections, hence the array stuff mentioned earlier. so i use the search form, which submits to the searchresults.php page which houses the following code. i use the rewrite header code at the top of the searchresults.php file, then the GET to WHERE function appears. Then i call the Get to WHERE function like this: [icode] $where = createwhereclause( $_GET); [/code] The use the $where variable in the mysql query like this: [icode] $query1 = mysql_query("SELECT * FROM houses ". $where); [/code] and then onto the display code Hope this isnt boring and is somehow useful to others in future. [/code] Link to comment https://forums.phpfreaks.com/topic/176967-post-methodget-empty-values-in-url/#findComment-934241 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.