Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

PHP embed <script> tag into if else statement

Posted on 2013-06-10
12
339 Views
Last Modified: 2013-09-17
Hi,

I have this code:

<?php
$str = $_SERVER['REQUEST_URI'];
if (eregi('\route=checkout/cart', $str)) {
echo "***********";
}
else
{
echo"";
}
?>

end code.


When ever I try to insert this code where the stars are now I get an unexpected < error,


<script src="//s3.amazonaws.com/searchdiscovery-satellite-production/1234567897.js"></script>

Any ideas??

Thanks
0
Comment
Question by:tonypearce
  • 4
  • 2
  • 2
  • +2
12 Comments
 
LVL 29

Expert Comment

by:Randy Downs
ID: 39234711
If you are inserting into an existing  block of php code you don't need these tags <?php ?>

In other words something like this

<?php 
some code

<?php 
more code
?>

?>

Open in new window

0
 
LVL 29

Expert Comment

by:Randy Downs
ID: 39234721
This is how you would insert the code in the above scenario


<?php
some code


$str = $_SERVER['REQUEST_URI'];
if (eregi('\route=checkout/cart', $str)) {
echo "***********";
}
else
{
echo"";
}


?>
0
 
LVL 12

Expert Comment

by:adrian_brooks
ID: 39234724
First of all, the use of eregi has been deprecated since PHP 5.3 and its continued use is highly discouraged. http://us2.php.net/manual/en/function.eregi.php

I would consider the use of preg_match as a replacement to the use of eregi.

Secondly, your supplied code sample doesn't provide enough detail to effectively help you debug the issue.

~AB
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 29

Expert Comment

by:Randy Downs
ID: 39234744
After re-reading this is probably what you need:

http://php.about.com/od/learnphp/qt/php_with_html.htm

<?php
 //your php code here
 ?>

 <b>Here is some more HTML</b>

 <?php
 //more php code
 ?>

 
 </body>
 </html>

Open in new window

0
 

Author Comment

by:tonypearce
ID: 39234760
Hi,

Sorry I have made myself very unclear..

This is what I'm trying to do and the code as I have tried:

<?php
$str = $_SERVER['REQUEST_URI'];
if (eregi('\route=checkout/cart', $str)) {
echo "<script src="//s3.amazonaws.com/searchdiscovery-satellite-production/1234567897.js"></script>";
}
else
{
echo"";
}
?>

This code is supposed to show some code if a certain url is used,
0
 
LVL 29

Expert Comment

by:Randy Downs
ID: 39234787
Try this:

<?php
$str = $_SERVER['REQUEST_URI'];
if (eregi('\route=checkout/cart', $str)) {
?>
<script src="//s3.amazonaws.com/searchdiscovery-satellite-production/1234567897.js"></script>

<?php
}
else
{
echo"";
}
?>
0
 
LVL 12

Expert Comment

by:adrian_brooks
ID: 39234795
Then what you actually want would be this;

<?php
$str = $_SERVER['REQUEST_URI'];
if (eregi('\route=checkout/cart', $str)) {
echo "<script src=\"http://s3.amazonaws.com/searchdiscovery-satellite-production/1234567897.js\"></script>";
}
else
{
echo"";
}
?>

Open in new window


Please take note of the escaped double quotes in my code revision. \"

This is required, because without that, PHP will see your string end at the next unescaped double quote.

~AB
0
 

Author Comment

by:tonypearce
ID: 39234936
Hi,

Thanks, but still getting this error:
Parse error: syntax error, unexpected '<' in **************

any ideas?
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39234952
Avoid propagating any use of ereg() functions.

Please use view-source on these links.
see http://www.laprbass.com/RAY_temp_tonypearce.php 
and http://www.laprbass.com/RAY_temp_tonypearce.php?route=checkout/cart

<?php // RAY_temp_tonypearce.php
error_reporting(E_ALL);

// CREATE A REGULAR EXPRESSION
$rgx 
= '#'                     // REGEX DELIMITER
. 'route=checkout/cart'   // LITERAL STRING
. '#'                     // REGEX DELIMITER
;

// IF AND ONLY IF THE EXPRESSION MATCHES THE URL
if (preg_match($rgx, $_SERVER['REQUEST_URI'])) 
{
    echo "<script src=\"http://s3.amazonaws.com/searchdiscovery-satellite-production/1234567897.js\"></script>";
}

Open in new window

HTH, ~Ray
0
 
LVL 82

Expert Comment

by:hielo
ID: 39236665
Try:
<?php
$str = $_SERVER['REQUEST_URI'];
if (preg_match('#\broute=checkout/cart#', $str))
{
	echo "<script src='//s3.amazonaws.com/searchdiscovery-satellite-production/1234567897.js' type='text/javascript'></script>";
}
else
{
	echo "";
}
?>

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39237440
@hielo: What do you get if you run this script ;-)
echo "";

Open in new window

Just kidding, ~Ray
0
 
LVL 82

Expert Comment

by:hielo
ID: 39238136
Well Mr. Paseur, if you can't figure that out a refresher may be necessary.

Allow me to refer you to:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_23681009.html#a22325869

:)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question