I am trying to make a php program where user will input $name but it will replace with its variable. The code is given bellow
<form method="post" >
<textarea type="text" name="preview"></textarea>
<input type="submit" value="preview"></input>
<form>
<?php
$name="Nahid";
echo $_POST['preview'];
?>
I am expecting an output like: My name is Nahid.
when user will input: My name is $name.
I modified the code to help achieve the stated objective of the program.
I also added some comments to help you understand the code.
I replaced <textarea> with an input field, and I changed the name from 'preview' to 'name' to avoid confusion.
<!DOCTYPE html>
<html>
<head>
<title>Name Preview</title>
</head>
<body>
<form action= "" method="post">
<label>Name: </label>
<input type="text" name="name">
<input type="submit" name="submit" value="PREVIEW"></input>
<form>
</body>
</html>
<?php
// Ensure that form is submitted
if(isset($_POST['submit']))
{
// Ensure that a name is entered
if (isset($_POST['name']) && !empty($_POST['name']))
{
// Store user's input in a variable
$name = htmlentities($_POST['name']);
// Display name
echo "<p>";
echo "My name is " . $name;
echo "</p>";
}
else
{
// Display notification if no name is entered.
echo "Please enter your name";
}
}
?>
Hope that is helpful.
if you enter your name in text area and submit form so you will see entered text
if you enter same value 'Nahid' in text area so you think $name is showing instead of $_POST['preview']
you can concat $name with $_POST['preview'] using dot
test folooowing code:
echo $name . ' <br> ' . $_POST['preview'];
please enter another name in textarea to test it
Related
I want to have an email sending system on my site.
The problem is when I try to assign a variable text from my HTML file it does not happen. I want that what is inside the variable should be written in the message of the email. Here's my code:
<html>
<?php include('form.php'); ?>
<head>
</head>
<body>
<form action="./form.php" method="post">
<div name="name"><input type="text" id="name"></div>
<div name="surname"><input type="text" id="surname"></div>
<div name="message"><textarea rows="4" cols="50" id="message">Inserisci qui il tuo testo.</textarea></div>
<div name="subject"><select id="subject">
<option selected="selected">--Inserisci Oggetto--</option>
<option>Registrazione al sito</option>
<option>Recupero credenziali</option>
</select></div>
<input type="submit" value="Invia penzolini"/>
<input type="hidden" name="button_pressed" value="1" />
</form>
</body>
</html>
<?php
$dochtml = new domDocument();
$dochtml->loadHTML('index.html');
if(isset($_POST['button_pressed']))
{
//prendi nome
$name = $dochtml->getElementById('name');
//prendi cognome
$surname = $dochtml->getElementById('surname');
//prendi l'oggetto della mail
$subject = $dochtml->getElementById('subject');
//msg<70 caratteri
$msg = "Inviato da" . ' ' . $name . $surname . ' ' . $dochtml->getElementById('message'); // /n=spazio
// manda mail
mail("panzersit#gmail.com",$subject,$msg);
echo 'Email inviata.';
}
?>
PHP cannot directly access your DOM. PHP runs only the server and on simple terms takes requests and gives a response.
Upon submit of this form to it's action page ./form.php, the values of the input forms are stored in the $_POST in a key named after it's name attribute. In your HTML code, add name attributes to the input tags like so:
<form action="./form.php" method="post">
<div name="name"><input type="text" name="name"></div>
<div name="surname"><input type="text" name="surname"></div>
</form>
Now if I submit this form and input Zachary for name input tag and Taylor for surname input tag, I can grab these values like so:
in ./form.php file:
$name = $_POST['name'];
// "Zachary"
$surname = $_POST['surname'];
// "Taylor"
To validate if anything was input in the first place, use:
isset($_POST['key']) since SOMETIMES input values with a null value are not even sent to the action page. This prevents PHP from throwing errors if you reference a $_POST key that does not exist.
Looking at documentation ( http://php.net/manual/pt_BR/domdocument.getelementbyid.php ) i'm locate a pontual observation:
"Please note that if your HTML does not contain a doctype declaration, then getElementById will always return null."
Post the contents of form.php will help to recreate the scenario.
To get the posted data from your submitted form, you can do it using $_POST['fieldname']
Just try as below and see what you are getting after form submission.
//echo "<pre>";
//print_r($_POST);
Uncomment above 2 lines, see what you are getting and COMMENT IT AGAIN.
if( isset($_POST['name']) )
{
$name = $_POST['name'];
}
if( isset($_POST['surname']) )
{
$surname = $_POST['surname'];
}
if( isset($_POST['subject']) )
{
$subject = $_POST['subject'];
}
This question already has answers here:
Avoiding form resubmit in php when pressing f5
(11 answers)
Closed 5 years ago.
I've the following PHP code
<html>
<head>
<title>
Test
</title>
</head>
<body>
<form name="test" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<input type="text" name="name">
<br>
<input type="submit" name="submit" value="Submit Form">
<br>
</form>
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "User Has submitted the form and entered this name : <b> $name </b>";
echo "<br>You can use the following form again to enter a new name.";
}
?>
</body>
</html>
that works fine and echos the text that the user writes in the input text box.
The code should also avoid the PHP_SELF exploits (I hope .. )
But if I try to refresh the web page in my Firefox browser an alert appears that tell me
To display this page, Firefox must send information that will repeat
any action (such as a search or order confirmation) that was performed
earlier.
any suggestion on how to avoid this?
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "User Has submitted the form and entered this name : <b> $name </b>";
echo "<br>You can use the following form again to enter a new name.";
echo ("<script> window.location.href='http://YourPatch.com';</script>");
}
<html>
<head>
<title>A BASIC HTML FORM</title>
<?PHP
$username = $_POST['username'];
print ($username);
?>
</head>
<body>
<form name ="form1" method ="POST" action = "allNewPractice.php">
<input type = "text" value ="username">
<input type = "submit" name = "submit1" value = "login">
</form>
</body>
</html>
The name of this file, as you can probably tell, is "allNewPractice.php" and I use
localhost/allNewPractice.php directly through my browser to access it, not through notepad++'s run.
it doesn't work whatsoever; its supposed to print the information I typed into the text box to the page but The page does NOTHING.When I click the login button the page only refreshes, shows the original text box and the login button, but doesn't show what i entered.
I got the tutorial from http://www.homeandlearn.co.uk/php/php4p6.html
what am i doing wrong?
Is there something wrong with my computer?
Learn basic HTML forms:
<input type = "text" value ="username">
Inputs with no name do not submit anything. It should be
<input type="text" name="username" value="somevalue" />
^^^^^^^^^^^^^^^----must be present.
try this. personally i just dont seem to use the post method but it works
<html>
<head>
<title>A BASIC HTML FORM</title>
<?PHP
if ($_GET['username']) {
$user = $_GET['username'];
echo $user;
}else{
echo "no text";
}
?>
</head>
<body>
<form method ="GET" action = "allNewPractice.php">
<input id="username" name="username" type="text" placeholder="username">
<input type = "submit" id = "submit" value = "login">
</form>
</body>
</html>
First, you need to add a name attribute to the input box, as such:
<input type = "text" value ="username" name="username">
When you call $_POST['username'], it is referencing the name attribute, not the value.
Second, you are setting the value of $username before anything has been posted. This will result in an error, undefined variable: username, because $_POST['username'] does not exist when you first load the page. Not until after you submit the form will it have a value. So, you need to check if the form has been submitted first:
if (isset($_POST['submit1'])) {
// Process data from form
}
Here is a complete and working version of your program:
<?php
if (isset($_POST['submit1'])) {
$username = $_POST['username'];
print ($username);
}
?>
<html>
<head>
<title>A BASIC HTML FORM</title>
</head>
<body>
<form name ="form1" method ="POST" action = "allNewPractice.php">
<input type = "text" value ="username" name="username">
<input type = "submit" name = "submit1" value = "login">
</form>
</body>
</html>
I'm new to PHP here and struggling with an issue. I've created a form to submit information to a powershell script on the back end. I have one drop down field (telephone) that is pulling from mysql and populating. That part is working great. The issue that I am having is that when i go to post the data and pass it to the powershell script that field (telephone) is being passed as blank. I'm not sure where I may have caused the issue and looking for some help here.
Here is what I have. I'd like to keep it in PHP if possible.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<?php
// If there was no submit variable passed to the script (i.e. user has visited the page without clicking submit), display the form:
if(!isset($_POST["submit"]))
{
?>
<form name="testForm" id="testForm" action="BTAM2.php" method="post" /> <br />
First Name: <input type="text" name="SAMname" id="SAMname" maxlength="20" /><br /> <br/>
Telephone Number:
<?php
$con=mysqli_connect("localhost","root","","it");
//============== check connection
if(mysqli_errno($con))
{
echo "Can't Connect to mySQL:".mysqli_connect_error();
}
//This creates the drop down box
echo "<select name='phonenum' id='phonenum'>";
echo '<option value="0">'.' '.'</option>';
$query = mysqli_query($con,"Select `btphonenumber` from `btphone` where `btuser` = ' '");
$query_display = mysqli_query($con,"SELECT * FROM btphone");
while($row=mysqli_fetch_array($query))
{
echo "<option value='". $row['id']."'>".$row['btphonenumber']
.'';
}
echo '</select>';
?><br /> <br />
<input type="submit" name="submit" id="submit" value="Create User" />
</form>
<?php
}
// Else if submit was pressed, check if all of the required variables have a value:
elseif((isset($_POST["submit"])) && (!empty($_POST["SAMname"])))
{
// Get the variables submitted by POST in order to pass them to the PowerShell script:
$firstname = $_POST["SAMname"];
$telenumber = $_POST["phonenum"];
var_dump($_REQUEST);
}
// Else the user hit submit without all required fields being filled out:
else
{
echo "Sorry, you did not complete all required fields. Please go back and try again.";
}
?>
I've resolved my own issue. in the line
echo "<option value='". $row['id']."'>".$row['btphonenumber']
I should have had
echo "<option value='". $row['btphonenumber']."'>".$row['btphonenumber']
I have a PHP form that is located on file contact.html.
The form is processed from file processForm.php.
When a user fills out the form and clicks on submit,
processForm.php sends the email and direct the user to - processForm.php
with a message on that page "Success! Your message has been sent."
I do not know much about PHP, but I know that the action that is calling for this is:
// Die with a success message
die("<span class='success'>Success! Your message has been sent.</span>");
How can I keep the message inside the form div without redirecting to the
processForm.php page?
I can post the entire processForm.php if needed, but it is long.
In order to stay on the same page on submit you can leave action empty (action="") into the form tag, or leave it out altogether.
For the message, create a variable ($message = "Success! You entered: ".$input;") and then echo the variable at the place in the page where you want the message to appear with <?php echo $message; ?>.
Like this:
<?php
$message = "";
if(isset($_POST['SubmitButton'])){ //check if form was submitted
$input = $_POST['inputText']; //get input text
$message = "Success! You entered: ".$input;
}
?>
<html>
<body>
<form action="" method="post">
<?php echo $message; ?>
<input type="text" name="inputText"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>
The best way to stay on the same page is to post to the same page:
<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">
There are two ways of doing it:
Submit the form to the same page: Handle the submitted form using PHP script. (This can be done by setting the form action to the current page URL.)
if(isset($_POST['submit'])) {
// Enter the code you want to execute after the form has been submitted
// Display Success or Failure message (if any)
} else {
// Display the Form and the Submit Button
}
Using AJAX Form Submission which is a little more difficult for a beginner than method #1.
You can use the # action in a form action:
<?php
if(isset($_POST['SubmitButton'])){ // Check if form was submitted
$input = $_POST['inputText']; // Get input text
$message = "Success! You entered: " . $input;
}
?>
<html>
<body>
<form action="#" method="post">
<?php echo $message; ?>
<input type="text" name="inputText"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>
Friend. Use this way, There will be no "Undefined variable message" and it will work fine.
<?php
if(isset($_POST['SubmitButton'])){
$price = $_POST["price"];
$qty = $_POST["qty"];
$message = $price*$qty;
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="#" method="post">
<input type="number" name="price"> <br>
<input type="number" name="qty"><br>
<input type="submit" name="SubmitButton">
</form>
<?php echo "The Answer is" .$message; ?>
</body>
</html>
You have to use code similar to this:
echo "<div id='divwithform'>";
if(isset($_POST['submit'])) // if form was submitted (if you came here with form data)
{
echo "Success";
}
else // if form was not submitted (if you came here without form data)
{
echo "<form> ... </form>";
}
echo "</div>";
Code with if like this is typical for many pages, however this is very simplified.
Normally, you have to validate some data in first "if" (check if form fields were not empty etc).
Please visit www.thenewboston.org or phpacademy.org. There are very good PHP video tutorials, including forms.
You can see the following example for the Form action on the same page
<form action="" method="post">
<table border="1px">
<tr><td>Name: <input type="text" name="user_name" ></td></tr>
<tr><td align="right"> <input type="submit" value="submit" name="btn">
</td></tr>
</table>
</form>
<?php
if(isset($_POST['btn'])){
$name=$_POST['user_name'];
echo 'Welcome '. $name;
}
?>
simple just ignore the action attribute and use !empty (not empty) in php.
<form method="post">
<input type="name" name="name">
<input type="submit">
</form>
<?PHP
if(!empty($_POST['name']))
{
echo $_POST['name'];
}
?>
Try this... worked for me
<form action="submit.php" method="post">
<input type="text" name="input">
<input type="submit">
</form>
------ submit.php ------
<?php header("Location: ../index.php"); ?>
I know this is an old question but since it came up as the top answer on Google, it is worth an update.
You do not need to use jQuery or JavaScript to stay on the same page after form submission.
All you need to do is get PHP to return just a status code of 204 (No Content).
That tells the page to stay where it is. Of course, you will probably then want some JavaScript to empty the selected filename.
What I do is I want the page to stay after submit when there are errors...So I want the page to be reloaded :
($_SERVER["PHP_SELF"])
While I include the sript from a seperate file e.g
include_once "test.php";
I also read somewhere that
if(isset($_POST['submit']))
Is a beginners old fasion way of posting a form, and
if ($_SERVER['REQUEST_METHOD'] == 'POST')
Should be used (Not my words, read it somewhere)