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

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);
}
?>

Related

Refreshing Page With Php

I'm working on a php code where I'm echoing out form values and a file onto the page. I have that working fine but I want to implement a start over button when pressed will display the page again without the echoes. Any suggestions or better ways to do this?
<html>
<head>
</head>
<body>
<div class="page">
<h1>File Loader</h1>
<form method="post">
<label>Name:</label><input type="text" name="name"></br>
<label>Date:</label><input type="text" name="date"></br>
<input type="submit" name="submit" value="Submit">
<input type="button" name="startOver" value="Start Over" onclick="phpfile.php"></br>
</form>
</div>
<?php
if ($_POST['submit']) {
$name = $_POST['name'];
$date = $_POST['date'];
echo "Name: $name</br></br>";
echo "Date: $date</br>";
$file_handle = fopen("file.csv", "r");
while (!feof($file_handle) ) {
$fields_of_text = fgetcsv($file_handle, 2024);
$f = $fields_of_text;
$format = "%s %s %s %s %s %s %s %s %s %s %s";
echo "<br>" . sprintf($format, $f[0], $f[1], $f[2], $f[3], $f[4], $f[5], $f[6], $f[7], $f[8], $f[9], $f[10]);
echo "<br>";
echo "<br>";
echo "<br>";
}
fclose($file_handle);
} else {
header("Location: phpfile.php");
}
?>
</div>
</body>
</html>
You can easily use query string/post params.
You can use $_GET like below or you can use a hidden input and change that to 1 or 2 with the onclick and javascript if you need to use post.
</head>
<body>
<div class="page">
<h1>File Loader</h1>
<form method="post">
<label>Name:</label><input type="text" name="name"></br>
<label>Date:</label><input type="text" name="date"></br>
<input type="button" value="Submit" onclick="window.location.href='phpfile.php?SubmitType=1';">
<input type="button" value="Start Over" onclick="window.location.href='phpfile.php?SubmitType=2';">
</br>
</form>
</div>
<?php
if ($_GET['SubmitType'] == '1') {
$name = $_POST['name'];
$date = $_POST['date'];
echo "Name: $name</br></br>";
echo "Date: $date</br>";
$file_handle = fopen("file.csv", "r");
while (!feof($file_handle) ) {
$fields_of_text = fgetcsv($file_handle, 2024);
$f = $fields_of_text;
$format = "%s %s %s %s %s %s %s %s %s %s %s";
echo "<br>" . sprintf($format, $f[0], $f[1], $f[2], $f[3], $f[4], $f[5], $f[6], $f[7], $f[8], $f[9], $f[10]);
echo "<br>";
echo "<br>";
echo "<br>";
}
fclose($file_handle);
}
else {
header("Location: phpfile.php");
exit();
}
?>
</div>
</body>
</html>

fwrite sort by date (latest high up)

I've tried to make a comment system, by using fwrite() code. It works but I want my latest comments to list on top of the list.
Here is my code which works but it doesn't work the way I want. Latest comments appears under the list :((
<?php
if ($_POST){
$name = $_POST['name'];
$content = $_POST['commentcontent'];
$date = date("Y-m-d H:i:s");
$handle = fopen("comments.html","a");
fwrite($handle,"<b>" . $name . "<b> (" .$date . ") </b>" ."</b>:</br>" . $content . "</br>");
fclose($handle);
}
?>
<html>
<body>
<form action="" method="POST">
Comments: <textarea rows="10" cols="30" name="commentcontent"/></textarea></br>
Name: <input type="text" name="name"></br>
<input type="submit" value="post!"></br>
<?php include"comments.html";?>
</form>
</body>
</html>

How to write to file with same name as HTML form field

I have made an attempt to write a PHP code that takes the contents of an HTML form then write them into a file. I have that down just fine, but I have another problem. I want to take an input of a field in the form and make it the file name that I am writing to.
Here is my PHP code:
<?php
if(isset($_POST['forwhom']) && isset($_POST['importance']) && isset($_POST['message'])) {
$file = "students.html";
$data = nl2br('-' . $_POST['forwhom'] . ':' . ' ' . $_POST['message'] . ' ' . $_POST['importance'] . "\n");
$ret = file_put_contents($file, $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "Success! Student added to database.";
}
}
else {
die('no post data to process');
}
?>
Currently, I am writing to the file "students.html" However, I want to write to the file "Dad.html", which happens to be the input in the field by the name of forwhom.
Basically, I am asking this: What do I use to replace "students.html" (line 3) so that the file name will be the same as the input of the field forwhom?
I apologize if that didn't make any sense. Thank you very much!
First check if data has been posted on your form using $_SERVER['REQUEST_METHOD'] == "POST".
For simplicity sake save all your "POST" requests in a variable eg.
$file = $_POST['forwhom'];
$importance = $_POST['importance'];
$message = $_POST['message'];
I sometimes find it much easier using empty() instead of isset().
Create a variable, lets say $status which would store all your messages, then any where in your HTML section you just use the $status to display the appropriate message to the user. Check below how i make use of $status in the form.
By doing so is much cleaner and makes your code more dynamic in a sense.
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$file = $_POST['forwhom'];
$importance = $_POST['importance'];
$message = $_POST['message'];
if (!empty($file) && !empty($importance) && !empty($message)) {
$data = nl2br('-' . $file . ':' . ' ' . $message . ' ' . $importance . "\n");
$file .= ".html";
$ret = file_put_contents($file, $data, FILE_APPEND | LOCK_EX);
if ($ret == true) {
$status = "Success! Student added to database.";
} else {
$status = "Error writing to file!";
}
} else {
$status = "Please enter name, email and message";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="" method="post">
<ul>
<li>
<label for="name">Name: </label>
<input type="text" name="forwhom" id="forwhom">
</li>
<li>
<label for="email">Email: </label>
<input type="text" name="importance" id="importance">
</li>
<li>
<label for="message">Your Message: </label><br>
<textarea name="message" id="message"></textarea>
</li>
<li>
<input type="submit" value="Go!">
</li>
</ul>
<?php if(isset($status)): ?>
<p><?= $status; ?></p>
<?php endif; ?>
</form>
</body>
</html>
I added a form just for explanation sake, hope it helps.

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

PHP: Remove Simple Session with Get-Method

I want to Remove the Sessions from this php code, actually if someone searches i get this url search.php?searchquery=test but if I reload the page, the results are cleaned. how can I remove the Sessions to get the Results still, if someone reloads the page? this are the codes:
search.php
<?php
session_start();
?>
<form method="get" action="querygoogle.php">
<label for="searchquery"><span class="caption">Search this site</span> <input type="text" size="20" maxlength="255" title="Enter your keywords and click the search button" name="searchquery" /></label> <input type="submit" value="Search" />
</form>
<?php
if(!empty($_SESSION['googleresults']))
{
echo $_SESSION['googleresults'];
unset($_SESSION['googleresults']);
}
?>
querygoogle.php
<?php
session_start();
$url = 'http://www.example.com';
$handle = fopen($url, 'rb');
$body = '';
while (!feof($handle)) {
$body .= fread($handle, 8192);
}
fclose($handle);
$json = json_decode($body);
foreach($json->responseData->results as $searchresult)
{
if($searchresult->GsearchResultClass == 'GwebSearch')
{
$formattedresults .= '
<div class="searchresult">
<h3>' . $searchresult->titleNoFormatting . '</h3>
<p class="resultdesc">' . $searchresult->content . '</p>
<p class="resulturl">' . $searchresult->visibleUrl . '</p>
</div>';
}
}
$_SESSION['googleresults'] = $formattedresults;
header("Location: search.php?searchquery=" . $_GET['searchquery']);
exit;
?>
thank you for your help!!
This is against google's terms of use. Use google API

Categories