Jump to content

Passing Javascript Output in PHP Variable


Recommended Posts

Folks,

 

Requirement:

I want to pass Javascript Output in a PHP Variable.

 

Purpose:

So that i can find a particular String from that Javascript Output and accordingly apply the control.

 

Javascript:

<script type='text/javascript'>

var amzn_wdgt={widget:'Search'};

amzn_wdgt.tag='powlawofatt-21';

amzn_wdgt.columns='3';

amzn_wdgt.rows='10';

amzn_wdgt.defaultSearchTerm='10 mp 5x zoom';

amzn_wdgt.searchIndex='Electronics';

amzn_wdgt.width='542';

amzn_wdgt.showImage='True';

amzn_wdgt.showPrice='True';

amzn_wdgt.showRating='True';

amzn_wdgt.design='2';

amzn_wdgt.colorTheme='Default';

amzn_wdgt.headerTextColor='#000000';

amzn_wdgt.outerBackgroundColor='#FFFFFF';

amzn_wdgt.marketPlace='GB';

</script>

<script type='text/javascript' src='http://wms.assoc-amazon.co.uk/20070822/GB/js/AmazonWidgets.js'>

</script>

 

 

What i am doing:

$javaoutput = "<script type='text/javascript'>

var amzn_wdgt={widget:'Search'};

amzn_wdgt.tag='powlawofatt-21';

amzn_wdgt.columns='3';

amzn_wdgt.rows='10';

amzn_wdgt.defaultSearchTerm='10 mp 5x zoom';

amzn_wdgt.searchIndex='Electronics';

amzn_wdgt.width='542';

amzn_wdgt.showImage='True';

amzn_wdgt.showPrice='True';

amzn_wdgt.showRating='True';

amzn_wdgt.design='2';

amzn_wdgt.colorTheme='Default';

amzn_wdgt.headerTextColor='#000000';

amzn_wdgt.outerBackgroundColor='#FFFFFF';

amzn_wdgt.marketPlace='GB';

</script>

<script type='text/javascript' src='http://wms.assoc-amazon.co.uk/20070822/GB/js/AmazonWidgets.js'>

</script>";

 

if (strpos($javaoutput, "No results for"))

{

  echo "Sorry no product found";

}

else

echo $javaoutput;

 

 

 

 

Problem:

It seems its outputting the Javascript output in PHP variable $javaoutput but the strpost() does not work.

 

What am i doing wrong? and How to correct it?

 

Cheers

Natasha Thomas

Link to comment
Share on other sites

thats because JavaScript is run by the client after the page has been served and PHP runs serverside BEFORE the page is served.. your trying to first run the javascript and then run the php.

 

 

You need to process the javascript and use that to create a redirect of somesort, using a GET method or post method to submit the data to PHP.

 

 

the variable $javaoutput does not hold the result of the JavaScript.. it holds the script itself.

when you:

 

echo ($javaoutput);

 

 

the result is :

<script type='text/javascript'>

var amzn_wdgt={widget:'Search'};

amzn_wdgt.tag='powlawofatt-21';

amzn_wdgt.columns='3';

amzn_wdgt.rows='10';

amzn_wdgt.defaultSearchTerm='10 mp 5x zoom';

amzn_wdgt.searchIndex='Electronics';

amzn_wdgt.width='542';

amzn_wdgt.showImage='True';

amzn_wdgt.showPrice='True';

amzn_wdgt.showRating='True';

amzn_wdgt.design='2';

amzn_wdgt.colorTheme='Default';

amzn_wdgt.headerTextColor='#000000';

amzn_wdgt.outerBackgroundColor='#FFFFFF';

amzn_wdgt.marketPlace='GB';

</script>

<script type='text/javascript' src='http://wms.assoc-amazon.co.uk/20070822/GB/js/AmazonWidgets.js'>

</script>

 

instead of the values you expect returned by that script..

Link to comment
Share on other sites

FROM PHP MANUAL:

 

strpos

 

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "".

 

 

so you should specifically check for the false value.

 

if (strpos($amacontent, "No results for") === false)
{
    echo "Products found!";
}
else
{
   echo "sorry no products found";
}

Link to comment
Share on other sites

FROM PHP MANUAL:

 

strpos

 

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "".

 

 

so you should specifically check for the false value.

 

if (strpos($amacontent, "No results for") === false)
{
    echo "Products found!";
}
else
{
   echo "sorry no products found";
}

 

 

I tired this code, the pointer always goes to TURE Condition... I mean it always echos

 

"Products found!"

 

Even when the "No Results found" appear in the String.

 

What is going worng?

 

This is what i did:

 

 

<?php


$amacontent = file_get_contents('http://minimate.co.uk/master/scripts/content/amazonsearch.php?kw=noprodcamera&cat=Electronics');


if (strpos($amacontent, "No results for") === false)
{
    echo "Products found!";
}
else
{
   echo "sorry no products found";
}

echo $amacontent;

?>

 

Link to comment
Share on other sites

<?php


$amacontent = file_get_contents('http://minimate.co.uk/master/scripts/content/amazonsearch.php?kw=noprodcamera&cat=Electronics');


if (strpos($amacontent, "No results for") === false) //if the string "No results for" IS NOT FOUND in $amacontent, then echo "Products found!".  This line should read "if (strpos($amacontent, "No results for") !== false)"
{
    echo "Products found!";
}
else
{
   echo "sorry no products found";
}

echo $amacontent;

?>

Link to comment
Share on other sites

<?php


$amacontent = file_get_contents('http://minimate.co.uk/master/scripts/content/amazonsearch.php?kw=noprodcamera&cat=Electronics');


if (strpos($amacontent, "No results for") === false) //if the string "No results for" IS NOT FOUND in $amacontent, then echo "Products found!".  This line should read "if (strpos($amacontent, "No results for") !== false)"
{
    echo "Products found!";
}
else
{
   echo "sorry no products found";
}

echo $amacontent;

?>

 

 

This solution also does not work. :-(

 

The pointer remains either in TRUE or FALSE irrespective of product found or not.  :-(

Link to comment
Share on other sites

when I view the source page of the html file the only thing there is Javascript code.. I believe for file_get_contents.. it basically retrieves the code in the document as a string.. so $amacontent doesn't contain the string "No results for".

 

thats why its not working. .. just for kicks plug the plain text "No results for" into the document. see if it works then.

Link to comment
Share on other sites

when I view the source page of the html file the only thing there is Javascript code.. I believe for file_get_contents.. it basically retrieves the code in the document as a string.. so $amacontent doesn't contain the string "No results for".

 

thats why its not working. .. just for kicks plug the plain text "No results for" into the document. see if it works then.

 

Ur right, the JS code itself goes into the PHP Variable, that's why its not finding "no Result for" string.

 

Is there any way we can ouput the JS into PHP Variable?

 

Below is the JS:

 

<script type='text/javascript'>
var amzn_wdgt={widget:'Search'};
amzn_wdgt.tag='powlawofatt-21';
amzn_wdgt.columns='3';
amzn_wdgt.rows='10';
amzn_wdgt.defaultSearchTerm='10 mp 5x zoom';
amzn_wdgt.searchIndex='Electronics';
amzn_wdgt.width='525';
amzn_wdgt.showImage='True';
amzn_wdgt.showPrice='True';
amzn_wdgt.showRating='True';
amzn_wdgt.design='2';
amzn_wdgt.colorTheme='Default';
amzn_wdgt.headerTextColor='#000000';
amzn_wdgt.outerBackgroundColor='#FFFFFF';
amzn_wdgt.marketPlace='GB';
</script>
<script type='text/javascript' src='http://wms.assoc-amazon.co.uk/20070822/GB/js/AmazonWidgets.js'>
</script>

 

 

 

Cheers

Natasha

Link to comment
Share on other sites

Your missing a fundamental concept. JavaScript executes within a client's browser, PHP on the other hand executes on the server long before any page is served to a browser.

 

To do what your asking you would need to write some JavaScript code (Ajax) to send the data back to the server.

 

We have an Ajax board, if your stuck, your question belongs there. Firstly however I suggest looking at one of the JavaScript frameworks (jQuery is very easy and very popular), these all provide Ajax functionality that is easy to implement.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.