Tony Pearce
asked on
PHP embed <script> tag into if else statement
Hi,
I have this code:
<?php
$str = $_SERVER['REQUEST_URI'];
if (eregi('\route=checkout/ca rt', $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/se archdiscov ery-satell ite-produc tion/12345 67897.js"> </script>
Any ideas??
Thanks
I have this code:
<?php
$str = $_SERVER['REQUEST_URI'];
if (eregi('\route=checkout/ca
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/se
Any ideas??
Thanks
This is how you would insert the code in the above scenario
<?php
some code
$str = $_SERVER['REQUEST_URI'];
if (eregi('\route=checkout/ca rt', $str)) {
echo "***********";
}
else
{
echo"";
}
?>
<?php
some code
$str = $_SERVER['REQUEST_URI'];
if (eregi('\route=checkout/ca
echo "***********";
}
else
{
echo"";
}
?>
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
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
After re-reading this is probably what you need:
http://php.about.com/od/learnphp/qt/php_with_html.htm
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>
ASKER
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/ca rt', $str)) {
echo "<script src="//s3.amazonaws.com/se archdiscov ery-satell ite-produc tion/12345 67897.js"> </script>" ;
}
else
{
echo"";
}
?>
This code is supposed to show some code if a certain url is used,
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/ca
echo "<script src="//s3.amazonaws.com/se
}
else
{
echo"";
}
?>
This code is supposed to show some code if a certain url is used,
Try this:
<?php
$str = $_SERVER['REQUEST_URI'];
if (eregi('\route=checkout/ca rt', $str)) {
?>
<script src="//s3.amazonaws.com/se archdiscov ery-satell ite-produc tion/12345 67897.js"> </script>
<?php
}
else
{
echo"";
}
?>
<?php
$str = $_SERVER['REQUEST_URI'];
if (eregi('\route=checkout/ca
?>
<script src="//s3.amazonaws.com/se
<?php
}
else
{
echo"";
}
?>
Then what you actually want would be this;
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
<?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"";
}
?>
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
ASKER
Hi,
Thanks, but still getting this error:
Parse error: syntax error, unexpected '<' in **************
any ideas?
Thanks, but still getting this error:
Parse error: syntax error, unexpected '<' in **************
any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 "";
}
?>
@hielo: What do you get if you run this script ;-)
echo "";
Just kidding, ~Ray
Well Mr. Paseur, if you can't figure that out a refresher may be necessary.
Allow me to refer you to:
https://www.experts-exchange.com/questions/23681009/need-a-good-tutorial-to-learn-Object-Oriented-Concepts-in-PHP.html?anchorAnswerId=22325869#a22325869
:)
Allow me to refer you to:
https://www.experts-exchange.com/questions/23681009/need-a-good-tutorial-to-learn-Object-Oriented-Concepts-in-PHP.html?anchorAnswerId=22325869#a22325869
:)
In other words something like this
Open in new window