return text from php code inside a <script>

Discussion in 'PHP' started by ma0, Feb 28, 2007.

  1. #1
    Hi, I'm trying to get some result from a remote php page so what I was planning to do is to have a page A with the code:
    <script xxxxx B.php></script>

    This way I can print something taken from page B.
    I was thinking if there is an easy way to do that.
    Maybe I should use javascript too?

    Thanks
    (I hope it's clear what I meant)

    ps. don't say use include because I can't do that. especially if it's on a remote server and it sends cookies.
     
    ma0, Feb 28, 2007 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,841
    Likes Received:
    4,543
    Best Answers:
    123
    Trophy Points:
    665
    #2
    Can you use curl?

    How about an iframe?

    Otherwise you'll need to hunt down an javascript scraper...
     
    sarahk, Feb 28, 2007 IP
  3. hemolack

    hemolack Guest

    Messages:
    31
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    A simple AJAX script maybe?

    
    <script type="text/javascript>
    function GetXmlHttpObject()	// Returns xmlHttp object
    {
    	var obj;
    	var xmlHttp = null;
    	try
    	{	// Firefox, Opera 8.0+, Safari
    		xmlHttp = new XMLHttpRequest();
    	}
    	catch (e)
    	{	// Internet Explorer
    		try
        		{
        			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    		}
    		catch (e)
    		{
    			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    		}
    	}
    	return xmlHttp;
    }
    
    function AjaxGet(element, str, remote) // Pass element id, string, and remote page
    {
    	obj = document.getElementById(element);
    	if (str.length == 0)
    	{ 
    		obj.innerHTML="";
    		return;
    	}
    	xmlHttp=GetXmlHttpObject()
    	if (xmlHttp == null)
    	{
    		alert ("Your browser does not support AJAX!");
    		return;
    	} 
    	var url = remote;
    	url = url + "?q=" + str;
    	url = url + "&sid=" + Math.random();
    	xmlHttp.onreadystatechange = stateChanged;
    	xmlHttp.open("GET", url, true);
    	xmlHttp.send(null);
    } 
    
    function stateChanged() 
    {
    	if (xmlHttp.readyState==4)
    	{ 
    		obj.innerHTML=xmlHttp.responseText;
    	}
    }
    </script>
    
    Code (markup):
    Define a div (let's say it has id="bstuff") to hold the stuff you want from page B. Then call AjaxGet('bstuff', querystring, 'b.php'); The querystring is a string that gets appended to the URL (the page will automatically be called as b.php?q=querystring -- just put in a dummy value for q if you don't use it and add your querystring to the end of it). Once the function is called (by an event handler, I assume), then presto-change-o, your previously empty div with id="bstuff" magically contains whatever b.php spit out.

    May not be exactly what you're looking for, but it has worked well for me in similar situations.
     
    hemolack, Feb 28, 2007 IP
  4. ma0

    ma0 Peon

    Messages:
    218
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I will check it.
    But I'm not sure it's the right thing, and probably is too "heavy"..

    It should be somethink like an Adsense page. I haven't used one yet, but I think they do the same thing I want to do and they surely doesn't ask you to curl or to add a piece of ajax/javascript.

    ;)
     
    ma0, Mar 1, 2007 IP
  5. ma0

    ma0 Peon

    Messages:
    218
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    It seems that my problem is more serious..

    I have a file a.php
    <?php echo "a</br>"; ?>
    <script type="text/javascript" src="bbb.php"></script>
    b

    and a file bbb.php
    with:
    <?php echo "hello world"; ?>

    and all I see is
    a
    b
    and a lovely error on FF javascript console telling me that I should add a ; before "world" on the second file.
    ???
    What am I missing here??
     
    ma0, Mar 1, 2007 IP
  6. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #6
    File bbb.php needs to echo some valid JavaScript - "hello world" is not valid JavaScript... You can try something like this in bbb.php:
    <?php
      echo "alert('Hello World');";
    ?>
    PHP:
     
    picouli, Mar 1, 2007 IP
  7. ma0

    ma0 Peon

    Messages:
    218
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks, you are right.
    Then how can I print something on the screen?
    If I have
    <td><script....</td>
    I want that my script fills that place. Is it possible or I have to do

    <td><div class="iknowu"><script...></div></td>
    and then write a script that change the value of the "iknowu" object?
    (this is maybe a JavaScript question, rather than a php one..sry)
     
    ma0, Mar 1, 2007 IP
  8. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Were you looking for something like this?

    page A
    <?php
    echo "Today's message is: <b>".file_get_contents ('http://someplace/pageb.php').'</b>';
    ?>
    PHP:
    page B
    <?php
    $messages = array ();
    $messages[] = 'Hi there';
    $messages[] = 'Whats up?';
    $messages[] = 'How are you';
    $messages[] = 'Good day to ya';
    
    echo $messages[array_rand ($messages)];
    ?>
    PHP:
     
    exam, Mar 1, 2007 IP
  9. ma0

    ma0 Peon

    Messages:
    218
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Good idea, but the answer is no.
    The reason is simple (but stupid me, I forgot to tell)
    The page B, wich is the page that calls the script, *could* be a simple html page, an asp, wap page, whatever...

    I probably have to put <div as I said..
     
    ma0, Mar 1, 2007 IP
  10. steb

    steb Peon

    Messages:
    213
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #10
    are you looking for this?

    
    <?php  echo "document.write('Hello World');";?>
    
    PHP:
     
    steb, Mar 1, 2007 IP
    ma0 likes this.
  11. ma0

    ma0 Peon

    Messages:
    218
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #11
    ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooh

    document.write!

    I think I need to buy a book about Javascript sooner or later..

    Thanks a lot! I will try it!

    we need a /bow smiley :)
     
    ma0, Mar 1, 2007 IP