Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,636 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,074 people online right now. Registration is fast and FREE... Join Now!




Is it possible to pass a string via header() without %20

 
Reply to this topicStart new topic

Is it possible to pass a string via header() without %20

jumbledup
post 29 May, 2008 - 02:54 PM
Post #1


New D.I.C Head

*
Joined: 29 May, 2008
Posts: 5


My Contributions


Hi all,

Relatively new to proper PHP coding (DW user - blush.gif ) and have come unstuck. I'm passing a variable from a form (users enter their Postcode - ZIP code) and it's supposed to strip away any surplus characters before using that variable as a $_GET['x'] variable in the MySQL recordset. The problem I'm having, however, is that when I pass the variable ($pcsearch) from the 2nd page (convertpc.php - code below) I can't seem to get rid of the %20 and replace it with a space. Any help would be greatly appreciated.

CODE

//Split a postcode into two parts
$postcode = $_POST['postcode'];
$postcode2 = strtolower($_POST['postcode']);

if (!checkPostcode ($postcode2) ) {
    echo 'Invalid postcode <br /> <a href="java script:history.back();">Back</a>';
    }
    else {
    $postcodearray = explode(" ", $postcode);
    $pcin = $postcodearray[0];
    $pc2 = $postcodearray[1];
    $pcout = $pc2[0];
    $pcsearch = ($pcin." ".$pcout);
    }
    

/* Redirect to a different page in the current directory that was requested */
$pcsearch = str_replace("%20", " ", $pcsearch);
$url = 'getresultsfromdatabase.php?postcode='.$pcsearch;
header("Location: ".$url);
exit();


*UPDATE --- I've just been sat thinking about this -- is there any way that I can just pass the variable $pcsearch directly to the search results page and, by calling this function on the search results page first, perform this function before anything else, thereby removing the need to use the header() function (which seems to be the thing causing me the headache huh.gif ). If the function takes the variable as $_POST['postcode'] and outputs the result $pcsearch, how do I use $pcsearch in my SELECT statement?

Thanks very much for your help.

This post has been edited by jumbledup: 29 May, 2008 - 03:52 PM
User is offlineProfile CardPM

Go to the top of the page

JBrace1990
post 29 May, 2008 - 04:05 PM
Post #2


D.I.C Regular

Group Icon
Joined: 9 Mar, 2008
Posts: 474



Thanked 21 times

Dream Kudos: 350
My Contributions


try using the str_replace function
User is offlineProfile CardPM

Go to the top of the page

jumbledup
post 29 May, 2008 - 04:08 PM
Post #3


New D.I.C Head

*
Joined: 29 May, 2008
Posts: 5


My Contributions


QUOTE(JBrace1990 @ 29 May, 2008 - 05:05 PM) *

try using the str_replace function


Thanks JBrace

I've already tried that to no avail - I even tried rawurldecode...

One thing I failed to mention is that at the beginning of this page there's a require_once to a function that validates postcodes - this works perfectly well and explains the if(!checkPostcode... bit of code.

Any further recommendations appreciated.

JumbledUp

This post has been edited by jumbledup: 29 May, 2008 - 04:16 PM
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 29 May, 2008 - 05:59 PM
Post #4


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,354



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


What if you changed all spaces to plus signs & then when it was received, you change them back.

If you want to pass the value with $_POST, you'll need to send it with a form. You can use it as a hidden type.
CODE

<input type="hidden" value="...">


QUOTE

how do I use $pcsearch in my SELECT statement?


CODE

<?php
if ($i == $_POST['pcsearch']) {
    echo "i equals 0";
}

switch ($i) {
case 0:
    echo "i equals 0";
    break;
case 1:
    echo "i equals 1";
    break;
case 2:
    echo "i equals 2";
    break;
}
?>
User is online!Profile CardPM

Go to the top of the page

akozlik
post 29 May, 2008 - 09:36 PM
Post #5


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


Try html entities. It takes symbols and turns them into html safe values. Hence, < turns into &less;

You can find more information at tizag.org. I'm kind of a whore for that site.
User is offlineProfile CardPM

Go to the top of the page

jumbledup
post 30 May, 2008 - 01:14 AM
Post #6


New D.I.C Head

*
Joined: 29 May, 2008
Posts: 5


My Contributions


QUOTE(no2pencil @ 29 May, 2008 - 06:59 PM) *

CODE

if ($i == $_POST['pcsearch']) {
    echo "i equals 0";
}

switch ($i) {
case 0:
    echo "i equals 0";
    break;
case 1:
    echo "i equals 1";
    break;
case 2:
    echo "i equals 2";
    break;
}
?>



Thank you for your response -- sadly you lost me with the switch above (I haven't studied switches yet!! blush.gif )

I think I might have resolved this because my script uses explode, so if I get it to explode on the "%20" it might work - I'll get back to you if it works.

Thanks again! biggrin.gif


QUOTE(akozlik @ 29 May, 2008 - 10:36 PM) *

Try html entities. It takes symbols and turns them into html safe values. Hence, < turns into &less;

You can find more information at tizag.org. I'm kind of a whore for that site.



Thanks for the link akozlik. Quite a useful site so they can be my pimp too! pimp.gif

The form that passes the postcode variable to the function is limited to 8 characters and there's also a validity check prior to the other functions taking place, so if someone tries to bugger my site up it's covered (well, at least I think it is tongue.gif

Will definitely take your advice to heart when I get to developing a user form with more fields!!

Thanks again.
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 30 May, 2008 - 01:15 AM
Post #7


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,354



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


Switch : Right from PHP themselves.

Switch is just a multiple if statement, with a default fall-out.

Let us know if you have any more questions.
User is online!Profile CardPM

Go to the top of the page

jumbledup
post 30 May, 2008 - 02:08 AM
Post #8


New D.I.C Head

*
Joined: 29 May, 2008
Posts: 5


My Contributions


Thanks everyone -- I've resolved it. I passed the $postcode direct to the search results page and it's worked!! -- Alright, I cheated somewhat, had outsourced the advanced search on the site to another company and I studied their code for about 1 hour. But, results!!

biggrin.gif:biggrin.gif:biggrin.gif:biggrin.gif:

This is the code for those of you that might want to create something similar:

CODE

    $postcode1 = $_POST['postcode'];
    $postcode2 = strtolower($_POST['postcode']);

    if (!checkPostcode ($postcode2) ) {
    echo 'Invalid postcode <br /> <a href="java script:history.back();">Back</a>';
    exit;
    }
    else {
    $pcarray = explode(" ",$postcode1);
    $pcin = $pcarray[0];
    $pc2 = $pcarray[1];
    $pcout = $pc2[0];
    $pcsearch = ($pcin." ".$pcout);
    }
    
    $resulttype = mysql_query("SELECT * FROM listings l WHERE l.postcode LIKE '%".$pcsearch."%'");
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 04:10AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month