$outputstring error with html class - php

So I'm not that great at php and I have a basic comment system that I'm trying to implement that calls and writes to a comments.php file.
everything works all well an good until I try and style the $outputstring a bit.
this is the code I have
$outputstring = "<br><p><span class="label">Name:</span> " .$name. "</p><br> <p><span class="label">Comment:</span>" .$message. "</p><br>";
I know whats causing it is the
<span class="label"></span>
but can anyone tell me why?
the script I got was just one off youtube while im experimenting with site building.
the full script is like this.
<?php
$act = $_POST['act'];
if($act == "post") {
$name = $_POST['name'];
$message = $_POST ['message'];
#$fp = fopen("comments.php", 'a');
if (!$fp) {
//The file could not be opened
echo "There was an error! Please try again later!";
exit;
} else {
//The file was successfully opened, lets write the comment to it.
$outputstring = "<br><p><span class="label">Name:</span> " .$name. "</p><br> <p><span class="label">Comment:</span>" .$message. "</p><br>";
//Write to the file
fwrite($fp, $outputstring, strlen($outputstring));
//We are finished writing, close the file for security / memory management purposes
fclose($fp);
//Post the success message
echo "Your post was successfully entered. Click <a href='index.php'>here</a> to continue.";
}
} else {
//We are not trying to post a comment, show the form.
?>
<h3>comments:</h3>
<hr/>
<?php include("comments.php"); ?>
<br><br>
<h3>Post a comment:</h3>
<form action="index.php" method="post">
<label>Name:<label>
<input type="text" name="name" value=""></input>
<br/>
<label>Comment:</label>
<textarea name="messages"></textarea>
<input type="hidden" name="act" value="post"></input>
<br/>
<input type="submit" name="submit" value="Submit"></input>
</form>
<?php
}
?>
If anyone could tell me what I would need to do to be able to add the span with a class that would be swell.
thanks.

$outputstring = "<br><p><span class="label">Name:</span> " .$name. "</p><br> <p><span class="label">Comment:</span>" .$message. "</p><br>";
This line is wrong because you break your line delimiters.
Use simple quotes instead of double quotes for the class attribute :
$outputstring = "<br><p><span class='label'>Name:</span> " .$name. "</p><br> <p><span class='label'>Comment:</span>" .$message. "</p><br>";
Or, as stated by chandresh_cool, escape your double quotes.

You cannot use double quotes inside double quotes until you escape them with backslashes, in this case use single quotes instead like this
$outputstring = "<br><p><span class='label'>Name:</span> " .$name. "</p><br> <p><span class='label'>Comment:</span>" .$message. "</p><br>";

$outputstring = '<br><p><span class="label">Name:</span> ' .$name. '</p><br> <p><span class="label">Comment:</span>' .$message. '</p><br>'; This should solve your problem use '' when you don't have dynamic values.

Related

Something wrong with PHP

I'm trying to make a forum-like section for my website. It's not posting, and I don't know why. Here's my PHP and html
<?php
if ($_POST) {
$title = $_POST['title'];
$name = $_POST['name'];
$content = $_POST['commentContent'];
$handle = fopen("comments.html", "a");
fwrite($handle, "<h2 class='Roboto-Slab'>$title</h2>", "<br>", "<h3
class='Roboto-Slab'>By $name</h3>", "<p class='Roboto-Slab'>$content</p>");
fclose($handle);
}
?>
<form action="" method="POST">
<textarea class="comment-boxmain" rows="20" cols="40" name="commentContent"
placeholder="Start Typing...."></textarea><br>
<input class="comment-boxname" placeholder="Title" type="text"
name="title">
<input class="comment-boxname" placeholder="Your Name" type="text"
name="name">
<input class="comment-btn" type="submit" value="post"><br>
</form>
<?php include "comments.html"; ?>
Please check out the answer at cvmblog.com/forum.php if that will help.
String concatenation is done with dots (.), and not commas (,).
Replace:
fwrite($handle, "<h2 class='Roboto-Slab'>$title</h2>", "<br>", "<h3
class='Roboto-Slab'>By $name</h3>", "<p class='Roboto-Slab'>$content</p>");
With:
fwrite($handle, "<h2 class='Roboto-Slab'>$title</h2>". "<br>". "<h3
class='Roboto-Slab'>By $name</h3>". "<p class='Roboto-Slab'>$content</p>");
And it will work. However, this concatenation is useless. You can do simply:
fwrite($handle, "<h2 class='Roboto-Slab'>$title</h2><br><h3 class='Roboto-Slab'>By $name</h3><p class='Roboto-Slab'>$content</p>");
Also check if comments.html file has CHMOD 777. Furthermore, enable error_reporting on your php.ini file, as the PHP error thrown on this case could guide you to the error line easily.
Here's an implementation of your code secured against stored XSS (the vulnerability that allows people to insert HTML and Javascript code on your page) as well as RCE (remote code execution):
<?php
if ($_SERVER['REQUEST_METHOD']=='POST') {
$title = strip_tags($_POST['title']);
$name = strip_tags($_POST['name']);
$content = nl2br(htmlspecialchars($_POST['commentContent']));
$handle = fopen("comments.html", "a");
fwrite($handle, "<h2 class='Roboto-Slab'>$title</h2><br><h3
class='Roboto-Slab'>By $name</h3><p class='Roboto-Slab'>$content</p>");
fclose($handle);
}
?>
<form action="" method="POST">
<textarea class="comment-boxmain" rows="20" cols="40" name="commentContent"
placeholder="Start Typing...."></textarea><br>
<input class="comment-boxname" placeholder="Title" type="text"
name="title">
<input class="comment-boxname" placeholder="Your Name" type="text"
name="name">
<input class="comment-btn" type="submit" value="post"><br>
</form>
<?php echo file_get_contents("comments.html"); ?>
Also, do some searching about database engines (if you want to still using files, take a look on implementation of flat-files databases, as it's called).

Form not posting - PHP

I want to make this form where people can send a email to me. I've done this before but this time it won't post... I won't get a error message or anything like that. Even my custom made error messages won't show up.
<?php
$err = "<br/>";
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if (!empty($name) && !empty($email) && !empty($message)) {
if (strstr($name, " ")) {
if(strlen($email) >=5 && strstr($email, "#") && strstr($email, ".")) {
} else {
$err = "<p style='color:#b30000'>Fill in a valid email!</p>";
}
} else {
$err = "<p style='color:#b30000'>Fill in a valid name!</p>";
}
} else {
$err = "<p style='color:#b30000'>Fill in all the fields!</p>";
}
} else {$name = ''; $email = ''; $message = '';}
echo $err . "
<form id='form' action='#form' method='post'>
<p>Full Name:</p>
<input class='input' type='text' name='name' placeholder=' Dirk Vondiek' value=". $name . ">
<p>E-Mail:</p>
<input class='input' type='text' name='email' placeholder=' example#domain.com' value=". $email . ">
<p>Your Message:</p>
<textarea class='textarea' name='message' placeholder=' Can you pls add me on steam?'>". $message ."</textarea>
<center><input class='submit' type='submit' name'submit' value='Send'></input></center>
</form>
"
?>
action='#form', it's invalid for an action syntax, so change that to action="" or action="handler.php" if you later decide to use two seperate files.
Edit for above: After testing for myself, that does indeed work, but the major culprit here is that you have a typo here name'submit' and your conditional statement
if (isset($_POST['submit'])) {...} will never happen because of it.
You forgot the equal sign.
So modify it to read as name='submit'.
You also have </input> you can safely get rid of it, it's not a valid closing tag.
And you forgot to close this statement (but in testing this didn't throw me an error and I find that rather a bit bizarre).
</form>
"
?>
So.
</form>
";
?>
That alone with error reporting would have thrown you something about it.
Nota:
However, if you have more code below the </form>" line, then you will get a parse error such as:
(Nota: I added an echo "Testing"; for this example error) but will throw you a parse error related to what you may be using:
Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting ',' or ';'
As you mention in your question that it's meant to send mail with, but that wasn't included in your original post.
If you do have more code below that line and inside the same <?php ...?> tags, then you will need to close off that statement as stated in my answer.
For example.
This will throw a parse error:
</form>
"
mail();
?>
This won't throw an error (valid) so it needs to be closed (mail() is just a fill-in example here).
</form>
";
mail();
?>
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Then the rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.
Please check your form action. You need to mention a URL here like example.php.
In case your PHP and HTML script are in the same page, you can keep it blank.
<form id='form' action='' method='post'>
Hope this helps.
You should change
<form id='form' action='#form' method='post'>
to
<form id='form' action='' method='post'>

How to display message in php without using alert or confirm?

I want to show the changes made in current file after submitting form as message without alert and confirm.How can I do this?
I update language with:
$query="update `account_detail` set `prompt_language`= '$language' WHERE `org_id`='".$_SESSION['account_id']."'";
$result = $mysqli->query($query) or die($mysqli->error);
$Msg="updated language to $language";
function logToFile($filename, $msg)
{
$fd = fopen($filename, "a");
$str = "[" . date("Y/m/d h:i:s", mktime()) . "] " . $msg;
fwrite($fd, $str . "\n");
fclose($fd);
}
logToFile("change.log", "updated language to $language");
header("location:".SITE_URL."index.php?view=view_service");
I put all cheanges made in file.And also want to show as message.
Something along the lines of:
<?PHP
if(isset($_REQUEST['something']))
{
echo('you made some changes...');
}
else
{ ?>
<form method="POST">
<input name="something" type="text" />
<!-- rest of code here... -->
<button type="submit">
</form>
<?PHP
}
?>
The logic being, if the variable has been set (i.e. the form has been submitted) then echo out a message to the page, othewise display the form.
try something like this:
your view page:
<body>
<div id="message"><?PHP
if(isset($_REQUEST['msg']))
{
echo $_REQUEST['msg'];
}
?>
</div>
<form method="POST" action="submit.php">
<input name="language" type="text" />
<!-- rest of code here... -->
<button type="submit" >SUBMIT</button>
</form>
</body>
your submit.php
<?php
if(isset($_POST['language'])){
$msg=array();
$language=$_POST['language'];
$Msg="updated language to $language";
logToFile("change.log", "updated language to $language");
////yor rest pf the code
header("location:view1.php?view=view_service&msg=".$Msg);
}
function logToFile($filename, $msg)
{
$fd = fopen($filename, "a");
$str = "[" . date("Y/m/d h:i:s", mktime()) . "] " . $msg;
fwrite($fd, $str . "\n");
fclose($fd);
}
?>

PHP fopen not working

I just can't figure this out, I keep getting Unable To Create Group.
What I'm attempting to do is you submit a forum, and it creates a file with some info in it. If the group name already exist(the file) then tell them that. Though I keep getting as I stated "Unable To Create Group" as my die message.
This is the PHP part:
<?php
if($_POST['ownername'] && $_POST['groupname']){
$ownername = htmlspecialchars($_POST["ownername"]);
$groupname = htmlspecialchars($_POST["groupname"]);
echo 'Owner is ' . $ownername . ' and the group is ' . $groupname;
$groupfile = '/groups/' . $groupname . '.txt';
if(file_exists($groupfile) == false){
$newgroup = fopen($groupfile, 'w') or die("Unable to create group");
$txt = "Users:" . $ownername;
fwrite($newgroup, $txt);
fclose($myfile);
echo "<font style='color:green'>The group has been created! You may access it <a href='chat.php?group=" . $groupname . "'>here</a></font>";
} else {
echo "<font style='color:red'>The group name is taken, please use another name or wait for it to be released</font>";
}
}
?>
Then this is my HTML part:
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post">
<input type="text" name="ownername" placeholder="Username"/>
<input type="text" name="groupname" placeholder="Group Name"/>
<input type="submit" value="Create"/>
</form>
Any and all help is appreciated, thank you very much!
If the filename doesn't exist yet, use 'x' instead of 'w' to create a file and allow writing into.
$newgroup = fopen($groupfile, 'x');

PHP form error issue

There is probably a simple solution for this but i'm not very proficient in php! Basically I want to submit a form and the user be returned with a thank you overlay image without refresh. I've managed to get this to work BUT now the form validating isn't working properly...
I need to make my overlay only appear after the form validating is successful, if it isn't successful I need to display the error instead of the thank you overlay...
I know I could use ajax for this form but I don't want to rely on javascript!
At the minute the validating is working, but the image is being overlayed on top of it...
This is php code:
<?php
if($_POST['formSubmit'] == "Submit")
{
$errorMessage = "";
if(empty($_POST['formName']))
{
$errorMessage .= "<li>You forgot to enter your name</li>";
}
if(empty($_POST['formTown']))
{
$errorMessage .= "<li>You forgot to enter your town</li>";
}
$varName = $_POST['formName'];
$varTown = $_POST['formTown'];
$varAge = $_POST['formAge'];
$varEmail = $_POST['formEmail'];
$varOne = $_POST['hidden-one'];
$varTwo = $_POST['hidden-two'];
$varThree = $_POST['hidden-three'];
$varFour = $_POST['hidden-four'];
$varFive = $_POST['hidden-five'];
if(empty($errorMessage))
{
$fs = fopen("mydata.csv","a");
fwrite($fs,"\n" . $varName . ", " . $varTown . ", " . $varAge . ", " . $varEmail . ", " . $varOne . $varTwo . $varThree . $varFour . $varFive);
fclose($fs);
}
}
?>
This is my html (with the php code):
<?php
if (isset($_POST['formSubmit'])) {
print "<div class=\"thank-you\"><a href='enter.php'><img src='images/thankyou-overlay.png'/></a></div>\n";
}
?>
<div id="mainContainer">
<p>Just complete your entry details below.</p>
<?php
if(!empty($errorMessage)) {
echo("<p>There was an error with your form:</p>\n");
echo("<ul>" . $errorMessage . "</ul>\n");
}
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" target="_self">
<div class="inputContainer">
<label class="text" name="name">Full Name:</label>
<input type="text" class="box" name="formName" value="<?=$varName;?>">
</div>
... more html inputs...
</form>
Whatever you are going to do, you have to use Javascript. You can choose AJAX either using an iframe where you direct your post to, and reading it in a javascript to check status of posting.
Edit:
Like this you can post it:
<form action="do_stuff.aspx" method="post" target="my_iframe">
<input type="submit" value="Do Stuff!" />
</form>
<!-- when the form is submitted, the server response will appear in this iframe -->
<iframe name="my_iframe" src="not_submitted_yet.aspx"></iframe>
So after the post, you have to read status from this iframe, (in other words de HTML output from it).
First , i am having difficulty comprehending what you are trying to do : But still i can point out a few things that have better alternates ;
You should put this code
if($_POST['formSubmit'] == "Submit")
{
...
}
above the form for the functionality you want
and also the above if should have an else to show the form when there are errors.
like
else
{
---form---
}
try this and c if it helps

Categories