Log in

View Full Version : This code is giving me some trouble



itivae
01-12-2013, 05:23 AM
Hello,
This code keeps tripping the error notifier. I've validated it. The $url is valid as I see the output xml if I go to the "real address." Any thoughts on this would be appreciated.



<?php
if (!isset($_GET['qry'])) {

echo '<ul class="listings-results search">';
echo "<li><h4>We’re sorry, search is currently undergoing maintenance.</h4></li></ul>";
}

$qry = urlencode($_GET['qry']);
$url = "http://myserver.com/SearchServlet?cname=newyork&fe=utf-8&st=adv&q_phr=&q_low=&q_not=&oc=all&pagesize=100&q_all={$qry}";

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$xml_string = curl_exec($ch);

curl_close($ch);

$xml = new SimpleXMLElement($xml_string);

echo '<pre>';

//echo var_dump($xml->results); exit;

if ($xml->results>0){
echo '<ul class="listings-results search">';
}
foreach($xml->results->result as $the) {

echo '<li class="listing-results">'.'<a href="'.$the->url.'">'.'<h4>'.fix_curlys($the->title).'</h4>';
echo '<span class="desc">'.fix_curlys($the->description).'</span>'.'</a>'.'</ul>';

}

echo '</pre>';


function fix_curlys($text){

return str_replace(
array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x93", "\xe2\x80\x94", "\xe2\x80\xa6"),
array("'", "'", '"', '"', '-', '--', '...'),
$text);
// Next, replace their Windows-1252 equivalents.
$text = str_replace(
array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(133)),
array("'", "'", '"', '"', '-', '--', '...'),
$text);

}
?>


Here is the input from the search form


<form action="/results2.php" accept-charset="UTF-8" method="post" id="search-form">
<input type="text" id="search-field" maxlength="128" value="Search!" title="Enter the terms you wish to search for." onfocus="if (this.value==this.defaultValue) this.value='';" onblur="if (this.value=='') this.value=this.defaultValue;" name="qry" class="form-text"/>ev
<button class="form-submit"/>
</form>

Thanks in Advance.

Beverleyh
01-12-2013, 07:17 AM
What validator? What 'error notifier'? What's error is being given?

At a glance, the <li class="listing-results"> tag doesn't appear to be closed - there's a closing </ul> but no closing </li>.

If you require further help, please provide more information - clearly state the problem and desired outcome - and provide a link to your page.

itivae
01-12-2013, 07:54 AM
Validated here: http://www.meandeviation.com/tutorials/learnphp/php-syntax-check/v4/syntax-check.php

It trips the under maintenance (i guess error isnt the best term to use) code.

if (!isset($_GET['qry'])) {

echo '<ul class="listings-results search">';
echo "<li><h4>We’re sorry, search is currently undergoing maintenance.</h4></li></ul>";
}

Which I am assuming is because the $qry is not being set by the form.

Beverleyh
01-12-2013, 08:19 AM
Your form uses method="post" but the php in using $_GET

Since you're form is not changing anything on the server, change it to method="get"

method="post" with $_POST

method ="get" with $_GET

More info on that : http://php.net/manual/en/tutorial.forms.php

itivae
01-12-2013, 02:59 PM
Hi Beverleyh,

Thanks for taking the time to try and help me. I have swapped the method to

method="get"
on the search form but it still does not seem to be populating the

$qry
variable.

Beverleyh
01-12-2013, 03:54 PM
It appears to be working OK at this side.

I removed reference to the SimpleXMLElement class and foreach loop to get my test page to to work (obviously, without the SimpleXMLElement class and variables for the foreach loop, my page was throwing up errors) , but it's otherwise getting the search term and setting the variable fine - I just echo'd out the variables on the page so you can see: http://fofwebdesign.co.uk/template/_testing/search-get/test.php

One thing I noticed is that you haven't specified the type attribute for your button (or closed the button tag). That can cause browsers to submit different values so depending on the browsers you're testing in, that may have been the problem. On my test page I set the button type to submit like this;
<button type="submit">SEARCH</button>

itivae
01-12-2013, 03:57 PM
Thanks so much. It turned out that the CMS had reverted the original searchforms code to point to another page :-/ Thank you again for taking a look.