Refresh php script on submit - php

i have a question on a short php script here. I have created a script to edit a txt file, the script works just fine, no problem with it. Im trying to make the script reload automaticaly on submit and im really stack here.
Here is the script:
<?
if($_POST['Submit']){
$open = fopen("../youtubelink.txt","w+");
$text = $_POST['update'];
fwrite($open, $text);
fclose($open);
$file = file("../youtubelink.txt");
foreach($file as $text) {
echo $text."<br />";
}
}else{
$file = file("../youtubelink.txt");
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
foreach($file as $text) {
echo $text;
}
echo "</textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
}
?>
Any help please?

use Ajax and query every x Minutes a script that checks the text file for changes. If the Ajax returns "yes, updates available" reload the page with Javascript.

Related

HTML file input not working in PHP

I'm making a social media website for a project. I have a PHP function that allows the user to upload an image to a post and inside the function is an html form to allow them to select a form.
echo "<form method='post' enctype='multipart/form-data'>";
echo "<input type='file' name='myFile'/>";
echo "<label for='myFile' id='fileLabel'>File input</label>";
echo "<input type='submit' name='submitFileForm' value='Upload' />";
echo "</form>";
When I process the form with this code and print_r($_FILES) is empty but if I echo $_POST['myFile'] it displays the correct file name.
if (isset($_POST['submitFileForm'])) {
print_r($_FILES);
$my_folder = "img/";
if (move_uploaded_file($_FILES['myFile']['tmp_name'], $my_folder . $_FILES['myFile']['name'])) {
echo 'Received file' . $_FILES['myFile']['name'] . ' with size ' . $_FILES['myFile']['size'];
} else {
echo 'Upload failed!';
var_dump($_FILES['myFile']['error']);
}
}
Thank you for the help , I have been trying to debug this for the last 2 hours. Also I forgot to mention, I already tried increasing upload_max_filesize = 100M and input_max_size = 100M and that is not the problem.
You have to change the if condition as following
if (isset($_POST['submitFileForm'])) {
It will be working

Display previous logged in timestamp from txt file

I was asked to do this by my college staffs so kindly help me out with this! I have a php file with a text box and login id is supposed to be entered in it and login button is to be pressed. Once this button is pressed, the login id and timestamp is stored in a txt file. Next time the same login id is used then the timestamp is to be overwritten. I have done this part successfully. Now, i wanna display the timestamp before overwritting it. This is something similar to last seen of whatsapp. How can i display it?
This is my code:
<html>
<head><title>Login Portal</title></head>
<body><center>
<h1>TPF EMPLOYEE LOGIN</h1><hr><br><br>
<?php
session_start();
if(isset($_POST['submit']))
{
$myfile = file_get_contents('data.txt');
$_SESSION['name']=$_POST['id'];
date_default_timezone_set('Asia/Calcutta');
$date = date('Y-m-d H:i:s');
$txt=$_SESSION['name'].",".$date.",\n";
$name = $_SESSION['name'];
if(preg_match("/$name/", $myfile))
{
$results = preg_replace("/$name.*\,/", $txt, $myfile);
file_put_contents('data.txt', $results);
}
else
{
file_put_contents('data.txt', $txt, FILE_APPEND);
}
}
else
{
echo "<form name='login' method='post'>";
echo "Enter your login id : <input type='text' name='id' id='id' /><br><br>";
echo "<input type='submit' name='submit' value='Login' />";
echo "</form>";
}
?>
</center>
</body>
</html>
This is the contents of my txt file:
a,2014-10-05 19:00:40,
b,2014-10-05 19:00:31,
Using the comma after the name as an identifier how do i display the previous timestamp before overwritting it?
Change:
if(preg_match("/$name/", $myfile))
{
to include $matches and alter the regexp, then work with $matches array:
if(preg_match("/$name\,(.*),/", $myfile, $matches))
{
echo 'Previous Login: ' . $matches[1];
Example: http://ideone.com/1diNNd
Hints: use a db, ensure $name is unique...

Updating a File Script Issue

I'm using this code to update a given file;
<?php
if($_POST['submit']){
$open = fopen("textfile.php","w+");
$text = $_POST['update'];
fwrite($open, $text);
fclose($open);
echo "File updated.<br />";
echo "File:<br />";
$file = file("textfile.php");
foreach($file as $text) {
echo $text."<br />";
}
}else{
$file = file("textfile.php");
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
foreach($file as $text) {
echo $text;
}
echo "</textarea>";
echo "<input name=\"submit\" type=\"submit\" value=\"Update\" />\n
</form>";
}
?>
But somehow its showing this error:
Although the file does updated with this code when I submit my text and no error or submission, however the error its showing on the screenshot bugs me, so any way I could remove those errors?
Thanks!
Change line 2 to this:
if(isset($_POST['submit'])){
Change line 15 to this:
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">";
Assuming that your submit button is named submit checking for $_POST['submit'] is unreliable (as not all browsers POST the submit button), you should check for another field name that is posted OR better yet, change to if($_SERVER['REQUEST_METHOD'] == 'POST'){.
Also, $PHP_SELF is assuming you have register globals on (which shouldn't be), you should use $_SERVER['PHP_SELF'] instead.
Joe has it. You want isset(). # (error suppression) is the coward's way out. Be a man and use isset() ;-)

how to add html & javascript after PHP exit

Please Help, nothing i add after the ?> works, i tried to put the code in a echo, but its not working would someone please put together a JSFiddle for me or point me in the right direction, i am fairly new with PHP, Thanks for the help
<?php
$filepath = 'http://www.godsgypsychristianchurch.net/music.json';
$content = file_get_contents($filepath);
$json = json_decode($content, true);
foreach ($json['rows'] as $row)
{
if ($_GET['album'] == $row[doc]['album'])
{
echo "<title>{$row[doc]['album']}</title>";
echo "<table align=\"center\" border=\"0\"><tr><td valign=\"top\" width=\"330\">";
echo "<img src=\"{$row['doc']['artwork']}\" alt=\"my image \" width=\"250\" /><br /><br />";
echo "<div class=\"albuminfo\" id=\"albuminfo\">";
print ' Download entire album.<p>';
echo "<font color=\"#fff\">Album: {$row[doc]['album']}</font><br />";
echo "<font color=\"#fff\">Church: {$row[doc]['church']}</font><br />";
echo "<font color=\"#fff\">Description: {$row[doc]['des']}</font><P><br /><P>";
echo "Tweet<br><br>";
print '<div id="like-button"></div>';
echo "<td valign=\"top\">";
echo "<div class=\"playlist\" id=\"playlist\">";
echo "<ol>";
$songCount = 0;
foreach ($row['doc']['tracks'] as $song) {
++$songCount;
$songUrl = $row['doc']['baseurl'] . urldecode($song['url']);
echo "<li>{$song['name']}<div id=\"download\">Download</li>";
}
echo "</ol>";
echo "<br><div id=\"player\"><audio preload></audio></div>";
echo "</div>";
echo "<P>";
echo "<small>To download a single MP3 at a time:</br><b>Windows OS:</b> hold the ALT button on the keyboard and click the Download button<br><b>Mac OSX:</b> hold the OPTION button on the keyboard and click the Download button<P><BR><b>Controls:</b><br>Play/Pause = spacebar</br>Next track = Right arrow<br>Previous track = Left arrow";
echo '</tr></td></table>';
}
}
exit;
?>
<!----NOTTING SHOWING UP---->
<!-- begin htmlcommentbox.com -->
<div id="HCB_comment_box">HTML Comment Box is loading comments...</div>
<script type="text/javascript" language="javascript" id="hcb"> /*<!--*/ if(!window.hcb_user){hcb_user={};} (function(){s=document.createElement("script");s.setAttribute("type","text/javascript");s.setAttribute("src", "http://www.htmlcommentbox.com/jread?page="+escape((window.hcb_user && hcb_user.PAGE)||(""+window.location)).replace("+","%2B")+"&opts=0&num=10");if (typeof s!="undefined") document.getElementsByTagName("head")[0].appendChild(s);})(); /*-->*/ </script>
<!-- end htmlcommentbox.com -->
Because you are using exit, which terminates the script. Get rid of that and it will continue to output the HTML underneath.
http://php.net/manual/en/function.exit.php
Omit the exit.
Exit will end Php processing.
With php exit the parser stops, and hands back all the content gathered until that point to the web server. Simply delete the exit; row.

my script is not reading *.txt file

I have a form which takes two values. One takes a .txt file, the file at which some links are hard coded and a text field which takes a url. When I press submit it takes that url and checks on every link that is on *.txt file. Hope you understand what I am saying if not then please comment I will clarify it. Now I have problems. My code does not work until the file at which links are, is not at my server. I don't how to handle this problem. I have done my search, I also try mysql but that is not ok for me. My script is this:
Enter your file :<input type="file" name="ufile" />
Enter your site name :<input type="text" name="utext" />
<input type="submit" value="Check" />
Now, my php script is this:
$needle = $_POST['utext'];
$file = $_FILES['ufile'];
$new = file($file, FILE_IGNORE_EMPTY_LINES | FILE_SKIP_EMPTY_LINES);
$new = array_map('trim', $new);
echo 'Total entries are: '.count($new).'<br />';
$found = array();
$notfound = array();
foreach ($new as $check) {
echo "<table border='1'><tr>";
echo "<td>Processing</td> <td>", $check,"</td></tr>";
$a = file_get_contents($check);
if (strpos($a, $needle)) {
echo "<td><font color='green'>Found:</font></td>";
$found[] = $check;
} else {
echo "<td><font color='red'>Not Found</font></td>";
$notfound[] = $check;
}
echo "</tr></table>";
}
echo "Matches ".count($found)."<br />";
echo "Not Matched ".count($notfound);
Is there any reason you never read the documentation about how PHP handles uploads in first place? That would make clear that $_FILES['ufile'] is array, so your code cannot work. If you really want to continue writing code without understanding it first, then replace:
$file = $_FILES['ufile'];
with
$file = $_FILES['ufile']['tmp_name'];

Categories