Basic Form using HTML and Php - php

I am trying to create just a basic form using Php and HTML. When I type into the fields and click submit nothing appears on the page except for "Hi,"
This is what I have for the HTML document
<!DOCTYPE html>
<html>
<head>
<title>Php Form</title>
</head>
<body>
<form method="post" action="PHPD1.php">
<label> First Name:<input name="FName" type="text"/></label>
<br>
<label> Last Name:<input name="LName" type="text"/></label>
<br>
<label>Message: <textarea name="message">
</textarea>
</label>
<br>
<input name="submit" type="submit" value="Submit"/>
</form>
</body>
</html>`
and for the php document I have
<html>
<head>
<title>Deliverable 1</title>
</head>
<?php
// Capture the values posted to this php program from the text fields
$FName = $_POST['FName'] ;
$LName = $_POST['LName'] ;
$Message = $_POST['Message'] ;
?>
<body>
Hi, <?php print $FName; ?>
<br>
<?php print $Message; ?>
</body>
</html>

<textarea name="message">
</textarea>
the name given for textarea is "message". so the same name should be used in the php page also to access the value.
instead of
$Message = $_POST['Message'] ;
use
$Message = $_POST['message'] ;
you have done a small mistake.
just check now.

Related

PHP Won't Echo out anything with $_POST

I have 2 files, index.html, and welcome.php.
index.html:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
And here is the PHP:
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
Echo seems to work whenever i'm not using $_POST, with just regular text or variables. Is there something I'm doing wrong? I am also writing this code on repl.it, could that have something to do with it? Major newbie here.

PHP sessions not working on multiple page form when going back to the first page

I don't get the PHP $_SESSION working when I try to echo the result on the first page. The error message states basically that the variables are undefined.
The path i want to use:
1st page = form 1
2nd page = form 2
Go back to 1st page = form 1 with all input filled from previous submit + all data from the 2 forms in a text.
Is that possible ?
Page 1 = index.php:
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<form action="overview.php" id="regForm" method="post">
<div class="tab">
<h2>1. Your info</h2>
<p><input type="text" placeholder="Prénom" name="fname" value="<?php echo htmlspecialchars($_SESSION['name']); ?>"></p>
<p><input type="text" placeholder="Nom" name="name" value="<?php echo htmlspecialchars($_SESSION['name']); ?>"></p>
<input type="submit" value="Valider">
</div>
</form>
<?php echo "Your name is :",$_SESSION['name'], "and your first-name is ", $_SESSION['fname'];?>
<?php echo "Your e-mail is :", $_SESSION['email'] ;?>
</body>
</html>
Page 2 = overview.php:
<?php session_start(); ?>
<?php
$_SESSION['fname'] = $_POST['fname'];
$_SESSION['name'] = $_POST['name'];
?>
<form action="index.php" id="regForm" method="post">
<h2>1. Tes informations personnelles</h2>
<p><input type="text" placeholder="e-mail" name="email"></p>
<input type="submit" value="Valider">
</form>
Back to Page 1 = index.php:
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<form action="overview.php" id="regForm" method="post">
<div class="tab">
<h2>1. Your info</h2>
<p><input type="text" placeholder="Prénom" name="fname" value="<?php echo htmlspecialchars($_SESSION['name']); ?>"></p>
<p><input type="text" placeholder="Nom" name="name" value="<?php echo htmlspecialchars($_SESSION['name']); ?>"></p>
<input type="submit" value="Valider">
</div>
</form>
<?php echo "Your name is :",$_SESSION['name'], "and your first-name is ", $_SESSION['fname'];?>
<?php echo "Your e-mail is :", $_SESSION['email'] ;?>
</body>
</html>
Do you guys see any issue that prevents the code to run ?
Just found out the "WHY".
I basically blocked cookies when I started the project which prevent $_sessions to work properly.
So make guys sure that cookies are enabled on your website. The kind of things that waste an hour of work ...

How to handle multiple php forms using one php page

I'm a student and I know nothing about PHP. but I have to do one of my assignment using PHP.
Here is the problem which I faced.
On my index page, there are 3 links that direct to 3 different forms. when the user chooses one form, then fill and submit it result.php file shows the output using values that the user enters in the form.
all the 3 forms should germinate its result using the same result.php file.
I cannot figure out how to generate the result page by identifying which form the user selects.
Here is my code,
form1.php
<!DOCTYPE html>
<html>
<head>
<title>PHP form handling</title>
</head>
<body>
<form name="form1" action="result.php" method="post">
<label for="pullDownMenu">Title</label>
<select name="pullDownMenu" id="pullDownMenu" size="1">
<option value="Mr">Mr</option>
<option value="Ms">Ms</option>
<option value="Mrs">Mrs</option>
<option value="Rev">Rev</option>
</select>
<p>Name: <input type="text" name="firstname" value="" /></p>
<p>Reg No: <input type="text" name="lastname" value="" /></p>
<p>Email Addr: <input type="text" name="Email" value="" /></p>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
form2.php
<!DOCTYPE html>
<html>
<head>
<title>form 2</title>
</head>
<body>
<form name="form2" action="result.php" method="post">
<p>Registrationa no: <input type="text" name="RegNO" value="" /></p>
<p>NIC number <input type="text" name="NIC" value=""></p>
<p>Telephone number: <input type="text" name="Telephone" value="" /></p>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
I tried the result.php file, but it didn't work. here is the result.php
<!DOCTYPE html>
<html>
<head>
<title>PHP demo</title>
</head>
<body>
<?php
if(!empty($_POST['form1'])){
$title=$_POST['pullDownMenu'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$Email = $_POST['Email'];
echo"<h1>student information</h1>";
echo'title is : ' . $title . '</br>';
echo 'first name is : '. $firstname . '</br>';
echo 'lastname is : '.$lastname;
}
if (!empty($_POST['form2'])) {
$regNo = $_POST['RegNO'];
$NIC = $_POST['NIC'];
$tel = $_POST['Telephone'];
echo "<p>Following details are saved to database</p>";
echo 'reg No\t:\t' . $regNo. '</br>';
echo 'NIC\t:\t' . $NIC. '</br>';
echo 'Tel No\t:\t' . $tel. '</br>';
}
?>
</body>
</html>
Consider using isset() to check for a specific variable. It can be better then checking with empty().
<!DOCTYPE html>
<html>
<head>
<title>PHP demo</title>
</head>
<body>
<?php
if(isset($_POST['form1'])){
echo "<h1>student information</h1>\r\n";
echo "title is : $_POST['pullDownMenu']<br />\r\n";
echo "first name is : $_POST['firstname']<br />\r\n";
echo "lastname is : $_POST['lastname']\r\n";
}
if (isset($_POST['form2'])) {
echo "<p>Following details are saved to database</p>\r\n";
echo "reg No\t:\t$_POST['RegNO']<br />\r\n";
echo "NIC\t:\t$_POST['NIC']<br />\r\n";
echo "Tel No\t:\t$_POST['Telephone']<br />\r\n";
}
?>
</body>
</html>
If you have more forms, consider using switch() instead of if().
You could set different values for button submit for each form. Or you could use a input tag hidden to set type of form. Example:
Form1 : <input type="hidden" name="type" value="form1">
Form2 : <input type="hidden" name="type" value="form2">
And in php form
if($_POST["type"]=="form1")
{
//code here
}else if($_POST["type"]=="form2"){
//code here
}

Invoke Combobox from PHP in HTML

I have a PHP file that parses a JSON list and feeds it to a combobox:
<?php
$jsonData = '{"marco":"marco#test.it", "giovanni":"giovanni#mail.it"}';
$json = json_decode($jsonData, true);
$opts = '';
foreach($json as $name => $email)
{
$opts .= '<option value="'.$email.'">'.$name.'</option>';
}
echo ' <select name="Team1">'.$opts.'</select> <br> ';
echo ' <select name="Team2">'.$opts.'</select> <br>';
?>
I'm trying to include it in an HTML page, so that when it loads it will show the combobox:
<html>
<head>
<title> Invio Mail </title>
</head>
<body>
<form name="mail" action="mailer.php" method="post">
<p>
<center><b> Invio mail </b>
<br>
<br>
<? include("combobox.php") ?><br>
Messaggio: <input type="text" name="name" rows="5" required><br>
<br>
<center><input type="submit" value=" Invia "></center><br>
</form>
</body>
</html>
But I can't manage to make them appear!
I don't need a real time update, it's enough that the list is updated when the page is created.
Also, I can't manage to read the associated value from the combobox.
Make sure that the HTML page has the .php extension instead of .html and check if your webserver supports short open tag for php.
Edit:
You can check the phpinfo() if short_open_tag has the value on.
Edit 2:
You can access the selected value in your mailer.php with $_POST['Team1'] and $_POST['Team2']
Edit:
<? include("combobox.php") ?>
to
<?php include("combobox.php") ?>
So, html code will be:
<html>
<head>
<title> Invio Mail </title>
</head>
<body>
<form name="mail" action="mailer.php" method="post">
<p>
<center><b> Invio mail </b>
<br>
<br>
<?php include("combobox.php") ?><br>
Messaggio: <input type="text" name="name" rows="5" required><br>
<br>
<center><input type="submit" value=" Invia "></center><br>
</form>
</body>
</html>

Printing text box value on page in PHP

I am new to PHP (in-fact I am learning it). I am trying to get the value of textbox defined in HTML and print it on HTML page through PHP code. I am able to send the value of textbox to another page using form post and getting it using $_POST['name'] on other page.
I am not able to print the value of textbox on same page.
Here is my code:
<html>
<head>
<?php
function myfunction()
{
printf($_POST['fname']);
}
?>
</head>
<body>
Name: <input type="text" name="fname" />
<input type="button" onClick="myfunction()" value="Click" />
</body>
</html>
First of all it's not required to write php function within <head> tags. Second,
you can't POST data without <form> tags. try this
<?php
if(isset($_POST['submit']) && $_POST['submit']=='Submit'){
$name=$_POST['fname'];
echo $name;
}
else {
?>
<html>
<head>
</head>
<body>
<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>">
Name: <input type="text" name="fname" />
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
<?php } ?>
you can not call PHP function by button's click event...
if you want to print on same page then you can do by below code..
<html>
<?php
if(count($_POST)>0){
echo $_POST['fname'];
}
?>
<body>
<form method='post'>
Name: <input type="text" name="fname" />
<input type="submit" value="Click" />
</form>
</body>
</html>
hope you will get what you want by my answer..

Categories