trying to get PHP to post string to a file - php

I'm trying to use PHP to send the values of a form, in the format of a list item which contains an image and a caption to be added to a textfile which is then read by another page. The image upload part works fine but the output of the php looks like
<li><img alt= " " src=" " > </li>
when it should be more like <li><img src="img/comics/sa005.png">Comic 5 - </li>
<?php
//get item details
$title = $_POST['title'];
$alt = $_POST['alt'];
$caption = $_POST['caption'];
$location = $_POST['location'];
//file location
$file = 'listitems.txt';
// The list item to add to the file
$string = ("\n <li>" . $title . "<img alt= \" " . $alt . " \" src=\" " . $location . " \" > " . $caption . "</li>");
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $string, FILE_APPEND | LOCK_EX);
echo "string = ";
echo $string;
?>
<form action="appendlist.php" method="post">
<input name="title" type="textbox" placeholder="Title" class="form-control" ><br><br>
<input name="alt" type="textbox" placeholder="Alt text" class="form-control" ><br><br>
<input name="caption" type="textbox" placeholder="Image caption" class="form-control" ><br><br>
<input name="location" type="textbox" placeholder="File location" class="form-control" >
<br>
<input type="submit" value="Add Image" name="submit">
</form>

Your code is also right. but you should have remove extra blank space. if that is not work then another problem.

instead of trying to escape the double quotes, try to just use single quotes where you need a string encapsulated
$string = ("\n <li>" . $title . "<img alt='" . $alt . "' src='" . $location . "' > " . $caption . "</li>");

Related

PHP/HTML to populate a text input with a value from database on load, but write the new value to DB without appearing in URL?

I have two little bits of code doing the work here:
HTML:
<form action="http://mypage.php">
<input type="text" name="AnswerArea" ID="AnswerArea" value="' . $this_answer . '" />
<input type="hidden" name="Q" value="' . $NextQ . '" />
<input type="hidden" name="P" value="' . $quest . '" />
<input type="hidden" name="ref" value="' . $ref . '" /><br><br>
<input class="button" name="submit" type="submit" value="Next" />
</form>
And the PHP:
<?php if ($AStyle != 'None'){
if(array_key_exists('AnswerArea', $_GET)) {
include 'connection.php';
$newanswer = $_GET['AnswerArea'];
$newSQL = 'UPDATE answers SET Q' . $prev . '="' . $newanswer . '" WHERE strRef="' . $ref . '"';
if (mysqli_query($mysqli, $newSQL)) {
echo 'Successful, comment out when ready';
}
mysqli_close($mysqli);
}
}
?>
The intention is that when submit is clicked the Q, P & ref values are in the URL and the new value of $this_answer is written to the database. However, the input submit and AnswerArea variables are appearing in there as well. I know you can't have GET and POST in the same form, so how should I go about doing this?
The AnswerArea variable could be longer than the permissible URL length so I want to avoid it getting passed as an address.
Any help gratefully received!

Shopping cart's item will not update quantity

I am making a dummy online store for a university project. Ive run into a problem I can't get my head around. Items can be added/removed from the cart but the quantity will not be updated if someone tries to add an item that already exists in the cart.
The HTML shown is the code that displays the item on the homepage.php that can be added to the cart.
The PHP shown is code that works with the 'add' action - Adding new item to cart, or if it is already current, it should add the quantity selected to the quantity already in the cart. The latter function will not work.
HTML
<div class="laptops"><br />
<img src="<?php echo "img/" . $row["img"] ?>"><br />
<div class="laptopText">
<h3><?php echo $row["brand"] . $row["model"] ?></h3><br />
<p><?php echo $row["cpu_type"] . ", " . $row["cpu_model"] . ", " . $row["OS"] . ", " . $row["RAM"] . ", " . $row["RAM_type"] . ", " . $row["storage"] . ", " . $row["display"] . ", " . $row["gpu"]?></p><br />
<p class="price"><?php echo "£" . $row["price"] ?></p><br />
<div class="itemFormDiv">
<form method="post" action="homepage.php?action=add&id=<?php echo $row["ID"] ?>">
<input class="quantity" name="quantity" type="number" min="1" max="9" value="1">
<input type="hidden" name="hidden_name" value="<?php echo $row["brand"] . $row["model"] ?>">
<input type="hidden" name="hidden_desc" value="<?php echo $row["cpu_type"] . ", " . $row["cpu_model"] . ", " . $row["OS"] . ", " . $row["RAM"] . ", " . $row["RAM_type"] . ", " . $row["storage"] . ", " . $row["display"] . ", " . $row["gpu"]?>">
<input type="hidden" name="hidden_price" value="<?php echo $row["price"] ?>">
<input class="addTooBasket" name="addTooBasket" type="submit" value="Add to Basket">
</form>
</div>
</div>
</div>
(When I add the PHP to stackoverflow it keeps preventing me from submitting the question so here it is on pastebin )
Can anyone work out how to fix this problem?
Set $counter = 0; before foreach, increment it in the end of each step and try something like $_SESSION["shopping_basket"][$counter]['item_quantity'] += intval($_POST["quantity"]);

I want to remove things after .com

Hello I found a way to remove www https but now I need to remove anything after ".com" www.something.com/something/something I want to remove */something/something~ from the URL.
<form method="post" action="">
<label>URL adresa:</label><input type="text" placeholder= "URL" name="url" required /><br>
<label>Titulek odkazu:</label><input type="text" placeholder= "Titulek" name="titulek" required/><br>
<label>Otevření nového okna ANO</label><input type="checkbox" name="choice1" /><br>
<label>Barva odkazu:</label><input type="color" name="color" /><br>
<input type="submit" name="submit" />
</form>
<?php
$zakaz = array('www.', 'https://', 'http://' );
if(isset($_POST['url']) AND isset($_POST['titulek']) AND isset($_POST['color']) AND !isset($_POST['choice1'])){
$url = $_POST['url'];
$titulek = $_POST['titulek'];
$color = $_POST['color'];
echo "<a href='" . $url . "'title ='" . $titulek . "'style=' color:" . $color . "; text-decoration: none ;'>" . str_replace($zakaz, '', $url) . "</a>";
}elseif(isset($_POST['url']) AND isset($_POST['titulek']) AND isset($_POST['color']) AND isset($_POST['choice1'])){
$url = $_POST['url'];
$titulek = $_POST['titulek'];
$color = $_POST['color'];
echo "<a href='" . $url . "'title ='" . $titulek . "' target= '_blank' style=' color:" . $color . "; text-decoration: none ;'>" . str_replace($zakaz,'',$url ) . "</a>";
}
?>
You can use PHP's explode().
You can use:
$website = explode("/",$fullwebsite)[0]; //something.com

Submitted comment edit in textarea then save

SO, I am working on a short script that takes a user's "bug report" and saves it. Showing the comment at the bottom of the page with an Edit button for editing. I want for that button to take the user to another page with a textarea, their comment would be editable here and a Save button to save their report. I am not sure how to link to the exact comment the user would upload.
Like this:
This is what I have so far:
Bug Reports
<form method="POST" action="Bugs.php">
<p>Bug Name <input type="text" name="name" /><br />
Hardware Type <input type="text" name="hw" /><br />
OS <input type="text" name="os" /> <br />
Frequency of Occurence <input type="text" name="freq" /></p>
<p>Proposed Solutions<br />
<textarea name="sol" rows="6" cols="100"></textarea>
<input type="submit" name="submit" value="Create New Bug Report" /></p>
</form>
<?php
$Dir = "comments";
if (is_dir($Dir)) {
if (isset($_POST['submit'])) {
if (empty($_POST['name'])) {
$String = "Unknown Visitor";
}
else
$String = stripslashes($_POST['name']) . "|";
$String .= stripslashes($_POST['hw']) . "|";
$String .= stripslashes($_POST['os']) . "|";
$String .= stripslashes($_POST['freq']) . "|";
$String .= stripslashes($_POST['sol']);
$CurrentTime = microtime();
$TimeArray = explode(" ", $CurrentTime);
$TimeStamp = (float)$TimeArray[1] + (float)$TimeArray[0];
/* File name is " Comment.seconds.microseconds.txt" */
$SaveFileName = "$Dir/Comment.$TimeStamp.txt";
if (file_put_contents($SaveFileName, $String)>0)
echo "File \"" . htmlentities($SaveFileName) . "\" successfully saved.<br />\n";
else
echo "There was an error writing \"" . htmlentities($SaveFileName) . "\".<br />\n";
}
}
if (is_dir($Dir)) { //show submitted reports on page
$CommentFiles = scandir($Dir);
foreach ($CommentFiles as $FileName) {
if (($FileName != ".") && ($FileName != "..")) {
echo "From <strong>$FileName</strong><br />";
echo "<pre>\n";
$Comment = file_get_contents($Dir . "/" . $FileName);
echo $Comment . "<a href=edit.php>Edit</a>";
echo "</pre>";
echo "<hr />";
}
}
}
?>
You really need to put all this into a database it would be much easier.
However.. when you foreach all the comments in the folder add a $_GET variable with the file name like this:
echo $Comment . "Edit";
Then with edit section you can call urldecode($_GET['com_id']) to access the file name and edit the comment.

Implementing Gravatar into custom Commenting System

I'm in the process of coding my very first blog. With the help of various tutorials, and other forums I have managed to gather a semi-working code.
Right now I have a code that takes and displays the comment, but the problem is I wish to display Gravatars beside each comment. I was just wondering how exactly I would go about implementing the code that they provided on their website.
Here is my current comment form:
<?php
}
$commenttimestamp = strtotime("now");
$sql = "SELECT * FROM php_blog_comments WHERE entry='$id' ORDER BY timestamp";
$result = mysql_query ($sql) or print ("Can't select comments from table php_blog_comments.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$timestamp = date("l F d Y", $row['timestamp']);
printf("<hr />");
print("<p>" . stripslashes($row['comment']) . "</p>");
printf("<p>Comment by %s # %s</p>", stripslashes($row['url']), stripslashes($row['name']), $timestamp);
printf("<hr />");
}
?>
<form method="post" action="process.php">
<p><input type="hidden" name="entry" id="entry" value="<?php echo $id; ?>" />
<input type="hidden" name="timestamp" id="timestamp" value="<?php echo $commenttimestamp; ?>">
<strong><label for="name">Name:</label></strong> <input type="text" name="name" id="name" size="25" /><br />
<strong><label for="email">E-mail:</label></strong> <input type="text" name="email" id="email" size="25" /><br />
<strong><label for="url">URL:</label></strong> <input type="text" name="url" id="url" size="25" value="http://" /><br />
<strong><label for="comment">Comment:</label></strong><br />
<textarea cols="25" rows="5" name="comment" id="comment"></textarea></p>
<p><input type="submit" name="submit_comment" id="submit_comment" value="Add Comment" /></p>
</form>
If you wish for me to post the php that processes each comment here as well just ask below.
My code now:
<?php
function get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array() ) {
$url = 'http://www.gravatar.com/avatar/';
$url .= md5( strtolower( trim( $email ) ) );
$url .= "?s=$s&d=$d&r=$r";
if ( $img ) {
$url = '<img src="' . $url . '"';
foreach ( $atts as $key => $val )
$url .= ' ' . $key . '="' . $val . '"';
$url .= ' />';
}
return $url;
}
}
$commenttimestamp = strtotime("now");
$sql = "SELECT * FROM php_blog_comments WHERE entry='$id' ORDER BY timestamp";
$result = mysql_query ($sql) or print ("Can't select comments from table php_blog_comments.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$timestamp = date("l F d Y", $row['timestamp']);
printf("<hr />");
print("<p>" . stripslashes($row['comment']) . "</p>");
printf("<p>Comment by %s # %s</p>", stripslashes($row['url']), stripslashes($row['name']), $timestamp);
echo $imagetag = "<img src='" . get_gravatar($email) . "' />";
printf("<hr />");
}
?>
You want an image tag whose src comes from the gravatar function.
Something like:
$imagetag = "<img src='" . get_gravatar($email_address) . ' />";
(You'll need to echo this variable where you want it to display.)
The only required parameter of the get_gravatar function is the email address, so just pass this to get_gravatar and you get the url of the gravatar image.
You also may use the Libravatar service that provides a gravatar-compatible but open source and federated alternative.
It has a nice PHP library - Services_Libravatar - that's easy to use:
<?php
require_once 'Services/Libravatar.php';
$sla = new Services_Libravatar();
$imgUrl = $sla->getUrl('foo#example.org');

Categories