PHP: One php file - many pages - php

I'm supposed to make 1 PHP file called "access.php" that contains 11 pages. I have to do this with 1 PHP file. I'm supposed to accomplish this using functions to display each page. The "welcome" page allows a user to select "Administrator" or "User" via radio button. After hitting submit, the corresponding page should appear. Right now the admin/user page functions simply echo "Administrator Page" or "User Page" for testing purposes.
Ideally, a new page should appear that displays one of those centered at the top of the screen. But what's happening right now is the text "Administrator Page/User Page" just appears at the bottom of the welcome screen.
Here is my code:
<?php
#---Functions---
#**PHP Header**
#function phpHeader(){
#print <<< EOF
#<!DOCTYPE html>
#<head>
#</head>
# <body>
#EOF
#}
#**PHP Footer**
#function phpFooter() {
#echo "</body> </html>";
#}
#**Admin Page**
function adminPage(){
#phpHeader();
echo "<center><h1>Administrator Page</h1></center>";
#phpFooter();
}
#**User Page**
function userPage(){
#phpHeader();
echo "<center><h1>User Page</h1></center>";
#phpFooter();
}
#**Welcome Page**
function welcomePage(){
#phpHeader();
echo "<center><h1>Welcome</h1>";
echo "<br><br><br>";
echo "<h3> Select Login Type</h3>";
echo "<br><br><br>";
echo '<form name="access" action="" method="post">';
echo '<input type="radio" name="AccessLevel" value="admin" />Administrator <br />';
echo '<input type="radio" name="AccessLevel" value="user" /> User <br /><br />';
echo '<input type="submit" name="submit" value="Choose Level" />';
echo '</form></center>';
$AccessLevel = $_POST['AccessLevel'];
if (isset($_POST['submit'])) {
if (! isset($AccessLevel)) {
echo '<h2>Select an option </h2>';
}
elseif ($AccessLevel == "admin") {
adminPage();
}
else {
userPage();
}
}
#phpFooter();
}
welcomePage();
?>

Move this entire block at the top of your page, right under your opening <?php tag.
Sidenote: You can use return; or exit; or die();, the choice is yours; I used return;
$AccessLevel = $_POST['AccessLevel'];
if (isset($_POST['submit'])) {
if (! isset($AccessLevel)) {
echo '<h2>Select an option </h2>';
}
elseif ($AccessLevel == "admin") {
echo adminPage();
return;
}
else {
userPage();
return;
}
}
Which will echo only "Administrator Page" or "User Page" at the top of the page and nothing else.

<?php
function adminPage(){
return "<center><h1>Administrator Page</h1></center>";
}
function userPage(){
return "<center><h1>User Page</h1></center>";
}
function welcomePage(){
return '<center><h1>Welcome</h1>
<br><br><br>
<h3> Select Login Type</h3>
<br><br><br>
<form name="access" action="" method="post">
<input type="radio" name="AccessLevel" value="admin" />Administrator <br />
<input type="radio" name="AccessLevel" value="user" /> User <br /><br />
<input type="submit" name="submit" value="Choose Level" />
</form></center>';
}
$AccessLevel = $_POST['AccessLevel'];
if(isset($_POST['submit'])){
if(!isset($AccessLevel)){
echo welcomePage();
}elseif($AccessLevel=="admin"){
echo adminPage();
echo welcomePage(); // if you want to repeat that content at the bottom again
}elseif($AccessLevel=="user"){
echo userPage();
echo welcomePage(); // if you want to repeat that content at the bottom again
}
}else{//no form submitted
echo welcomePage();
}
?>

Related

Is it possible to store variable in input box in HTML or PHP

I would like to create an input box that is only readable and have the title of the book, is it possible that the input box can store a variable?
P.S The one I want to change is the id, now I successfully disabled the input box, but the output is $id instead of the variable that I use the $_GET method.
My code is as follow
<?php
include_once 'header.php';
$id = mysqli_real_escape_string($conn, $_GET['bookid']);
$title = mysqli_real_escape_string($conn, $_GET['title']);
?>
<section class="signup-form">
<h2>Pre-order</h2>
<div class="signup-form-form">
<form action="preorder.inc.php" method="post">
<input type="text" disabled="disabled" name="id" value= $id >
<input type="text" name="uid" placeholder="Username...">
<input type="text" name="BRO" placeholder="BookRegistrationCode...">
<button type="submit" name="submit">Pre-order</button>
</form>
</div>
<?php
// Error messages
if (isset($_GET["error"])) {
if ($_GET["error"] == "emptyinput") {
echo "<p>Fill in all fields!</p>";
}
else if ($_GET["error"] == "wronginfo") {
echo "<p>Wrong information!</p>";
}
else if ($_GET["error"] == "stmtfailed") {
echo "<p>Something went wrong!</p>";
}
else if ($_GET["error"] == "none") {
echo "<p>Success!</p>";
}
}
?>
</section>
Put <?php echo $var; ?> inside the value attribute.
<input type="text" value="<?php echo $var; ?>" />
EDIT
As per #arkascha's comment, you can use alternative php short tags:
<input type="text" value="<?= $var; ?>" />
As per the docs: https://www.php.net/manual/en/language.basic-syntax.phptags.php

Simple guest list. Do not show first added record

I wrote a simple list of guests. The problem is that when you add the first entry in this list is not displayed and each following is added as expected.
Maybe there is a better way to perform this task?
The script is designed to create a simple list of guests. I wonder whether the transmission of using a GET method in this case is appropriate.
<body>
<div style="float:left; vertical-align:top;">
<h2> Guest list </h2>
<?php
if (isset($_GET['my_list']))
{
$list = $_GET['my_list'];
$explode = explode(',', $list);
array_pop($explode);
foreach($explode as $name)
{
echo $name.'<br>';
}
}
?>
</div>
<div style="float:right; vertical-align:top;">
<h2> New entry: </h2>
<form method="GET">
<input type="text" name="new"><br>
<input type="hidden" name="my_list" value="
<?php
if (isset($_GET['my_list']))
{
if (isset($_GET['new']))
{
echo $_GET['new'].','.$_GET['my_list'];
}
else
{
echo '';
}
}
else
{
if (isset($_GET['new']))
{
echo $_GET['new'];
}
else
{
echo '';
}
}
?>">
<input type="submit" name="send" value="Save"><br>
</form>
</div>
</body>

textarea content auto clears whenever i click the form submit button [PHP]

I have a textarea with a form submit button. Whenever I click the submit button, the content on my textarea clears. but i dont want to clear the content of my textarea. here is my code
codepage.php
<?php
$ans = "hello";
if (isset($_POST['textcode'])) {
{
if ($_POST['textcode'] == $ans) {
echo "<div id=errorPlace>proceed to next lesson</div>";
}
else
{
echo "<div id=errorPlace>Error</div>";
}
}
}
?>
<form method="POST" name="validatePHP">
<textarea name="textcode"></textarea>
<input type="submit" class="btnSubmit" title="Submit Code" name="add" value=""></input>
</form>
thanks for the answers! It worked. now i have another question, what if the textarea has already a preloaded text in it and when I type in another text in it and click the submit button, the textarea should have now have the text that i inputted and the preloaded text in the textarea.
here is my updated code
<?php
$ans = "hello";
if (isset($_POST['textcode'])) {
{
if ($_POST['textcode'] == $ans) {
echo "<div id=errorPlace>proceed to next lesson</div>";
}
else
{
echo "<div id=errorPlace>Error</div>";
}
}
}
?>
<form method="POST" name="validatePHP">
<textarea name="textcode"><?php if(isset($_POST['textcode'])) {
echo htmlentities ($_POST['textcode']); }?>hell</textarea>
<input type="submit" class="btnSubmit" title="Submit Code" name="add" value=""></input>
</form>
Try render content after submit
<textarea name="textcode"><?= $_POST['textcode']; ?></textarea>
<textarea name="textcode">
<?php if(isset($_POST['textcode'])) {
echo htmlentities ($_POST['textcode']); }?>
</textarea>
Maybe you could do it with $_SESSION?
at the top of your page type session_start();
then inside
if ($_POST['textcode'] == $ans) {
echo "<div id=errorPlace>proceed to next lesson</div>";
}
add the code $_SESSION['textareaMsg'] = $_POST['textcode']; like this...
if ($_POST['textcode'] == $ans) {
echo "<div id=errorPlace>proceed to next lesson</div>";
$_SESSION['textareaMsg'] = $_POST['textcode'];
}
then where your text area is set just replace it with this.
<?php
if(isset($_SESSION['textareaMsg'])){
echo '<textarea name="textcode">'.$_SESSION['textareaMsg'].'</textarea>';
}else{
echo '<textarea name="textcode"></textarea>';
}
?>
This works by saving the text area as a session variable when you submit the form, and checking if its set when you load the form, if it is then it will replace the contents of the text area with what is set in the session. Hope this helps!
Try following code
<?php
$ans = "hello";
$textcode = ""; //declare a variable without value to avoid undefined error
if (isset($_POST['textcode'])) {
{
$textcode=$_POST['textcode']; //assign the value to variable in you if statment
if ($textcode == $ans) { //useing variable in if statment
echo "<div id=errorPlace>proceed to next lesson</div>";
}
else
{
echo "<div id=errorPlace>Error</div>";
}
}
}
?>
<form method="POST" name="test.php">
<!--echo user input -->
<textarea name="textcode"><?php echo $textcode; ?></textarea>
<input type="submit" class="btnSubmit" title="Submit Code" name="add" value=""></input>
</form>

A simple operation but it giving an error I want to update each id

It is simple but I am not getting values of radio buttons for updation when I press submit.
It is giving an error in foreach statement like :Warning: Invalid argument supplied for foreach() Please say how to get the values of radio button after pressing submit button,
here I am updating the status of state .
<?php
include("db.php");
?>
<html>
<body>
<?php
$state=$_POST['state'];
if(isset($_POST['submit']))
{
$result1 = mysql_query("select state,id,status from states ");
while($rr1=mysql_fetch_array($result1))
{
//getting values of radio buttons
$myval=$rr1['id'];
echo $myval;
$val=$_POST['yes.$rr1["id"]'];
echo $val;
$val1=$val.$myval;
$vall=yes.$rr1['id'];
foreach($vall as $values)
{
echo $values;
$update=mysql_query("UPDATE states SET status='". $values."'
WHERE id='$myval' ");
}
}
}
echo "ok";
?>
<form action="" name="form" id="form" method="post" >
<?php
//session_start();
include("db.php");
$result = mysql_query("select state,id,status from states ");
while($info1=mysql_fetch_assoc( $result))
{
echo $info1['city'];
echo "<br>";
/*echo "<br>";
echo "company Name:".$info1['company_name'];
echo "<br>";
echo "salary:".$info1['maxsalary'];
echo "<br>";
//echo $info1['company_name'];*/
?>
<label><?php echo $info1['state']; ?></label>
<input type="radio" <?php if($info1['status']=="yes"){ echo
"checked='checked'"; } ?> name="yes.<?php echo $info1[ 'id']; ?>[]"
value="yes">Yes
<input type="radio" <?php if($info1['status']=="no"){ echo
"checked='checked'"; } ?> name="yes.<?php echo $info1[ 'id']; ?>[]"
value="no">no
<br/>
<?php } ?>
<br />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
I think you need to write
$vall=yes.$rr1['id'];
to
$vall="yes".$rr1['id'];
Thanks

PHP code is not submitting data from POST

It was working until I tried adding this statement
<?php echo "<input type=\"hidden\" name=\"hidden1\" value=\"$id\">" ?>
I was able to get the indexnum from my form before but when I added that line nothing seems to load in the fields.
Here is the full form:
<form name="approveform" method="POST" action="">
<?php echo "<input type=\"hidden\" name=\"hidden1\" value=\"$id\">" ?>
Index Number*: <input type="text" name="IndexNum">
<input type="submit" value="Approve" action="">
</form>
Its getting late and I probably need to just go to sleep but IM SO CLOSE!!! Here is the full code that processes the POST.
if($user_data['permissions'] >= 1)
{
// If users permission is 1 or 2 they get a field for inputting the index # and a button to change the approve field from 0 to 1 may need to make a new field to record who approved it....
//Determine if the order is already approved. If not approved show index field and allow user to approve it with index number
if($data2[0]['Approved'] == 1)
{
echo " <font color=\"green\"> Approved";
}
if($data2[0]['Approved'] == 0)
{
echo " Not Approved. Supply an index number and click approve to authorize this order to be completed.";
if (empty ($_POST) === false)
{
$required_fields = array('IndexNum');
foreach ($_POST as $key=>$value)
{
if (empty($value) && in_array($key, $required_fields) === true)
{
$errors[] = 'Fields marked with an asterisk are required';
break 1;
}
}
if (isset($_POST['success']) === true && empty($_POST['success']) === true)
{
echo 'Index has been updated and Order is now set to Approved';
}
else
{
if (empty($errors) === true)
{
$indexnum=$_POST['IndexNum'];
$approvedby=$user_data['lname'];
$vendorid1= $_POST['hidden1'];
echo $indexnum;
echo $approvedby;
echo $vendorid1;
//update_approved($indexnum, $approvedby, $vendorid1);
//header('Location: index.php');
//exit();
}
else if(empty($errors) === false)
{
echo output_errors($errors);
}
}
}
?>
<form name="approveform" method="POST" action="">
<?php echo "<input type=\"hidden\" name=\"hidden1\" value=\"$id\">" ?>
Index Number*: <input type="text" name="IndexNum">
<input type="submit" value="Approve" action="">
</form>
<?php }
}
Thank you all for looking into this. I get that $id value from a previous POST. I use it elsewhere in the code without issue.
Thank you all so much!
Try like
<input type="hidden" name="hidden1" value="<?php echo $id;?>">
Also try like
<?php echo '<input type="hidden" name="hidden1" value=".$id.">' ?>

Categories