Should I include HTML within my PHP - php

So recently I've been using action="form.php" in my forms. But now I see that you can do without needing an action, since the PHP and the form are in the same file. Which way would be more secure. A PHP file by itself, followed by another file taking care of the form or both combined?
So would I do? Is it safer?
<?php
$name = $_POST['name'];
?>
<form method="post">
<input type="text" name="name">
<input type="submit>
</form>

user <?php echo $_SERVER['PHP_SELF'];?>
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table border="0">
<tr><td> Subject:</td><td> <input type="text" name="subject" /></td></tr>
<tr><td> Message:</td><td> <input type="text" name="message" /></td></tr>
<tr><td> <input type="submit" value="Submit" name="submit"/></td><td><input type="reset" value="Clear" /></td></tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
$email = "example#test.com";
mail($email, $subject, $message);
echo "<center>Email sent</center>";
}
?>

Related

I am trying to use an HTML form to append the results to a Text file using php

Here is my HTML form
<html>
<body>
<h1>HTMl FORM</h1>
<p>
<form action="example.php" method="POST">
<p>Your First name: <input type="text" name="fname" /></p>
<p>Your Last name : <input type="text" name="lname" /></p>
<p><input type="Submit" name="Submit" value="Submit" /> <input type="Reset”/></p>
</form>
</p>
</body>
</html>
PHP file "example.php" Shows the Client what was inputted and should append to the text file "phpfile.txt"
<html>
<body>
<p>
Hi <?php echo htmlspecialchars($_POST['fname']); ?>.<br>
<?php echo htmlspecialchars($_POST['lname']); ?>
</p>
<?php
if(isset($_POST['Submit'])){
$fname = $_POST['fname']."
";
$lname = $_POST['lname']."
";
$file=fopen("phpfile.txt", "a");
fwrite($file, $fname);
fwrite($file, $lname);
fclose($file);
}
?>
</body>
</html>
I am getting the php file to print for the user but it is not appending to the .txt file. I am unsure where I am going wrong
You are probably getting a syntax error.
You also need to use \n to add a new line. You also need to place the echo $_POST parts in the isset function.
<html>
<body>
<h1>HTMl FORM</h1>
<p>
<form action="" method="POST">
<p>Your First name: <input type="text" name="fname" /></p>
<p>Your Last name : <input type="text" name="lname" /></p>
<p><input type="Submit" name="Submit" value="Submit" />
<input type="Reset"/>
</p>
</form>
</p>
<?php
if(isset($_POST['Submit'])){
echo 'Hi '.htmlspecialchars($_POST['fname']);
echo '<br>';
echo htmlspecialchars($_POST['lname']);
$fname = $_POST['fname']."";
$lname = $_POST['lname']."";
$file=fopen("phpfile.txt", "a");
fwrite($file, $fname." ".$lname. "\n");
fclose($file);
}
?>
</p>
</body>
</html>

PHP print variable from input

I have following code and it doesn't works for me.
I need print variable from input.
<html>
<head></head>
<body>
<form action='#' method="post">
<input type="text" name="name">
<input type="submit" name="submit">
</form>
<?php
$name = name
echo $name;
?>
</form>
</body>
</html>
Thanks.
you have to use POST to get text from input.
This is right code:
<html>
<head></head>
<body>
<form action='#' method="post">
<input type="text" name="name">
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit']))
{
$name = $_POST['name'];
echo $name;
}
?>
</form>
</body>
</html>

if (isset($_POST["submit"]))

I am a completely new programmer and I am trying to develop a website in php. All I want to do in this part of the code is to "read" the user's inputs and save them as session variables in order to do/ calculate something else in another subpage. In order to see if everything is working fine I added the lines echo "Welcome ",$_SESSION["firstname"]; echo "ok" but it is not working. Can you help me?
Here is my code:
<!DOCTYPE html>
<html>
<body>
<form method="post" action="">
<b>First Name:</b> <input name="firstname" type="text" value=""></br></br>
<b>Last Name:</b> <input name="lastname" type="text" value=""></br></br>
<b>Age:</b> <input name="age" type="number" min="0" value=""></br></br>
<b>Number of people in household:</b> <input name="numberofpeopleinhousehold" type="number" min="1"value=""></br></br>
</br></br>
<input type="submit" name="submit" value="Submit">
</form
<?php
session_start();
if (isset($_POST["submit"])){
$_SESSION["firstname"]= $_POST["firstname"];
$_SESSION["lastname"]= $_POST["lastname"];
$_SESSION["age"]= $_POST["age"];
$_SESSION["numberofpeopleinhousehold"]= $_POST["numberofpeopleinhousehold"];
}
echo "Welcome ".$_SESSION["firstname"];//here is....
echo "ok"
?>
</body>
</html>
What session_start() does is sends a cookie in the page header when it's served to the browser. If you've already send some data to the browser, like your form, then PHP will not be able to start a session. You need to move session_atart() to the very top of your document, above the form or any echos. Also, you're missing a semi-colon, as noted by others. Also, you need to properly close your <form> tag.
That is actually working. If you look at the source code you will see the your name is echoed out.
One issue is you are missing a greater than sign at the end of your form tag.
</form
should be
</form>
Also, you need to start the session before any html. Try this...
<?php
session_start();
if (isset($_POST["submit"])){
$_SESSION["firstname"]= $_POST["firstname"];
$_SESSION["lastname"]= $_POST["lastname"];
$_SESSION["age"]= $_POST["age"];
$_SESSION["numberofpeopleinhousehold"]= $_POST["numberofpeopleinhousehold"];
}
?>
<!DOCTYPE html>
<html>
<body>
<form method="post" action="">
<b>First Name:</b> <input name="firstname" type="text" value=""></br></br>
<b>Last Name:</b> <input name="lastname" type="text" value=""></br></br>
<b>Age:</b> <input name="age" type="number" min="0" value=""></br></br>
<b>Number of people in household:</b> <input name="numberofpeopleinhousehold" type="number" min="1"value=""></br></br>
</br></br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if ($_SESSION["firstname"] != ""){
echo "Welcome ",$_SESSION["firstname"];
echo "ok";
}
?>
</body>
</html>
echo output inside if, delcare session start on top, tested and works 100%. Note- add php code on top and close form tag
<?php
session_start(); //session start must be first line
if (isset($_POST["submit"])){
$_SESSION["firstname"]= $_POST["firstname"];
$_SESSION["lastname"]= $_POST["lastname"];
$_SESSION["age"]= $_POST["age"];
$_SESSION["numberofpeopleinhousehold"]= $_POST["numberofpeopleinhousehold"];
echo "Welcome ".$_SESSION["firstname"]; //inside if condition
echo "ok";
}
?>
<!DOCTYPE html>
<html>
<body>
<form method="post" action="#">
<b>First Name:</b> <input name="firstname" type="text" value=""/></br></br>
<b>Last Name:</b> <input name="lastname" type="text" value=""/></br></br>
<b>Age:</b> <input name="age" type="number" min="0" value=""/></br></br>
<b>Number of people in household:</b> <input name="numberofpeopleinhousehold" type="number" min="1"value=""/></br></br>
</br></br>
<input type="submit" name="submit" value="Submit"/>
</form> <!-- close form tag -->
</body>
</html>

How to create PHP script

I am trying to create simple HTML form/PHP script from were user can input info and what i want to do is just to appear on screen bellow if it was successful or when you press button reset to give you message it is erased. As i am learning about PHP i am trying to do some project but i can not figure it out.
<title>Login Page</title>
</head>
<body>
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$lastName = $_POST['lastName'];
$submit="Your name and last name is input in!";
echo $submit;
}
if(isset($_POST['reset'])){
echo "Yours data are erased!";
}
?>
<h2>Login Page</h2>
<p>Please input your info in form bellow:</p>
<form name="login" method="post" action="index.php">
<table border="0">
<tr>
<td>Name: </td>
<td><input type="text" name="name" size="20" /></td>
</tr>
<tr>
<td>Last Name: </td>
<td><input type="password" name="lastName" size="20" /></td>
</tr>
</table>
<input type="submit" name="submitButton" value="Submit" />
<input type="reset" name="resetButton" value="Reset" />
</form>
</body>
</html>
PHP Scripts must be processed by the PHP interpreter.
On windows, a common and easy solution is to use Apache through a WAMP setup.
Instructions can be found here
You need to change if(isset($_POST['submit'])){ into if(isset($_POST['submitButton'])){
and change reset in to <input type="submit" name="resetButton" value="Reset" /> and call this also by the name resetButton
If i where you i would check if(isset($_POST['submitButton'])){ for entres because pressing submitButton will echo your message eather.
Checkin for entry
if(isset($_POST['submitButton'])){
$name = $_POST['name'];
$lastName = $_POST['lastName'];
if (!empty($name) && !empty($lastName)) {
$submit="Your name and last name is input in!";
echo $submit;
} else {
echo 'Show this line that there is not entery in $name or $lastName';
}
}

Returning PHP variables data back to an HTML Page

I am fairly new to php and have only written basic post email returns. However what im trying to achive now is to return the data held within a variable back to the webpage in a specific location on the page.
So far I have a form that collects the data Name & Surname, this is collected by php.
$firstnameField = $_POST ['name'];
$surnameField = $_POST ['surname'];
All I want to do is print the data within these variables back to the screen in a specific location on the page. I have looked around but others are talking about databases and ajax, and I havent the slightest about that stuff.
Thanks in advance.
It's pretty simple: just use echo/print inside a <?php ?> code block wherever you want that variable printed in your document:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$firstname = $_POST['name'];
}
?>
<html>
<body>
<?php if (isset($firstname)) { echo "<p>Hello, $firstname</p>"; } ?>
<form method="POST">
Enter a name: <input type="text" name="name" />
</form>
</body>
</html>
<?php
if ((isset($_POST["dataEntered"])) && ($_POST["dataEntered"] == "Yes"))
echo "Hello! " . $_POST["name"] . " " . $_POST["surname"];
else
{
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" name="form1" id="form1">
<table>
<tr>
<td>Name:</td>
<td><input name="name" type="text" id="name" value="" size="32" /></td>
</tr>
<tr>
<td>Surname:</td>
<td><input name="surname" type="text" id="surname" value="" size="32" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Send Form" /></td>
</tr>
</table>
<input type="hidden" name="dataEntered" value="Yes" />
</form>
<?php
}
?>

Categories