Solved

PHP embed <script> tag into if else statement

Posted on 2013-06-10
12
342 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
[X]
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
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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 110

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 110

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 …

726 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