natasha_thomas Posted May 7, 2011 Share Posted May 7, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235751-passing-javascript-output-in-php-variable/ Share on other sites More sharing options...
efficacious Posted May 7, 2011 Share Posted May 7, 2011 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.. Quote Link to comment https://forums.phpfreaks.com/topic/235751-passing-javascript-output-in-php-variable/#findComment-1211820 Share on other sites More sharing options...
natasha_thomas Posted May 7, 2011 Author Share Posted May 7, 2011 <?php $amacontent = file_get_contents('http://mydomain.co.uk/amazonsearch.php?kw=sjfhjshj&cat=Electronics'); if (strpos($amacontent, "No results for")) { echo "sorry no products found"; } ?> Even this is not working... Quote Link to comment https://forums.phpfreaks.com/topic/235751-passing-javascript-output-in-php-variable/#findComment-1211831 Share on other sites More sharing options...
efficacious Posted May 7, 2011 Share Posted May 7, 2011 whats the string returned by the php page? or rather whats the result if you echo $amacontent? Quote Link to comment https://forums.phpfreaks.com/topic/235751-passing-javascript-output-in-php-variable/#findComment-1211832 Share on other sites More sharing options...
efficacious Posted May 7, 2011 Share Posted May 7, 2011 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/235751-passing-javascript-output-in-php-variable/#findComment-1211834 Share on other sites More sharing options...
natasha_thomas Posted May 7, 2011 Author Share Posted May 7, 2011 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/235751-passing-javascript-output-in-php-variable/#findComment-1211836 Share on other sites More sharing options...
efficacious Posted May 7, 2011 Share Posted May 7, 2011 case may be sensitive make sure the needle and the string match exactly Quote Link to comment https://forums.phpfreaks.com/topic/235751-passing-javascript-output-in-php-variable/#findComment-1211837 Share on other sites More sharing options...
natasha_thomas Posted May 7, 2011 Author Share Posted May 7, 2011 case may be sensitive make sure the needle and the string match exactly Case is not the issuse, am sure.... moreover i tried to use stripos() as well. No luck... Quote Link to comment https://forums.phpfreaks.com/topic/235751-passing-javascript-output-in-php-variable/#findComment-1211840 Share on other sites More sharing options...
trq Posted May 7, 2011 Share Posted May 7, 2011 Really, it wouldn't hurt you to try and debug your own code, what have you done? echo $amacontent to see what it is actually returning. Quote Link to comment https://forums.phpfreaks.com/topic/235751-passing-javascript-output-in-php-variable/#findComment-1211842 Share on other sites More sharing options...
natasha_thomas Posted May 7, 2011 Author Share Posted May 7, 2011 Hi Thrope, echo $amacontent gives the Output of Javascrpt. var_dump($amacontent) gives string(674) " and then the ouput of Javascript. Quote Link to comment https://forums.phpfreaks.com/topic/235751-passing-javascript-output-in-php-variable/#findComment-1211850 Share on other sites More sharing options...
spiderwell Posted May 7, 2011 Share Posted May 7, 2011 if all you are doing is writing out a string depending on an outcome in javascript, why not use javascript to write out the string and keep the whole thing in one language? just an idea... Quote Link to comment https://forums.phpfreaks.com/topic/235751-passing-javascript-output-in-php-variable/#findComment-1211871 Share on other sites More sharing options...
jcbones Posted May 7, 2011 Share Posted May 7, 2011 <?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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/235751-passing-javascript-output-in-php-variable/#findComment-1211948 Share on other sites More sharing options...
natasha_thomas Posted May 7, 2011 Author Share Posted May 7, 2011 <?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. :-( Quote Link to comment https://forums.phpfreaks.com/topic/235751-passing-javascript-output-in-php-variable/#findComment-1212024 Share on other sites More sharing options...
trq Posted May 8, 2011 Share Posted May 8, 2011 Again, debug your code by looking at the contents of $amacontent. Quote Link to comment https://forums.phpfreaks.com/topic/235751-passing-javascript-output-in-php-variable/#findComment-1212141 Share on other sites More sharing options...
efficacious Posted May 9, 2011 Share Posted May 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235751-passing-javascript-output-in-php-variable/#findComment-1212655 Share on other sites More sharing options...
natasha_thomas Posted May 11, 2011 Author Share Posted May 11, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235751-passing-javascript-output-in-php-variable/#findComment-1213577 Share on other sites More sharing options...
trq Posted May 11, 2011 Share Posted May 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235751-passing-javascript-output-in-php-variable/#findComment-1213754 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.