a comment in case of field empty php? - php

I have 3 PHP files , the first one contain fields to compile which mean in html a form. the second , the code that took the fields from the first file and register them inside a folder .txt with a title=the date and time , the third file confirm the registration: i did the 3 files they work : but i would like to reload the fields page, in case where I click on the button and one or more of the fields is empty, with a comment near the field empty :
the first file form.php is:
I put the code only with one field to don't disturb more :`
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<title>Titre de la page</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
}
<form action="POST.PHP" method="POST">
<input type="text" name="name" placeholder="name" value="name">
<button type="button">Click Me!</button>
</form>
</body>
</html>
the second file POST.php contain :
<?php
$nameErr= "";
$name = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Missing";
} else {
$name = $_POST["name"];
$filename="file/". date("Y-m-d_H_m_s") . "_" . uniqid() . ".txt" ;
$fp = fopen( $filename, "w" );
fwrite($fp, $name);
fclose($fp);
include 'registred.php';
}
}
?>
and the third file registred.php contain :
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Titre de la page</title>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<h2>completed registration</h2>
<h3>wish to see you soon !!</h3>
<ul>
<li >name:<?=htmlspecialchars($name)?></li>
</ul>
</body>
</html>

This is not the best way to do that but its good enough to give you a idea about how u r gonna manage this.
<?php session_start();?>
<form action="post.php" method="post">
<input type="text" name="txtName">
<?php
if(isset($_SESSION['mising_data'])){
echo $_SESSION['mising_data'];
}
?>
<input type="submit" name="btnSubmit">
</form>
now in post.php file
<?php
session_start();
if (isset($_POST['btnSubmit'])) {
if (empty($_POST['txtName'])) {
$_SESSION['mising_data'] = 'missing Data';
header('Location: form.php');
}
}
?>
to handle this professionally u should use jquery ajax() method

Related

how to redirect to a php file within an html select tag

I am trying to make an inex.php file as a dashboard page that shows a selection of files that are part of the folder that index.php is part of. and when the user selects the file, I want it to redirect to that page. I am using xammp as a local webserver for this.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>index</title>
</head>
<body>
<h1>Admin portal</h1>
<h3>select a php file relevent to a table in the database you would like to manage :index brings you right back :(</h3>
<form action="" method="POST">
<label for="pages">page:</label>
<select name="pages" id="page">
<?php
foreach (glob("*.php") as $filename) {
echo "<option value='strtolower($filename)'>$filename</option>";
}
?>
</select>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
I am using a foreach to get all the files that are in the folder and populate them in a selection menu. I want to know how I can redirect to those files when I hit submit.
Your code is correct, you need only remove strtolower method from value, and use onchange on select tag.
Edit, Second Sol when submitted form :
<form action="" method="POST" onsubmit="return submitForm()">
<label for="pages">page:</label>
<select name="pages" id="page" >
<?php
foreach (glob("*.php") as $filename) {
$filename = strtolower($filename);
echo "<option value='$filename'>$filename</option>";
}
?>
</select>
<br><br>
<input type="submit" id='submitBtn' value="Submit">
</form>
<script type="text/javascript">
function submitForm(){
window.location.href = document.getElementById("page").value;
return false;
}
</script>
First sol,Try it:
<select name="pages" id="page" onchange=" (window.location = this.value);">
<?php
foreach (glob("*.php") as $filename) {
$filename = strtolower($filename);
echo "<option value='$filename'>$filename</option>";
}
?>
</select>
Don't use a select to redirect, use a <a> tag.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>index</title>
</head>
<body>
<h1>Admin portal</h1>
<h3>select a php file relevent to a table in the database you would like to manage :index brings you right back :(</h3>
<h1>pages</h1>
<ul>
<?php
foreach (glob("*.php") as $filename) {
echo "<li>" . $filename . "</li>";
}
?>
</ul>
</body>
</html>
// edit: wrong concat operand

how to create a static page in local directory from php form

I want to create a php form page. Which when submitted then it automatically create a html static page with filled form data in local directory of the web server. tell me please how can i do that.
Since it us tough to write html , meta tag etc every time in textarea ....
So creating a template file and then using it as base , and finally editing only the body and title tag ( you can do more ) , it makes life much easier ...
First we need to create file.php which harnesses all the logic of creating a new file and injecting all the code ( using php methods fopen which creates a new file and file_put_contents which puts all the data inside it ).
Secondly , we create a template file from which all the boilerplate is used up.
(Containing some variable like {BODY} ).
finally we use file.php by filling form and get all required inputs ( getting all inputs and using loop to change all the template variables with inputs entered ).
file.php
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
if (isset($_POST['filename'])) {
$body = $_POST['body'];
$title = $_POST['title'];
$swap_var = array(
"{BODY}" => $body,
"{TITLE}" => $title
);
$template = "template.php";
if (file_exists($template)) {
$html = file_get_contents($template);
} else {
die ("Unable to locate your template file");
}
foreach (array_keys($swap_var) as $key) {
if (strlen($key) > 2 && trim($swap_var[$key]) != '')
$html = str_replace($key, $swap_var[$key], $html);
}
$filename = $_POST['filename'];
if (!file_exists($filename)) {
fopen($filename, "w");
}
file_put_contents($filename , $html);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width">
<title>Creating and adding text in File</title>
<style type="text/css" media="all">
input , textarea {
width: 100% ;
}
body{
padding: 10%;
}
</style>
</head>
<body>
<form action="file.php" method="post" accept-charset="utf-8">
<input type="text" name="filename" id="filename" placeholder="Enter Filename (with extension like .html)" />
<br>
<br>
<input type="text" name="title" id="title" placeholder="title" />
<br>
<br>
<textarea name="body" id="body" rows="8" placeholder="enter body"></textarea>
<br>
<br>
<button type="submit">Create New html file</button>
</form>
</body>
</html>
template.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width , initial-scale=1.0">
<title>{TITLE}</title>
</head>
<body>
{BODY}
</body>
</html>
Make sure to create these 2 files in same directory and note this code will generate all the new files in the same directory as these 2 files , remember you can always change the location of generation of new files .
You can use php file functions like example.
$myfile = fopen("WHATEVER_THE_FILENAME_YOU_WANT.html", "w") or die("Unable to open file!");
$data = """
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
ECHO YOUR FORM DATA HERE.
</body>
</html>
""";
fwrite($myfile, $data);
fclose($myfile);

How to get php variable from an action form and use it in the form thats activating the action

I have two files here as a test the first one which is this below and when I click submit it suppose to do the action on the next page but I want to know how to get retrieve athe $life variable from the action php file and put it in the normal html file
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form class="" action="../logic/profileAction.php" method="post">
<label for=""></label>
<button type="submit" name="button">Submit</button>
</form>
</body>
</html>
Second file which is the php file:
<?php
$life ="Yo";
?>
check this code. you need to run this code in server
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
include_once 'edit.php';
echo $life;
?>
<form class="" action="../logic/profileAction.php" method="post">
<label for=""></label>
<button type="submit" name="button">Submit</button>
</form>
</body>
</html>
and this is your include edit.php
<?php
$life = 'Ok';
?>
then your first file show ok when you run this code

PHP Keep the variable scope even after the page reload

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'[;
}

How to pass the server side validation messages back to client?

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.*

Categories