My PHP is not PHPing, so made simple test... must be missing something obvious.
<html>
<head>
</head>
<body>
<?php echo '<script language=\"javascript\">confirm("Do you see this?")</script>;'; ?>
</body>
</html>
In code body, I get: confirm("Do you see this?");'; ?>
When I "View Source", I see:
<html>
<head>
</head>
<body>
<?php echo '<script language=\"javascript\">confirm("Do you see this?")</script>;'; ?>
</body>
</html>
what extension has your file? is a webserver running? how are you calling your php script?
make sure it has a .php extension, the webserver is running, the file resides under the webroot directory and you call it via http://localhosti/path/to/file.php
also make sure you don't escape quotation marks when not needed, echo '<script type="text/javascript">…</script>'; should do the job
You should remove the backslashes from the \"javascript\".
<?php echo '<script language="javascript">confirm("Do you see this?")</script>;'; ?>
In PHP you can put strings in single ' or double " quotes. This is quite hard to explain (and/or understand) in a few lines, so here's a few valid ways to write down a string containing quotes:
echo 'This has "double quotes"...';
echo 'This has \'single quotes\'...';
echo "This has \"double quotes\"...";
echo "This has 'single quotes'...";
There are many more subtleties to this, but this should get you started.
Take out the \ from the double quotes and the extra semi colon
<html>
<head>
</head>
<body>
<?php echo '<script language="javascript">confirm("Do you see this?")</script>'; ?>
</body>
</html>
Related
I have this data in my database that i fetched:
As you can see i store an html template in my database.
This is the code i used to output that html:
$php = Blade::compileString($template->content);
dd($php) //i used laravel framework, btw.
Then this is the output:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Exmaple.com</title>
<style>
</style>
</head>
<body>
<div class="account-details">
<b>Account Name</b>: <?php echo e(company.name); ?><br>
<b>Plan</b>: <?php echo e(plan.name); ?><br>
<b>Source</b>: <?php echo e(source.name); ?><br>
<b>Source Type</b>: <?php echo e(source.source_type.name); ?><br>
<br>
</div>
<div class="welcome" style="font-family:Arial;font-size:small">
Hi <?php echo e(user.first_name); ?>,<br>
<br>
There seems to be a problem blah blah blah<br>
<br>
Details of the problem are:<br>
<?php echo e(sourceMessage); ?><br>
<br>
You can check and update the source here:<br>
https://example.com<br>
<br>
<br>
<span style="font-size:12.8px">
Kind regards,<br>
<br>
Test Team<br>
<br>
Email: example.com<br>
Website: example.com<br>
</span>
</div>
</body>
</html>
I want to change the .(dot) into -> so in this example, some of text will output like:
from <?php echo e(company.name); ?> to <?php echo e(company->name); ?>
from <?php echo e(source.name); ?> to <?php echo e(source->name); ?>
So i think if it's in a <?php echo e(whatever); ?> that's the time we check and replace the . with -> ?
I think this can be done by RegEx but I'm not expert on that.
The reason why i wanted to replace it is because i am getting a template from an email service then returns ., so i wanted to replace that with -> because I know PHP reads -> in accessing objects rather than ..
You can use preg_replace to look for pieces of code matching <?php ... e(f) and replace the .s in f with ->:
$html = preg_replace_callback('/(<\?php\s+.*?\be\()([^)]+\))/',
function ($m) {
return "{$m[1]}$" . str_replace('.', '->', $m[2]);
},
$html);
Note we use a callback as it makes it easier to deal with replacing a.b.c with a->b->c. Also, to really look like PHP, you need to add a $ at the beginning of the variable name, which this code does. If you don't want it, just change {$m[1]}$ to {$m[1]}
Demo on 3v4l.org
If you're doing that on a editor like VS code or sublime, you just need to replace the function e usage parts.
CTRL + H
(In sublime or VS code) to open up replace dialog.
Make sure the Regex option is clicked.
This would do the trick;
Search (e\(.+)\.(.+\))
Replace $1->$3
I'm having trouble using the echo function in php. I don't fully understand when double quotes, single quotes, and escaping quotes are necessary. I've tried several variations and I can't seem to get it to show up. I know how to echo basic html, but I've not used classes before, I think that might be the dealbreaker here.
Edit: flagged for being a duplicate, I'm new here, not really sure what to do. But it's not duplicate post, the other post uses elements that I'm not familiar with using, this was just straight html. Thank you.
<?php echo "<div id=\"resume-links\" style='display:flex;float:right;''>
<a class='button button--type-action--size-medium' href='/myaccount' style='margin-right:10px;''>Dashboard →</a>
<a class='button button--type-action--size-medium' href='#popmake-1301'>Get Featured →</a>
</div>
<div id=\"resume-link\" style='clear:both;float:right;padding-top:10px;font-style:italic;'><p><a href='/article-submissions'>Learn More</a></p></div>";?>
You can use
<<<START
htmlHere
START;
And after margin-left you hav 2 ' but must 1
you can wrap single quotes inside double quotes
echo " <div id='resume-id'>qwerty</div> ";
you can wrap double quotes inside single quotes
echo '<div id ="resume-id">qwerty</div>';
you can use escape character when your wrapper quotes are same as inner quotes
echo "<div id =\"resume-id\">qwerty</div>";
or
echo '<div id ="resume-id">qwerty\'s</div>';
output : qwerty's
Use Heredoc syntax.
Example :
$str = <<<EOD
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
</head>
EOD;
Documentation
I'm using this line of php in my main page
echo generateRadioButtons("fbresponse.php", "moRating1", 6);
Which when posting the following on the response file
echo $_POST['moRating1']
It works fine and displays the correct result, but! my question is how would i add text to that so..
Blah blah blah, you rated x question: 'moRating1'
I've tried doing
<html>
<head>
<title>Questions</title>
</head>
<body>
<h1>Survey responses</h1>
<p>How well did you rate it : <?php print $moRating1 ?></p>
</body>
</html>
inside the response file but that just doesnt load anything..
Any help please!
It's probably because this function uses eval() to execute its content (I guess it from lack of PHP tags in your first example).
If it's true, then you should be able to close PHP tag, print HTML and open it again.
?>
<html>
<head>
<title>Questions</title>
</head>
<body>
<h1>Survey responses</h1>
<p>How well did you rate it : <?php print $_POST['moRating1'] ?></p>
</body>
</html>
try doing:
$mRating1 = $_POST['moRating1'];
...
?>
...
<p>How well did you rate it: <?php echo $mRating1?></p>
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I m learning PHP , and I m posting variable from a HTML form
Below is my code:
<html>
<head>
<title>Welcome to PHP Products</title>
</head>
<body>
<?php
$txt1=$_POST["product_form_no"];
$txt2=$_POST["product_form_name"];
$txt3=$_POST["product_form_desc"];
<h1>The Entered Product</h1>
<p>Product No</p>echo $txt1;
<p>Product Name</p>echo $txt2;
<p>Product Desc</p>echo $txt3;
?>
</body>
</html>
I m getting the below error
Parse error: syntax error, unexpected '<' in C:\wamp\www\product_entered_list.php on line 12
Any help would be helpful !!!
<?php
$txt1=$_POST["product_form_no"];
$txt2=$_POST["product_form_name"];
$txt3=$_POST["product_form_desc"];
?> <---missing
Without that closing ?>, you're still in "PHP mode" when the <h1> is reached, and PHP tries to interpret that as PHP code, not HTML.
This means you'll also have to modify your echo lines:
<p>Product No</p><?php echo $txt1; ?>
^^^^^^ ^^^
Try closing php tag before "The Entered Product", after that, open php tags for each echo and close them. Code will be like the following:
<?php
$txt1=$_POST["product_form_no"];
$txt2=$_POST["product_form_name"];
$txt3=$_POST["product_form_desc"];
?>
<h1>The Entered Product</h1>
<p>Product No</p><?php echo $txt1; ?>
<p>Product Name</p><?php echo $txt2; ?>
<p>Product Desc</p><?php echo $txt3; ?>
Tell me if it works.
Try this... you need to use the closing php tag before writing the HTML.
This is true for both your POST variable section as well as where you echo the 3 variable values
<html>
<head>
<title>Welcome to PHP Products</title>
</head>
<body>
<?php
$txt1=$_POST["product_form_no"];
$txt2=$_POST["product_form_name"];
$txt3=$_POST["product_form_desc"];
?>
<h1>The Entered Product</h1>
<p>Product No</p> <?php echo $txt1; ?>
<p>Product Name</p><?php echo $txt2; ?>
<p>Product Desc</p><?php echo $txt3; ?>
</body>
</html>
Your problem is here:
$txt3=$_POST["product_form_desc"];
<h1>The Entered Product</h1>
<p>Product No</p>echo $txt1;
Right in the middle of the PHP you start using HTML. You must first close the PHP tags, or use echo to print out the HTML.
Do either this:
$txt3=$_POST["product_form_desc"];
?>
<h1>The Entered Product</h1>
<p>Product No</p><?= $txt1 ?>
Or this:
$txt3=$_POST["product_form_desc"];
echo"<h1>The Entered Product</h1>";
echo "<p>Product No</p>" . $txt1;
There are a lot of tags missing. Closing one after variable assignment, closing & opening around the echos. I'd suggest going back one step and start with PHP basics.
The issue is you've typed HTML inbetween the <?php ?> tags, also known as a syntax error :))
In future make sure to use PHP's echo function to correctly display output.
echo "<h1>The Entered Product</h1>";
echo "<p>Product No " . $txt1 . "</p>";
echo "<p>Product Name</p>" . $txt2;
echo "<p>Product Desc</p>" . $txt3;
On a side note, displaying data on a page, directly from the form is fine if you're learning and the script is not going to be public. However, if you intend to do any serious coding (i.e. the public will use the script) you may want to read up a-bit on form security.
In your code at line number 6 PHP code begins and at line number 11 HTML code begins, php tags were not closed so that code as well gets interpreted. So "<" at beginning of line number 11 causes FATAL error as it is not recognized PHP code.
To incorporate HTML code in PHP you can either use PHP opening and closing tags to merge HTML and PHP code in one file. OR you can use quotes "" to make html code as string to PHP and echo that.
Below is method 1:
<html>
<head>
<title>Welcome to PHP Products</title>
</head>
<body>
<?php
$txt1=$_POST["product_form_no"];
$txt2=$_POST["product_form_name"];
$txt3=$_POST["product_form_desc"];
echo "
<h1>The Entered Product</h1>
<p>Product No</p>$txt1
<p>Product Name</p>$txt2
<p>Product Desc</p>$txt3";
?>
</body>
</html>
Method 2:
<html>
<head>
<title>Welcome to PHP Products</title>
</head>
<body>
<?php
$txt1=$_POST["product_form_no"];
$txt2=$_POST["product_form_name"];
$txt3=$_POST["product_form_desc"];
?>
<h1>The Entered Product</h1>
<p>Product No</p><?php echo $txt1;?>
<p>Product Name</p><?php echo$txt2;?>
<p>Product Desc</p><?php echo $txt3;?>
</body>
</html>
I am new to PHP. I need to output the following JavaScript with PHP. This is my code:
<html>
<body>
<?php
echo "<script type="text/javascript">";
echo "document.write("Hello World!")";
echo "</script>";
?>
</body>
</html>
But it's showing the error:
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /var/www/html/workbench/person/script.php on line 4
Can anyone please help? I also need some simple tutorials on how to use PHP, HTML and JavaScript for an application.
You should escape the JavaScript string delimiters inside the PHP string. You're using double quotes for both PHP and JavaScript strings. Try like this instead:
<html>
<body>
<?php
// Here, we use single quotes for PHP and double quotes for JavaScript
echo '<script type="text/javascript">';
echo 'document.write("Hello World!")';
echo '</script>';
?>
</body>
</html>
You have to escape quotes on both JavaScript and PHP when the string delimiter are the same as the quotes:
echo "\""; // escape is done using a backslash
echo '\'';
Same in JavaScript:
alert("\""); // escape is done using a backslash
alert(echo '\'');
But because it's very hard to read a string with such escape sequences, it is better to combine single with double quotes, as needed:
echo '"';
echo "'";
The error you get if because you need to escape the quotes (like other answers said).
To avoid that, you can use an alternative syntax for you strings declarations, called "Heredoc"
With this syntax, you can declare a long string, even containing single-quotes and/or double-quotes, whithout having to escape thoses ; it will make your Javascript code easier to write, modify, and understand -- which is always a good thing.
As an example, your code could become :
$str = <<<MY_MARKER
<script type="text/javascript">
document.write("Hello World!");
</script>
MY_MARKER;
echo $str;
Note that with Heredoc syntax (as with string delimited by double-quotes), variables are interpolated.
Another option is to do like this:
<html>
<body>
<?php
//...php code...
?>
<script type="text/javascript">
document.write("Hello World!");
</script>
<?php
//....php code...
?>
</body>
</html>
and if you want to use PHP inside your JavaScript, do like this:
<html>
<body>
<?php
$text = "Hello World!";
?>
<script type="text/javascript">
document.write("<?php echo $text ?>");
</script>
<?php
//....php code...
?>
</body>
</html>
Hope this can help.
An easier way is to use the heredoc syntax of PHP. An example:
<?php
echo <<<EOF
<script type="text/javascript">
document.write("Hello World!");
</script>
EOF;
?>
You need to escape your quotes.
You can do this:
echo "<script type=\"text/javascript\">";
or this:
echo "<script type='text/javascript'>";
or this:
echo '<script type="text/javascript">';
Or just stay out of php
<script type="text/javascript">
You need to escape the double quotes like this:
echo "<script type=\"text/javascript\">";
echo "document.write(\"Hello World!\")";
echo "</script>";
or use single quotes inside the double quotes instead, like this:
echo "<script type='text/javascript'>";
echo "document.write('Hello World!')";
echo "</script>";
or the other way around, like this:
echo '<script type="text/javascript">';
echo 'document.write("Hello World!")';
echo '</script>';
Also, checkout the PHP Manual for more info on Strings.
Also, why would you want to print JavaScript using PHP? I feel like there's something wrong with your design.
The following solution should work quite well for what you are trying to do.
The JavaScript block is placed very late in the document so you don't have to
worry about elements not existing.
You are setting a PHP variable at the top of the script and outputting just
the value of the variable within the JavaScript block.
This way, you don't have to worry about escaping double-quotes or HEREDOCS
(which is the recommended method if you REALLY must go there).
Javascript Embedding Example
<div id="helloContainer"><div>
<script type="text/javascript">
document.getElementById('helloContainer').innerHTML = '<?= $greeting; ?>';
</script>
You want to do this:
<html>
<body>
<?php
print '
<script type="text/javascript">
document.write("Hello World!")
</script>
';
?>
</body>
</html>
instead you could easily do it this way :
<html>
<body>
<script type="text/javascript">
<?php
$myVar = "hello";
?>
document.write("<?php echo $myVar ?>");
</script>
</body>
You are using " instead of ' It is mixing up php syntax with javascript. PHP is going to print javascript with echo function, but it is taking the js codes as wrong php syntax. so try this,
<html>
<body>
<?php
echo "<script type='text/javascript'>";
echo "document.write('Hello World!')";
echo "</script>";
?>
</body>
</html>
<?php
echo '<script type="text/javascript">document.write(\'Hello world\');</script>';
?>
Try This:
<html>
<body>
<?php
echo "<script type="text/javascript">";
echo "document.write("Hello World!");";
echo "</script>";
?>
</body>
</html>