Be Excellent To Each Other

And, you know, party on. Dude.

All times are UTC [ DST ]




Reply to topic  [ 28 posts ] 
Author Message
 Post subject: PHP help
PostPosted: Fri Nov 14, 2008 17:25 
Awesome
User avatar
Yes

Joined: 6th Apr, 2008
Posts: 12334
Hello all.

I am making my first forray into php coding and would like you to point me in the correct direction please.

I wish to have a search box, and when someone types their query into said search box it takes that term, and sticks it into a URL thus http://www.example.com/?searchtermhere& ... ady_in_url

Then that URL will return an XML file which I can stick on a page somewhere.

Is there eitehr an easy way of doing this that someone would be kind enough to point out, or at the very least lead me to the correct part of the php manual?

_________________
Always proof read carefully in case you any words out


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Fri Nov 14, 2008 17:40 
User avatar
That Rev Chap

Joined: 31st Mar, 2008
Posts: 4924
Location: Kent
Have you already got the page you're redirecting to? It might not be the best way to transfer user input if not.

_________________
InvertY


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Fri Nov 14, 2008 17:54 
Awesome
User avatar
Yes

Joined: 6th Apr, 2008
Posts: 12334
To be honest, I'm not sure.

What happens is that I want this search box to query a database on another site. That database querying is already set up apparently, but here is an example of how things should work.

User types 'basset hound' into search box. Search box sticks the words basset hound into the URL eg http://www.dogsearch.com/?basset%20hound&type=pedigree.
Magic happens, and an XML document is sent back in the form of

Code:
<xml><dog>basset hound</dog>
<location>cheshire</location></xml>


I then want to stick that xml output into a page of results.

Does this even make sense??

_________________
Always proof read carefully in case you any words out


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Fri Nov 14, 2008 17:56 
User avatar
Heavy Metal Tough Guy

Joined: 31st Mar, 2008
Posts: 6608
Yeah, how is the XML being generated? That may not be the best way to do it.


Any way , I guess you want some html saying

Code:
<form action="my_php_script.php" type="GET">
<input type="text" name="search_term" />
<input type="submit" />
</form>


Would send it off the the URL /my_php_script.php?search_term=what_i_typed_in_the_box

Hitting the button will send the search term off to the PHP script, which will do stuff with it. I know nothing of PHP, but I could do stuff in perl if you fancied. Use perl. All the cool kids are you know. You'd use perl if you loved me.


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Fri Nov 14, 2008 17:58 
Excellent Member

Joined: 30th Mar, 2008
Posts: 489
sounds like you want to do something like this:
http://www.tgreer.com/class_http_php.html


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Fri Jan 23, 2009 0:02 
Awesome
User avatar
Yes

Joined: 6th Apr, 2008
Posts: 12334
Hello, I'm back, and looking for more help. On my site (link in sig) I've got a variable $searchString which is passed from the first page to my results page, and it then displays "You searched for $searchString" eg. "You searched for Resistance+2+Fall+Of+Man"

How do I go about removing the + symbols from that variable for display? And should I do it before the search input is stored as $searchString or after?

_________________
Always proof read carefully in case you any words out


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Fri Jan 23, 2009 0:04 
User avatar

Joined: 30th Mar, 2008
Posts: 14370
Location: Shropshire, UK
You searched for <?php echo htmlspecialchars(strip_tags(urldecode($searchString)));?>

urldecode() gets rid of the url-encoding in the variable, converting +'s to spaces. Or at least, it should do.

strip_tags() gets rid of any HTML/PHP tags in the variable, and is recommended for security, otherwise anyone could just type <script>alert('arse');</script> into your search box and execute JavaScript code on the client.

htmlspecialchars() then makes sure that anything else that may have slipped through the 'net security wise is encoded as HTML before being spat out, and this should cover the rest of it. htmlentities() may be a better choice here.


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Fri Jan 23, 2009 0:36 
Awesome
User avatar
Yes

Joined: 6th Apr, 2008
Posts: 12334
That's brilliant GazChap, thanks!

It's already wrapped in a massive php wrapper though, so i changed the relevant part to:

Code:
         echo '<tr><td><h2>Your Search Results for &quot;';
echo htmlspecialchars(strip_tags(urldecode($searchString)));
         echo '&quot;...</h2><p class="intro">Bargain Comp......etc....


Although it doesn't appear to strip out html brackets when I tried them...

_________________
Always proof read carefully in case you any words out


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Fri Jan 23, 2009 10:26 
User avatar

Joined: 30th Mar, 2008
Posts: 14370
Location: Shropshire, UK
It won't strip out HTML angle brackets unless they form a HTML tag. It won't strip out <> but it will strip out <b> for example.


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Fri Jan 23, 2009 10:32 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69715
Location: Your Mum
Code:
$searchString = str_replace("<", "", str_replace(">", "", $searchString));

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Fri Jan 23, 2009 10:51 
User avatar

Joined: 30th Mar, 2008
Posts: 14370
Location: Shropshire, UK
Or:
Code:
$searchString = preg_replace("/(<|>)/", "", $searchString);


;)


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Fri Jan 23, 2009 11:04 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69715
Location: Your Mum
GazChap wrote:
Or:
Code:
$searchString = preg_replace("/(<|>)/", "", $searchString);


;)

Actually, you could get rid of strip_tags with
Code:
$searchString = htmlspecialchars(urldecode(preg_replace("/(<.*?>)/", "", $searchString)));

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Sun Feb 01, 2009 17:08 
Awesome
User avatar
Yes

Joined: 6th Apr, 2008
Posts: 12334
Back again:
I have a variable which will either be 'not blank' or 'returns value "N/A"'

Code:
if ((!$variable==' ') OR ($variable=='N/A')) {
echo 'my output';


Is that the correct way to do it?

Edit:
I've realised that by having a value of "N/A" the code automatically qualifies as 'not blank'.

Is there a way to say " echo 'my output'; " if $variable is not blank, and is also not N/A ?

_________________
Always proof read carefully in case you any words out


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Sun Feb 01, 2009 22:32 
User avatar
Be Excellent Member

Joined: 30th Mar, 2008
Posts: 98
Mr Russell wrote:
Is there a way to say " echo 'my output'; " if $variable is not blank, and is also not N/A ?

Code:
if( $variable != ' ' && $variable != 'N/A' ) {
   echo 'my output';
}


or

Code:
if( !( $variable == ' ' || $variable == 'N/A' ) ) {
   echo 'my output';
}


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Sun Feb 01, 2009 22:39 
Awesome
User avatar
Yes

Joined: 6th Apr, 2008
Posts: 12334
That's beautiful. Thankee!

You wouldn't realise I'm a novice would you? ;)

_________________
Always proof read carefully in case you any words out


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Thu Aug 20, 2009 22:34 
Awesome
User avatar
Yes

Joined: 6th Apr, 2008
Posts: 12334
Thread Ressurecto-o

If I have this code:
Code:
<form name="classic" method="post" action="error.php">
Quicksearch: <input name="INPUT" id="INPUT" type="text" value="" />
<select name="countries" onchange="updatecities(this.selectedIndex); document.classic.action=(this.value)">
<option value="" selected>Choose a category</option>
<option value="search1.php">Category One</option>
<option value="search2.php">Category Two</option>
<option value="search3.php">Category Three</option>
<option value="search4.php">Category Four</option>
</select>

<select name="cities">
</select>

<input type="image" name="search" src="search.png" />
</form>

<script type="text/javascript">

var countrieslist=document.classic.countries
var citieslist=document.classic.cities

var cities=new Array()
cities[0]=""
cities[1]=["A|Avalue"]
cities[2]=["B|bvalue", "C|cvalue", "D|dvalue", "E|evalue"]
cities[3]=["F|fvalue", "G|gvalue"]
cities[4]=["H|hvalue", "J|jvalue"]

function updatecities(selectedcitygroup){
citieslist.options.length=0
if (selectedcitygroup>0){
for (i=0; i<cities[selectedcitygroup].length; i++)
citieslist.options[citieslist.options.length]=new Option(cities[selectedcitygroup][i].split("|")[0], cities[selectedcitygroup][i].split("|")[1])
}
}

</script>
at the top of every page of my site, how can I get it to retain the values of the selected dropdowns?

This is the code running the search on my comparison site currently, and it's annoying to have to reselect the category and subcategory you want each time you do a new search.

_________________
Always proof read carefully in case you any words out


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Thu Aug 20, 2009 22:46 
User avatar
Isn't that lovely?

Joined: 30th Mar, 2008
Posts: 11168
Location: Devon
Mr Russell wrote:
Thread Ressurecto-o

If I have this code:
Code:
<form name="classic" method="post" action="error.php">
Quicksearch: <input name="INPUT" id="INPUT" type="text" value="" />
<select name="countries" onchange="updatecities(this.selectedIndex); document.classic.action=(this.value)">
<option value="" selected>Choose a category</option>
<option value="search1.php">Category One</option>
<option value="search2.php">Category Two</option>
<option value="search3.php">Category Three</option>
<option value="search4.php">Category Four</option>
</select>

<select name="cities">
</select>

<input type="image" name="search" src="search.png" />
</form>

<script type="text/javascript">

var countrieslist=document.classic.countries
var citieslist=document.classic.cities

var cities=new Array()
cities[0]=""
cities[1]=["A|Avalue"]
cities[2]=["B|bvalue", "C|cvalue", "D|dvalue", "E|evalue"]
cities[3]=["F|fvalue", "G|gvalue"]
cities[4]=["H|hvalue", "J|jvalue"]

function updatecities(selectedcitygroup){
citieslist.options.length=0
if (selectedcitygroup>0){
for (i=0; i<cities[selectedcitygroup].length; i++)
citieslist.options[citieslist.options.length]=new Option(cities[selectedcitygroup][i].split("|")[0], cities[selectedcitygroup][i].split("|")[1])
}
}

</script>
at the top of every page of my site, how can I get it to retain the values of the selected dropdowns?

This is the code running the search on my comparison site currently, and it's annoying to have to reselect the category and subcategory you want each time you do a new search.



surely you want to be using cookies for that?

Malc

_________________
Where's the Kaboom? I was expecting an Earth shattering Kaboom!


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Thu Aug 20, 2009 23:43 
User avatar

Joined: 30th Mar, 2008
Posts: 14370
Location: Shropshire, UK
If you have a PHP file that is included in every page of your site, put this somewhere in it:
Code:
session_start();


Then, in error.php, put this line in once in the code that runs after the form has been validated:
Code:
$_SESSION['formvars'] = $_POST;


Then, do this on the form code itself:
Code:
<form name="classic" method="post" action="error.php">
Quicksearch: <input name="INPUT" id="INPUT" type="text" value="<?php echo htmlspecialchars($_SESSION['formvars']['INPUT']);?>" />
<select name="countries" onchange="updatecities(this.selectedIndex); document.classic.action=(this.value)">
<option value="">Choose a category</option>
<option value="search1.php" <?php if ($_SESSION['formvars']['countries'] == "search1.php") echo " selected";?>>Category One</option>
<option value="search2.php" <?php if ($_SESSION['formvars']['countries'] == "search2.php") echo " selected";?>>Category Two</option>
<option value="search3.php" <?php if ($_SESSION['formvars']['countries'] == "search3.php") echo " selected";?>>Category Three</option>
<option value="search4.php" <?php if ($_SESSION['formvars']['countries'] == "search4.php") echo " selected";?>>Category Four</option>
</select>

<select name="cities">
</select>

<input type="image" name="search" src="search.png" />
</form>

<script type="text/javascript">

var countrieslist=document.classic.countries
var citieslist=document.classic.cities

var cities=new Array()
cities[0]=""
cities[1]=["A|Avalue"]
cities[2]=["B|bvalue", "C|cvalue", "D|dvalue", "E|evalue"]
cities[3]=["F|fvalue", "G|gvalue"]
cities[4]=["H|hvalue", "J|jvalue"]

function updatecities(selectedcitygroup){
citieslist.options.length=0
if (selectedcitygroup>0){
for (i=0; i<cities[selectedcitygroup].length; i++)
citieslist.options[citieslist.options.length]=new Option(cities[selectedcitygroup][i].split("|")[0], cities[selectedcitygroup][i].split("|")[1])
}
}

</script>


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Fri Aug 21, 2009 0:22 
Awesome
User avatar
Yes

Joined: 6th Apr, 2008
Posts: 12334
Thanks for the reply GazChap, but I think I've buggered it up.

I'm not including a php file, rather the whole page is a .php file so I put the session start thing in as <?php session_start();?>

The error.php is not really used though. That's just for if someone presses search without selecting any options, so I'm not really sure where to put the $_SESSION['formvars'] = $_POST; part ??

Here is the code for the complete page once I've fed my own stuff into it rather than that example stuff above:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<title>Bargain Comparison.com - Compare prices and find the Cheapest DVDs, CDs, Games and Books</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="page_header">

   <div id="header_search">
<?php session_start();?>
<form name="classic" method="post" action="error.php">
Quicksearch: <input name="INPUT" id="INPUT" type="text" size="40" value="<?php echo htmlspecialchars($_SESSION['formvars']['INPUT']);?>" />
<select name="countries" onchange="updatecities(this.selectedIndex); document.classic.action=(this.value)" style="width: 135px">
<option value="">Choose a category</option>
<option value="searchdvds.php" <?php if ($_SESSION['formvars']['countries'] == "searchdvds.php") echo " selected";?>>DVDs</option>
<option value="searchgames.php" <?php if ($_SESSION['formvars']['countries'] == "searchgames.php") echo " selected";?>>Games</option>
<option value="searchcds.php" <?php if ($_SESSION['formvars']['countries'] == "searchcds.php") echo " selected";?>>CDs</option>
<option value="searchbooks.php" <?php if ($_SESSION['formvars']['countries'] == "searchbooks.php") echo " selected";?>>Books</option>
</select>

<select name="cities" style="width: 135px">
</select>

<input type="image" name="search" src="search.png" style="position:relative;top:4px;" />
</form>

<script type="text/javascript">

var countrieslist=document.classic.countries
var citieslist=document.classic.cities

var cities=new Array()
cities[0]=""
cities[1]=["Title|dvdtitle"]
cities[2]=["XBox 360|xbox360", "PS3|PS3", "Wii|wii", "DS|NDS", "PSP|PSP", "PC|PC", "XBox|XBox", "PlayStation|PS", "PS2|PS2", "GameCube|GCube", "Gameboy Advance|GA"]
cities[3]=["Title|cdtitle", "Artist|artist"]
cities[4]=["Title|booktitle", "Author|author"]

function updatecities(selectedcitygroup){
citieslist.options.length=0
if (selectedcitygroup>0){
for (i=0; i<cities[selectedcitygroup].length; i++)
citieslist.options[citieslist.options.length]=new Option(cities[selectedcitygroup][i].split("|")[0], cities[selectedcitygroup][i].split("|")[1])
}
}

</script>
<?php $_SESSION['formvars'] = $_POST;?>
   </body>
</html>


That whole set of code is on every page of the site. You can see this in action at http://www.bargaincomparison.com/unused/ to see more what I'm trying to do??

_________________
Always proof read carefully in case you any words out


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Fri Aug 21, 2009 7:45 
User avatar

Joined: 30th Mar, 2008
Posts: 32624
Does PHP really not have any more elegant way of preserving the state of HTML controls than that? I'm no fan of ASP's viewstate -- partly because I've seen it somehow manage to contribute hundreds of kb to page sizes -- but it's very neat from a development perspective.


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Fri Aug 21, 2009 9:22 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69715
Location: Your Mum
Mr Russell wrote:
so I'm not really sure where to put the $_SESSION['formvars'] = $_POST; part ??

Top of the page, before you start outputting any HTML at all.

Doctor Glyndwr wrote:
Does PHP really not have any more elegant way of preserving the state of HTML controls than that?

Yes. Or no. That's a wonderfully-worded question to try and answer ;)

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Sat Aug 22, 2009 16:18 
Awesome
User avatar
Yes

Joined: 6th Apr, 2008
Posts: 12334
Hmm, I put
Code:
<?php $_SESSION['formvars'] = $_POST;?>
<?php session_start();?>

right at the start of the file, before the doctype declaration, and although I don't get the errors any more, it doesn't retain the drop downs either.
http://www.bargaincomparison.com/unused/

_________________
Always proof read carefully in case you any words out


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Sat Aug 22, 2009 20:43 
User avatar

Joined: 30th Mar, 2008
Posts: 14370
Location: Shropshire, UK
Swap the lines round.


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Sat Aug 22, 2009 21:57 
Awesome
User avatar
Yes

Joined: 6th Apr, 2008
Posts: 12334
GazChap wrote:
Swap the lines round.


Aha! That's got it to remember the drop down, excellent! Thanks.

Only problem now is that the conditional dropdown has now broken.
http://www.bargaincomparison.com/unused/

_________________
Always proof read carefully in case you any words out


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Mon Aug 24, 2009 11:26 
User avatar

Joined: 30th Mar, 2008
Posts: 14370
Location: Shropshire, UK
Add id="countries" into your select box, i.e.:
Code:
<select name="countries" id="countries" ... >


Then put this at the bottom of the page, just before </script>

Code:
window.onload = function() {
    countryElement = document.getElementById('countries');
    updatecities(countryElement.selectedIndex, '<?php echo htmlspecialchars($_SESSION['formvars']['cities']);?>'); document.classic.action=(countryElement.options[countryElement.selectedIndex].value);
}


What that'll do is, after the page has finished loading, it'll run the updatecities function based on the selected value of the main drop down, which -should- update the conditionals dropdown. Although it might not select the same one.

You could sort that by changing updatecities to this function
Code:
function updatecities(selectedcitygroup, selectedvalue){
    citieslist.options.length=0
    if (selectedcitygroup>0){
        for (i=0; i<cities[selectedcitygroup].length; i++) {
            citieslist.options[citieslist.options.length]=new Option(cities[selectedcitygroup][i].split("|")[0], cities[selectedcitygroup][i].split("|")[1])
            if (selectedvalue && cities[selectedcitygroup][i].split("|")[1] == selectedvalue) {
               citieslist.options[citieslist.options.length - 1].selected = true;
            }
        }
    }
}
(d'oh, can't use bold in code tags)

What I've done there is changed updatecities so that it accepts a selected value from the $_SESSION on the window.load event, and then there's some extra code in the function that sets it to the right value if it's the right one.

Should work in theory, but can't know for sure until you try it, if it doesn't I can sort it :)


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Mon Aug 24, 2009 13:06 
Awesome
User avatar
Yes

Joined: 6th Apr, 2008
Posts: 12334
I think that's cracked it!

You're a bloody star.

I think I may have to owe you a massive jug of beer.

_________________
Always proof read carefully in case you any words out


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Mon Aug 24, 2009 14:10 
User avatar

Joined: 30th Mar, 2008
Posts: 14370
Location: Shropshire, UK
I'll settle for letting me win at Words ;)


Top
 Profile  
 
 Post subject: Re: PHP help
PostPosted: Mon Aug 24, 2009 14:28 
Awesome
User avatar
Yes

Joined: 6th Apr, 2008
Posts: 12334
GazChap wrote:
I'll settle for letting me win at Words ;)


I guess it's my move then... NEVARRR!

_________________
Always proof read carefully in case you any words out


Top
 Profile  
 
Display posts from previous:  Sort by  
Reply to topic  [ 28 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: BikNorton, Columbo and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search within this thread:
You are using the 'Ted' forum. Bill doesn't really exist any more. Bogus!
Want to help out with the hosting / advertising costs? That's very nice of you.
Are you on a mobile phone? Try http://beex.co.uk/m/
RIP, Owen. RIP, MrC. RIP, Dimmers.

Powered by a very Grim... version of phpBB © 2000, 2002, 2005, 2007 phpBB Group.