PHP post syntax variables [closed] - php

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>

Related

I'm using trumbowyg as text editor for the textareas in my forum, but how do I echo it?

I didn't find any post related to my problem, so here I go :
I added trumbowyg (it's a WYSIWYG editor) to edit the content in my <textarea></textarea>, and it works just fine when I post it in my database.
Only problem is : how do I echo it ?
The parsing method of trumbowyg takes this form : you click on, let's say B in the toolbar on top of the textarea, and it will put your text in bold weight. But in the server, once posted, it takes actually this form : <strong>some text</strong>.
Obviously, when I echo the var stocking the data in this part of my sql request, it output it the same way : <strong>some text</strong> and not some text.
I don't know if it's actually so simple that I can't seem to find the solution, or if I'm trying something impossible... ?
Thanks by advance guys !
Well... Guess it was so obvious that I didn't find the answer in here. If it can help people who find themselves in the same situation as me : just wrap your var containing html with html_entity_decode($var)
That's it.
See below (textarea is showed if the user consulting the profile is the one wich it belongs to, else it just echo the descrption (called in an Action.php file, I didn't put the "requires" before the <!DOCTYPE html> declaration.
<!DOCTYPE html>
<html lang="en">
<?php include "includes/head.php"; ?>
<body>
<?php include ("includes/navbar.php") ?>
<div class="container">
<h2><?= $user_pseudo;?></h2>
<h5><?= $user_access_level;?></h5>
<?php
if($_SESSION['id'] == $user_id){
?>
<form method="POST">
<textarea id="parse" name="description"><?= $user_description; ?></textarea>
<button class="btn btn-primary" name="validate" type="submit">Mettre à jour</button>
</form>
<?php
} else {
?>
<br/><br/><br/>
<div class="container">
<?= html_entity_decode($user_description) ?>
</div>
</div>
<?php
}
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-3.3.1.min.js"><\/script>')</script>
<script src="trumbowyg/dist/trumbowyg.min.js"></script>
<script>
$('#parse').trumbowyg();
</script>
</body>
</html>

PHP - replacing dot into arrows

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

What's the correct method using PHP & HTML [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have created many sites that requires php, html and css. They all work fine but i'm now thinking if all that time i do it in a wrong way.
Usually, first i build the site template and styling and then i code the php.
Then i just include the php functions etc in the HTML.
For example take a look of a table that lists some posts (index.php):
<div class="posts">
<h1 class="content-title">New Posts</h1>
<?php
$rows = list_posts();
while ($row=mysql_fetch_array($rows)) {
echo "
<div>
<img class='post-avatar' src='img/icon_newsletter.jpg'>
<h2 class='post-title'>".$row['post_title']."</h2>
<p class='post-meta'>
Από <a href='#'>".$row['post_user']."</a> | ".$row['post_date']." | <a class='post-views' href='viewpost.php?id=".$row['post_id']."#comments'>Comments: ".total_comments($row['post_id'])."</a>
</p>
<p>
".substr($row['post_body'], 0, 180)."...
</p>
<img class='post-icon-read' src='img/read.png' /> <a href='viewpost.php?id=".$row['post_id']."'>Συνέχεια άρθρου..</a>
</div>
";
}
?>
</div>
There is room for improvement.
Keep the HTML within the PHP. It iss not personal opinion, there are technical reasons.
When you do this:
html
<?php PHP ?>
html
<?php ?>
html
<?php PHP ?>
html
There is overhead within PHP when you switch back and forth between HTML mode and PHP Mode. And it makes you look like a Word Press Hack.
A few things:
- Heredoc syntax
- Numeric Arrays
- escape quotes
Start with the basic template:
echo <<<EOT
// Top of page
EOT;
while ($row=mysql_fetch_array($rows), MYSQL_NUM) {
}
echo <<<EOT
// bottom of page
EOT;
The reason to use MYSQL_NUM is so the column values can be use without concatenation.
Instead of
<a href='#'>".$row['post_user']."</a> | ".$row['post_date']." | <a class='post-views' href='viewpost.php?id=".$row['post_id']."#comments'>Comments: ".total_comments($row['post_id'])."</a>
you can do this:
$comment = total_comments($row[2]) ;
echo "$row[0]$row[1]<a class=\"post-views\" href=\"viewpost.php?id=$row[2]#comments\">Comments: $comment</a>";
The backslash will escape the double quotes within double quotes. Quote marks do not need to be escaped within Heredoc.
Single dimension Numeric arrays can be used within double quotes and within Heredoc.
Top of page:
echo <<<EOT
<!DOCTYPE html>
<html lang="en"><head><title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Edit Rotation Diet</title>
<style type="text/css">
</style></head>
<body><div id="page">
EOT;
Bottom of page:
echo <<<EOT
<script type="text/javascript">//<![CDATA[
//]]>
</script></body></html>
EOT;
Let's say the page content is a table
<body><div id="page"><table>
EOT;
while ($row=mysql_fetch_array($rows), MYSQL_NUM) {
echo <<<EOT
<tr>
<td>$row[0]</td>
<td>$row[1]</td>
<td>$row[2]</td>
<td>$row[3]</td>
</tr>
EOT;
}
echo <<<EOT
</table>
<script type="text/javascript">//<![CDATA[
Indentation of HTML along with PHP is wrong. You should think about how the HTML is going to look when someone views the source. View the source on most any Word Press page.. Looks like crap.
The blank line between the echo and first html gives you a line feed.
When you echo HTML like you did it ends up looking real bad when you view the source, like Word Press.
Personally, I do not like a four space indent. Before you know it your code is hidden beyond the right border. I use two spaces.
I think this is ugly and a waste of vertical space:
if($x = 1)
{
echo '<p>one</p>';
}
else
}
echo '<p>two</p>'
}
I think this is plenty clear. Without having excessive vertical scrolling/
if($x = 1){
echo '<p>one</p>';
}
else{
echo '<p>two</p>
}

echo text alongside a generated value PHP

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>

php in body -- missing something obvious

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>

Categories