I made this simple chat site for my website but I don't know how to make it auto refresh every time a message has been sent.
Site that sends and prints out all messages:
<form action="messages.php" method="POST">
<input name="chat_box" /><br>
<input type="submit" value="Send" />
</form>
<?php
include "messages.txt";
?>
Site that sends text input to a text file:
<?php
$messages = $_POST["chat_box"];
$handler=fopen("messages.txt", 'a');
fwrite($handler,$_SERVER["REMOTE_ADDR"].":".$messages."<br>");
fclose($handler);
header("Location: chat_box.php");
?>
Can anyone help me?
Try this code :
This is a messages.php :
<?php
$page = $_SERVER['PHP_SELF'];
$sec = "10";
?>
<html>
<head>
<meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
</head>
<body>
<form action="messages.php" method="POST">
<input name="chat_box" /><br>
<input type="submit" value="Send" />
</form>
<?php
include "messages.txt"; //Uncomment this to check the autorefresh
echo "Auto refresh in 10 second!";
$messages = $_POST["chat_box"];
$handler=fopen("messages.txt", 'a');
fwrite($handler,$_SERVER["REMOTE_ADDR"].":".$messages."<br>");
fclose($handler);
?>
</body>
</html>
Hope this help you out... :)
If you mean to get new messages, your best bet is probably to re-load the text file every 10 seconds. to do so replace the php in the bottom of your 1st code set with this:
<div id="messages"></div>
<script type="text/javascript">
$(document).ready(function() {
function functionToLoadFile(){
jQuery.get('messages.txt', function(data) {
$("#messages").html(data)
});
}
setInterval(functionToLoadFile, 10000);
});
</script>
Related
I'm trying to make this work but there something wrong:
<?php
if(isset($_POST['submit'])){
echo "<script>location='https://google.com'</script>";
exit();
}
?>
<html>
<head>
<script>
function onSubmit() {
document.getElementById('menu_post').value = 'main';
document.forms['MenuForm'].submit();
}
</script>
</head>
<body>
<a onclick="onSubmit();return false;" href="javascript:void(0)">Test</a>
<form name="MenuForm" method="post" action="index.php">
<input type="hidden" id="menu_post" name="menu_post" value="" />
</form>
</body>
</html>
I tried this one [php on submit redirect to another page
But still doesn't work.
Thanks in advance for any help guys.
It should be something like:
if(isset($_POST['submit'])){
echo "<script>window.location.href='https://google.com'</script>";
exit();
}
Edit
You've actually nothing being posted as 'submit'.
if(isset($_POST['menu_post'])){
echo "<script>window.location.href='https://google.com'</script>";
exit();
}
So I want to replace the form submission with a thank you message after you submit, I need the PHP because this will eventually deal with databases in that php, however right now... The only way it works, is that is Type in a name, press submit. It goes back to a blank form, enter nothing (nothing in address) and submit again and it works...
right now the only way i could think of making it work would be some dummy checkbox where when checked value changes the post is sent. However i don't think that will pass with my groupmates
wondering how i can make it only have to submit once.
Index.PHP
<!DOCTYPE HTML>
<html>
<head>
<?php include_once "thankyou.php"; ?>
<script type="text/javascript" src="jquery-3.1.0.js"></script>
<script type="text/javascript">
$("document").ready(function() {
$("#ContactUs_Submit").click(function(evt) {
<?php $inputName = $_POST["ContactUs_Name"]; ?>
$("#ContactUs_CommentsDiv").replaceWith("<?php
thankyou($inputName); ?>");
return false;
});
});
</script>
</head>
<body>
<div id="ContactUs_CommentsDiv">
<form method="post">
<!-- WITH JQUERY USE SINGLE URL, WITH PAGES-->
<label for="ContactUs_Name">Name: </label>
<input type="text" name="ContactUs_Name" id="ContactUs_Name" />
<br/>
<Label for="ContactUs_Email">Email: </Label>
<input type="email" name="ContactUs_Email" id="ContactUs_Email" />
<br/>
<input id="ContactUs_Submit" type="submit">
</form>
</div>
</body>
</html>
thankyou.php
<?php
function thankyou($name) {
echo "<p> Thank you for your input ";
//if ($_POST["ContactUs_Name"] != "") {
// echo " " . $_POST["ContactUs_Name"];
// }
if ($name != ""){
echo $name;
}
echo "!";
}
?>
Can you not just use AJAX to do this?
My HTML:
<form action="test.php" method="get">
<input type="text" name="text" />
</form>
My PHP:
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<?php
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi, {$text_html}</h1>";
?>
</body>
I want to transport and show data input from type="text" fields in my HTML form, into my PHP file, but the result is as per below:
Hi, {$text_html}"; ?>
Why is the extra code showing?
This is my Source Code.
Assuming you use something (like js) to submit your form.
When you try to output a variable you should use concat of strings.
// this will print the variable name, not is content
echo "<h1>Hi, {$text_html}</h1>";
// Using '.' you can concat strings, so:
echo "<h1>Hi".$text_html."</h1>";
In this way you tell the script that you want the value of $text_html instead of print the string "$text_html"
Hello You need to change in your form code you have to add submit button just. all other code is working fine. Just change your form code with below code.
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" />
</form>
Because you need to transform data from one page to other page either via form submit or you can use Session or Cookie. but currently in your case you just need to add submit button your code work
You will need a submit button. After the submit button is trigerd the if condition will be set to true and the code will execute.
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" />
</form>
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi".$text_html."</h1>";
}
?>
</body>
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" /> // add submit button
</form> ////html page
on php page
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi".$text_html."</h1>";
}
?>
<html>
<head>
<title>Result</title>
<meta charset="UTF-8">
</head>
<body style="background:aqua;">
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<div>Hi,".$text_html."<div>";
}
?>
</body>
The website generates the random number from 1 to 100 when accessing the first page(page1.php). And the user will guess the number.
The first page contains
- a text box for accepting a
number from the user, and a submit button.
The second page(page2.php) will be returned to the user if the guess number is too high or too low. And the page shows a message telling the user "Too High" or "Too Low". The page also contains
a button(retry button) that allows the user to go back to the first page(page1.php) to re-enter a new number
a button that allows the user to quit the game.
The third page(page3.php) is returned to the user if the guess is correct. The page displays "Correct", the random number, and the count of tries.
And I have this index.php which is heart for all the pages. And here is the code.
index.php
<?php
$name = '';
$inputnumber = '';
$random = 33; //this is just an assumption to keep it simple
$message = '';
$guesscount = '';
if (isset($_POST['action'])) {
$action = $_POST['action'];
}
if ($action === 'guess') {
$guesscount = $_POST['$guesscount'];
$inputnumber = $_POST['$inputnumber'];
if ($inputnumber == $random) {
$message = "Correct!";
include 'page3.php';
}
if ($inputnumber > $random) {
$message = "Too High";
include 'page2.php';
}
if ($inputnumber < $random) {
$message = "Too Low";
include 'page2.php';
}
}
if ($action === 'retry') {
include 'page1.php';
}
page1.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Number Guess</title>
</head>
<body>
<h1>Number Guess</h1>
<form name="myForm" action="index.php" method="post" >
Number Guess: <input type="text" name="$inputnumber" value="<?php if(isset($inputnumber)==1){
echo $inputnumber;}else echo ""; ?>" /><br>
<input type="submit" name="action" value="guess" />
<hr>
Guess Count: <?php echo $guesscount; ?>
</form>
</body>
</html>
page2.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Number Guess</title>
</head>
<body>
<h1>Number Guess</h1>
<form name="myForm" action="index.php" method="post" >
Message: <?php echo $message; ?>
<input type="hidden" name="$guesscount" value="<?php echo $guesscount;?>"/><br>
<input type="submit" name="action" value="retry" />
<hr>
Guess Count: <?php echo $guesscount;?>
</form>
</body>
</html>
page3.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Number Guess</title>
</head>
<body>
<h1>Number Guess</h1>
<form name="myForm" action="index.php" method="post" >
Message: <?php echo $message; ?>
Number of Tries: <?php echo $guesscount; ?>
<input type="submit" name="action" value="ok" />
</form>
</body>
</html>
page1.php is the page to load first.
Challenge I have faced is, I couldn't keep the $guesscount stable always. It keeps resetting on me. I have tried session but couldn't resolve it.Please help resolving it.
Thanks in advance.
I don't know why but my gut feeling tells me that the reason why the session is not working for you on other pages is because you do not initiate it ??
So what you have to do is:
index.php
<?php
session_start();
$_SESSION['myVariable'] = 'myVariable';
?>
page1.php
<?php
session_start();
$mySessionVar = $_SESSION['myVariable'];
var_dump($mySessionVar); // <- this should print myVariable
?>
You may get an error saying that $_SESSION is null or not set and to prevent that you can just enclose $_SESSION inside and isset method
if(isset($_SESSION['myVariable']) && $_SESSION['myVariable'] != null) {
$mySessionVar = $_SESSION['myVariable'[;
}
I have the following two script files (i.e., formtest.html and calc.php).
When I do the server side validation on calc.php, how do I pass the error message (i.e.
$err_msg back to the formtest.html?
Thank you
<html>
<head>
<title>Form Test</title>
</head>
<body>
<form method="post" action="calc.php">
<pre>
Loan Amount <input type="text" name="principle" />
<input type="submit" />
</pre>
</form>
</body>
</html>
.
// calc.php
$err_msg = '';
if ( !empty(isset($_POST['principle'])) )
{
// process the form and save to DB
} else {
$err_msg .= 'Loan Amount is empty!';
}
?>
You will either need to use a Server Side Include to bring in the PHP output into the HTML file (if it needs to remain an HTML file), or (a better solution) would be to include it all in one file like so:
(calc.php)
<?php
$err_msg = '';
if (isset($_POST['principle']) && !empty($_POST['principle']) )
{
// process the form and save to DB
} else {
$err_msg .= 'Loan Amount is empty!';
}
?>
<html>
<head>
<title>Form Test</title>
<script type="text/javascript">
<!--
var errMsg = '<?php echo $err_msg; ?>';
// Do something with javascript here.
-->
</script>
</head>
<body>
<div class="error">
<?php
// Or echo it inline with your HTML.
echo $err_msg;
?>
</div>
<form method="post" action="calc.php">
<pre>
Loan Amount <input type="text" name="principle" />
<input type="submit" />
</pre>
</form>
</body>
</html>
Not sure if that's valid as I wrote it off the top of my head. But that's the gist. Hope that made sense. ;)
**Changed code to reflect your comment.*