7 Replies - 1232 Views - Last Post: 30 May 2008 - 03:08 AM Rate Topic: -----

#1 jumbledup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-May 08

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

Post icon  Posted 29 May 2008 - 03:54 PM

Hi all,

Relatively new to proper PHP coding (DW user - :blush: ) 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.

//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: ). 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 - 04:52 PM


Is This A Good Question/Topic? 0
  • +

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

#2 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 760
  • Joined: 09-March 08

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

Posted 29 May 2008 - 05:05 PM

try using the str_replace function
Was This Post Helpful? 0
  • +
  • -

#3 jumbledup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-May 08

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

Posted 29 May 2008 - 05:08 PM

View PostJBrace1990, on 29 May, 2008 - 05:05 PM, said:

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 - 05:16 PM

Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5380
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

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

Posted 29 May 2008 - 06:59 PM

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.
<input type="hidden" value="...">



Quote

how do I use $pcsearch in my SELECT statement?


<?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;
}
?>


Was This Post Helpful? 0
  • +
  • -

#5 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

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

Posted 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.
Was This Post Helpful? 0
  • +
  • -

#6 jumbledup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-May 08

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

Posted 30 May 2008 - 02:14 AM

View Postno2pencil, on 29 May, 2008 - 06:59 PM, said:

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: )

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! :D


View Postakozlik, on 29 May, 2008 - 10:36 PM, said:

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:

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 :P

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

Thanks again.
Was This Post Helpful? 0
  • +
  • -

#7 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5380
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

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

Posted 30 May 2008 - 02:15 AM

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.
Was This Post Helpful? 0
  • +
  • -

#8 jumbledup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-May 08

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

Posted 30 May 2008 - 03:08 AM

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!!

:D::D::D::D:

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

	$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."%'");


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1