Form queries, tricky for me

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bubblebobble
Forum Newbie
Posts: 7
Joined: Tue Nov 22, 2011 5:00 am

Form queries, tricky for me

Post by bubblebobble »

First post here, Hi !

I have been struggling with this for a couple of days, even losing sleep.

I figure this is a simple thing to acheive but while my html,css is excellent my PHP scripting skills are poor, but I am having ago.

SCENERIO:

I have a search box on one of my sites.
It is a standard html form.
when the query is entered it POSTs to an external URL which contains a catalogue of goods, the external domain then sends the data into an IFRAME on my domain.
It works flawslessly no problems.

However, there are several pages all coded in html from scratch that do not contain an IFRAME for display purposes but contain the search box; the search box is intergrated in the NAVBAR.

Also the search box is on my index page which does not contain an iframe; if one is implemented it will destroy the sites look.

WHAT I NEED TO ACHEIVE:

I need to send the search query from the non iframed pages to another generic page containing the iframe. Simple I thought but no, I cant use javascript due to Cross site security , I have tried. here is my form.


Code: Select all

<form method="post" action="external sites catalogue" target=content>
        
	    <fieldset>
				
                
				<input type="text" class="text" name=lookfor value="Search" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"maxlength="255" />
                
                
				<input type="image" src="images/topnav/btn_search.png" class="button" />
		  </fieldset>
		</form>
Any help and espically example code would be great, it is really showing how limited standard html is. I am sure PHP url get functions can be implemented to acheive this simple task.

thankyou
Last edited by Benjamin on Tue Nov 22, 2011 7:24 am, edited 1 time in total.
Reason: Added [syntax=php||htm||css||javascript||sql||etc] - Please use [syntax] tags when posting code in the forums! Thanks.
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

Re: Form queries, tricky for me

Post by maxx99 »

If your server is able to do that you can use multiple methods to get external content on PHP side.

You can usehttp://php.net/manual/en/function.file-get-contents.php

Here's another example, socket use:

Code: Select all

<?php 
function http_request( 
      $verb = 'GET',             /* HTTP Request Method (GET and POST supported) */ 
      $ip,                       /* Target IP/Hostname */ 
      $port = 80,                /* Target TCP port */ 
      $uri = '/',                /* Target URI */ 
      $getdata = array(),        /* HTTP GET Data ie. array('var1' => 'val1', 'var2' => 'val2') */ 
      $postdata = array(),       /* HTTP POST Data ie. array('var1' => 'val1', 'var2' => 'val2') */ 
      $cookie = array(),         /* HTTP Cookie Data ie. array('var1' => 'val1', 'var2' => 'val2') */ 
      $custom_headers = array(), /* Custom HTTP headers ie. array('Referer: http://localhost/ */ 
      $timeout = 1000,           /* Socket timeout in milliseconds */ 
      $req_hdr = false,          /* Include HTTP request headers */ 
      $res_hdr = false           /* Include HTTP response headers */ 
      ) 
  { 
      $ret = ''; 
      $verb = strtoupper($verb); 
      $cookie_str = ''; 
      $getdata_str = count($getdata) ? '?' : ''; 
      $postdata_str = ''; 

      foreach ($getdata as $k => $v) 
                  $getdata_str .= urlencode($k) .'='. urlencode($v) . '&'; 

      foreach ($postdata as $k => $v) 
          $postdata_str .= urlencode($k) .'='. urlencode($v) .'&'; 

      foreach ($cookie as $k => $v) 
          $cookie_str .= urlencode($k) .'='. urlencode($v) .'; '; 

      $crlf = "\r\n"; 
      $req = $verb .' '. $uri . $getdata_str .' HTTP/1.1' . $crlf; 
      $req .= 'Host: '. $ip . $crlf; 
      $req .= 'User-Agent: Mozilla/5.0 Firefox/3.6.12' . $crlf; 
      $req .= 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' . $crlf; 
      $req .= 'Accept-Language: en-us,en;q=0.5' . $crlf; 
      $req .= 'Accept-Encoding: deflate' . $crlf; 
      $req .= 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7' . $crlf; 
      
      foreach ($custom_headers as $k => $v) 
          $req .= $k .': '. $v . $crlf; 
          
      if (!empty($cookie_str)) 
          $req .= 'Cookie: '. substr($cookie_str, 0, -2) . $crlf; 
          
      if ($verb == 'POST' && !empty($postdata_str)) 
      { 
          $postdata_str = substr($postdata_str, 0, -1); 
          $req .= 'Content-Type: application/x-www-form-urlencoded' . $crlf; 
          $req .= 'Content-Length: '. strlen($postdata_str) . $crlf . $crlf; 
          $req .= $postdata_str; 
      } 
      else $req .= $crlf; 
      
      if ($req_hdr) 
          $ret .= $req; 
      
      if (($fp = @fsockopen($ip, $port, $errno, $errstr)) == false) 
          return "Error $errno: $errstr\n"; 
      
      stream_set_timeout($fp, 0, $timeout * 1000); 
      
      fputs($fp, $req); 
      while ($line = fgets($fp)) $ret .= $line; 
      fclose($fp); 
      
      if (!$res_hdr) 
          $ret = substr($ret, strpos($ret, "\r\n\r\n") + 4); 
      
      return $ret; 
  } 
?> 

  Example usages : 

<?php 
  $string =  http_request('GET', 'bulksms.mysmsmantra.com', 8080,'/WebSMS/SMSAPI.jsp' , array('username'=>'dsdasd',"password"=>'sdasdasd','sendername'=>'xxx','mobileno'=>'111','message'=>'xxx'));

echo $string;
?>
bubblebobble
Forum Newbie
Posts: 7
Joined: Tue Nov 22, 2011 5:00 am

Re: Form queries, tricky for me

Post by bubblebobble »

thankyou for the reply.

From what I have read on the link you so kindly provided it seems this may be the way forward.
I think its time to construct a few pages and try it out, although I will be honest and say only about 20% of the code makes sense to me at the moment.
I will report back shortly.

cheers
bubblebobble
Forum Newbie
Posts: 7
Joined: Tue Nov 22, 2011 5:00 am

Re: Form queries, tricky for me

Post by bubblebobble »

ok, I am lost somewhere.
I know that learning and understanding is a great thing but it does take time, and while I am not looking for someone to do the task for me I think if possible it would help if you held my hand a little more please.

So,this is what I need to accomplish.

I need to be able to take a search query, as per form above.
Send it to a remote server via the server address, as per form above.
then the resulting data displayed in an IFRAME on another of my webpages, NOT the one I sent the query from.

The site is basically hosting all the info and just passing me html to display.

thankyou
bubblebobble
Forum Newbie
Posts: 7
Joined: Tue Nov 22, 2011 5:00 am

Re: Form queries, tricky for me

Post by bubblebobble »

apologies for the constant replies but I think this example would describe what I need to acheive

Code: Select all

<form method="post" action="external sites catalogue" target=content>
       
            <fieldset>
                               
               
                                <input type="text" class="text" name=lookfor value="Search" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"maxlength="255" />
               
               
                                <input type="image" src="images/topnav/btn_search.png" class="button" />
                  </fieldset>
                </form>
target=content> this statement if html would allow would be

target="another html page", put it within an iframe on the page called "iframe"

simple in php i presume
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

Re: Form queries, tricky for me

Post by maxx99 »

Some example I wrote with before you wrote last post :)
form.html

Code: Select all

<form method="post" action="anothersite.php"  target=content>
        <input type="text"  name="lookfor" value="Search"  />
        <input type="submit" value="Search"/>
</form>
anothersite.php

Code: Select all

<?php
if(!empty($_POST['lookfor'])){
   $url = "http://google.com/search?q=".$_POST['lookfor']; //where google.com is external page for search
   $content = file_get_contents($url);
   echo $content;
}
?>
bubblebobble
Forum Newbie
Posts: 7
Joined: Tue Nov 22, 2011 5:00 am

Re: Form queries, tricky for me

Post by bubblebobble »

Hi,

I am back to address this nightmare issue for me.
I have tried and tried to solve this but I am missing something ( maybe a brain ?) :roll:

I have been unable to get this to function, so I very reluctantly disabled the search facility on my landing page due to it not having an iframe.

This is what I want to do so could somebody please post the exact .php page and html page so I can get to grips with it?

Scenerio:

A search query is posted from a search form to another external website and the result is displayed in an iframe on the same page.
Works fine.

To be acheived:

I have a landing page which includes the search form but NOT an Iframe.
I require the the search string to be entered into the form and displayed in another page which contains an Iframe.


Please see what you guys can do, I think max has it but I cant make it work, here is more current search form again:

Code: Select all

<form method="get" action="http://URL FOR SEARCH" target=content>
        
	    <fieldset>
				
                
				<input type="text" class="text" name="keywords" value="Search" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"maxlength="30" />
                
                
				<input type="image" src="images/topnav/btn_search.png" class="button" />
		  </fieldset>
		</form>
like I say this works fine on a page containing an Iframe named "content"

Code: Select all

<iframe src="http://IFRAME DEFAULT URL" name="content" width="803" height="1200" align="left">
Thankyou
bubblebobble
Forum Newbie
Posts: 7
Joined: Tue Nov 22, 2011 5:00 am

Re: Form queries, tricky for me

Post by bubblebobble »

I ve been doing more reading,

I think the problem my lay in the fact that my html page containing the Iframe needs to rendered before the search query is submitted.

So therefore I need the initial search query string from the page WITHOUT the Iframe to be processed after the Iframe is created on the other page. :banghead:
There has to be a script to do just what I ask...surely?
bubblebobble
Forum Newbie
Posts: 7
Joined: Tue Nov 22, 2011 5:00 am

Re: Form queries, tricky for me

Post by bubblebobble »

Just to make it clear,

the returned data is a html page.

thanks
Post Reply