Display text after submit button is clicked. PHP - php

I am trying to catch some text from the textbox, to save it in a variable and then post it on my browser if I click the submit button. My code is this:
<?php
$var1=$_POST['text1'];
$var2=$_POST['display'];
if(isset($var2)){
var_dump( $var1);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="submit" id="display" name="display"><br /><br />
</body>
</html>
I don't know what is wrong. The code on rextester

For starters you need to implement a form on you HTML page and set the action to a new .php page.
<form action='display.php' method='post'>
*your input fields go here*
Now create a new php page (in this case display.php)
Declare the variables as you did....
$var1=$_POST['text1'];
$var2=$_POST['display'];
if(isset($var2)){
var_dump( $var1);
then you can simply echo each variable accordingly...
Final Result
HTML PAGE:
<form action='result.php' method='post'>
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="submit" id="display" name="display"><br /><br />
</form>
result.php
<?php
$var1 = $_POST['text1']; // create variables of your form elements
$var2 = $_POST['display'];
echo '$var1 . ' ' . $display . '.';
?>
Basically the php page is saying get text1 and display from the form (names) and create variables of them...
Then echo (print) these variables on the screen. (in plain english xD)
p.s
Your rextester isn't working because you haven't specified a form.

You need to setup the FORM tags so they have the correct attributes for POST. Try:
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="submit" id="display" name="display"><br /><br />
</form>
</body>
</html>

You need to use a form, otherwise you aren't posting anything.
Something like:
form.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form method="post" action="yourphpfile.php">
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="text" value="display" name="display" id="display" /><br /><br />
<input type="submit" value="submit"><br /><br />
</form>
</body>
</html>
yourphpfile.php
<?php
if(!empty($_POST['text1']) and !empty($_POST['display'])){
$var1=$_POST['text1'];
$var2=$_POST['display'];
echo "$var1 $var2";
}
?>
Check this complete example

To display text box and button on clicking submit button
<form>
<input type="submit" value="OK" name="btn_name"/><br />
<?php
if(isset($_GET['btn_name']))
{
echo "<input type=text name=txt_userinput />";
echo "<input type=submit value=Area name='btn_calculate'/>";
}
?>
</form>

Related

php, submit 2 buttons in same page

I would to submit 2 buttons in same php page, every button show deferent message on label,
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<title>Test Submit 2 buttons</title>
<body>
<?php
$add_result = "";
if (isset($_POST['addnews']))
{
$add_result = "Hello, add new news";
}
if (isset($_POST['deletenews']))
{
$add_result = "Hello, delete all news";
}
?>
<br /> <br /> <br /> <br /> <br /> <br/>
<form accept-charset="utf-8" method="POST" dir="rtl" style="text-align:center" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label>Enter the news before click on any button::</label>
<br /> <br />
<input type="text" name="news_text" size="100" id="partNumber" required/>
<br /> <br /> <br /> <br />
<input type="submit" name="addnews" id="round" value="Add" />
<br /> <br/> <br />
<input type="submit" name="deletenews" id="round" value="Remove" />
<label for="message">
<?php echo $add_result; ?>
</label>
</form>
<style type="text/css">
input#round{
....
}
</style>
I used PHP_SELF in the form, I want the result show directly after click on any button without change the page
Thanks
You can't do anything with PHP without reloading the page, because each time it needs to be interprrted by server.
In case of 2 submit buttons both of them just submit the form, irrelevant what is the button name, and on the server-side you can't know, what button was pushed.
Either do some tricky js stuff, like changing some hidden input value or form action etc.
Or just keep it simple and use radio boxes

multiple form with single submit button to retrieve value of fields within bith form

index.php
<html>
<head>
<script type="text/javascript">
function submitForms()
{
document.forms["form-1"].submit();
document.forms["form-2"].submit();
}
</script>
</head>
<body>
<form method="POST" action="form.php" id='form-1'>
<input type="text" name="txt1" />
</form>
<form method="POST" action="form.php" id='form-2'>
<input type="text" name="txt2" />
</form>
<input type="button" value="Click Me!" onclick="submitForms();" />
</body>
</html>
form.php
<?php
echo $_POST['txt1'];
echo $_POST['txt2'];
?>
Above is my code and when i submit both forms then both text-fields with their value it does not shoe me both text-field values.It only shoe me second text-field value.Please help me quickly.
I think because you try to get the params after sumbit two forms. You have sent the two forms at once and the second has stepped to the first, so the result is the return of the second form.
I think this will be better:
<html>
<head>
</head>
<body>
<form method="POST" action="form.php">
<input type="text" name="txt1" />
<input type="text" name="txt2" />
<input type="submit" value="Click Me!" />
</form>
</body>
</html>
<?php
echo $_POST['txt1'];
echo $_POST['txt2'];
?>
Sorry for my english

displaying the html entities in php code

How to display the input type elements in the php code in the same page
I am trying this code but it is not working
HTML
<html>
<input type="text" name="keywords" id="keywordsid" value="sample keyword" />
</html>
PHP code
<?php
echo $kewords;
?>
To display html input type fields in the php code what should we do?
index.php file
<html>
<form action="index.php" method="post">
<input type="text" name="keywords" id="keywordsid" value="sample keyword" />
<input typ="submit" value="Submit">
</form>
</html>
<?php
if(isset($_POST['keywords']))
{
$keywords = $_POST['keywords'];
echo $keywords;
}
?>
Use PHP pre defined $_SERVER variable :
$_SERVER['PHP_SELF']
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="keywords" id="keywordsid" value="sample keyword" />
<input type="submit">
</form>
<?php
if(isset( $_POST['keywords']))
{
echo $_POST['keywords'];
}
?>
</body>
</html>
Here, <?php echo $_SERVER['PHP_SELF']; ?> helps you to get result on the same page without sending the request to another page.
First of all without form you dont get input type field name in php.
so, use form and after submmit form you are using $_POST['keywords'] instead of $kewords to echo input field name.
let, your page url is index.php
<html>
<body>
<form action="index.php" method="post">
<input type="text" name="keywords" id="keywordsid" value="sample keyword" />
<input type="submit">
</form>
<?php
if(isset( $_POST['keywords']))
echo $_POST['keywords'];
?>
</body>
</html>
Try something like this
<html>
<form action ="" method="post">
<input type="text" name="keywords" id="keywordsid" value="sample keyword" />
<input type="submit">
</html>
and write php code
<?php
if(isset($_POST['keywords']))
{
echo $_POST['keywords'];
}
?>
This is the way to write php and html
You can use the code bellow to echo the keywords.
<?php
echo '<input type="text" name="keywords" id="keywordsid" value="'.$keywords.'" />';
?>

Beginner: IF statement/PHP/HTML issue

I do apologise in advance for my lack of knowledge in PHP and HTML. I have scoured the internet for 3/4 days trying to create what is probably a simple system. I need the user to enter a digit before the next page is loaded. I have an IF statement on my page.
1.php
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="css/reset.css" rel="stylesheet" type="text/css" />
<div id="container">
<div id="authorise">
<form action="2.php" method="post">
<!--Product Comment Box--><br>
<p>Please enter your 4<br> digit authorisation code:<br> <br>
<input type="text" name="digits" />
<input type="submit" name="submit" />
</form>
</div>
</div>
2.php
<?php
if ($_POST['digits'] === '210392')
{echo 'https://www.facebook.com/'
?>
But I need a form first which the user would input the code '210392' and press submit. Then the if statement can take place. I know how to do a form but dont know how to assign that form a variable name 'digits'. All help is appreciated.
A basic form:
<form action="your_file.php" method="post">
<input type="text" name="digits" />
<input type="submit" name="submit" />
</form>
Then in your_file.php:
<?php
if ($_POST['digits'] === '210392') {
// etc.
$_POST is a superglobal array that has all data POSTed to your script. digits in this case is an index in that array. It comes from the name of the input field.
<form action="check.php" method="POST">
<input type="text" name="digits" />
<input type="submit" value="click here" />
</form>
//in check.php
if (isset($_POST['digits']) and $_POST['digits'] == 12345){
header("Location: http://google.com");
}
else {
echo " error !!";
}
study html from w3school
http://www.w3schools.com/html/html_forms.asp
and php
http://www.w3schools.com/PhP/php_forms.asp
<form action="https://scm-intranet.tees.ac.uk/users/l1071039/bobbin/admin.php" method="post">
Password : <input type="password" name="pwd" />
<input type="submit" name="send">
</form>
// admin.php
<?php
if( isset($_POST['pwd']) ){
$pwd = $_POST['pwd'];
}
?>
You first need to create an input with the very important name attribute which you'll use later.
<form method="post">
<input type="password" name="mypassword" />
<button type="submit">Go</button>
</form>
PHP:
//Post method access with $_POST variable:
if ($_POST['mypassword'] == 'my-password') { //Oh, you should probably use SSH and hash the password
//Hooray!
}
Instead of echo, use header.
<?php
if ($_POST['digits'] === '210392')
{
header (loaction:https://www.facebook.com/);
}
?>

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