How can i output a string in a label on page? - php

I am trying want to ouput a string ,stored in a variabel, in a label on a page when i click on a button.
But i can't find out how. Still a beginner.
<form action="Test.php" method="post">
Output text: <input type="label" name="word" />
<input type="submit" method="submit" value="Print!" />
</form>
<?php
$word = "test";
if (isset($_POST['submit']))
{
//something that gives the label value $word//
}
?>

There are a few things wrong with your code.
Let me outline them.
Your submit input should have a name attribute, since your conditional statement is based on it if (isset($_POST['submit'])){...}, something I've modified to check if the input is not left empty, using PHP's empty() function.
The input type you have for your "Output text" is invalid, it should be type="text" and not type="label", there is no type="label".
method="submit" for your submit button is invalid for a few reasons. Method belongs in <form> and there is no method="submit".
You then need to assign a POST variable from the input:
such as:
$word = $_POST['word'];
Plus, from what looks to me that you're executing the entire code from within the same page, you can just do action="", unless your code is set in 2 seperate files.
In regards to what you want to achieve: You can then echo the input (if one was entered) using a ternary operator and giving it (the input) a value.
I.e.:
value="<?php echo isset($_POST['word']) ? $_POST['word']: '' ?>"
Here:
<form action="" method="post">
Output text: <input type="text" name="word" value="<?php echo isset($_POST['word']) ? $_POST['word']: '' ?>" />
<input type="submit" name="submit" value="Print!" />
</form>
<?php
if ( isset($_POST['submit']) && !empty($_POST['word']) )
{
$word = $_POST['word'];
echo $word;
}
?>
If you want to use a "label" for your input, then use:
<label for="word">Output text:
<input type="text" name="word" />
</label>
You should also guard against XSS attacks (Cross-side scripting) using:
http://php.net/strip_tags
http://php.net/htmlentities
http://php.net/manual/en/function.htmlspecialchars.php
I.e.:
$word = strip_tags($_POST['word']);
$word = htmlentities($_POST['word']);
$word = htmlspecialchars($_POST['word']);
A few articles you can read on XSS:
http://en.wikipedia.org/wiki/Cross-site_scripting
https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet

Your code should look like this
<form action="Test.php" method="post">
Output text: <input type="label" name="word" value ="<?php echo isset($label['data'])?$label['data']: '' ?>" />
<input type="submit" method="submit" value="Print!" />
</form>
<?php
$word = "test";
$label = array();
if (isset($_POST['submit']))
{
//something that gives the label value $word//
$label['data'] = $word;
}
?>
This should work
Regards
Ahmad rabbani

First of all your input element has type = label, it doesn't mean anything. Change it to type=text
And you are submitting value but not printing it. So in input field you have to print it also.
Look below code.
<?php
$word = "test";
if (isset($_POST['submit']))
{
// whatever you do with $word
}
?>
<form action="Test.php" method="post">
Output text: <input type="text" name="word" value="<?php echo $word; ?>"/>
<input type="submit" name="submit" value="Print!" />
</form>
UPDATE
One more thing I forgot to mention that you are submitting form to Test.php and printing this to file, if this file's name is Test.php then not an issue, other wise leave action property blank, so it submit data to itself.
method = submit there is nothing like this. you can set button name to submit, like name= "submit".

Related

How to add PHP code inside input's value in a form [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 4 years ago.
I want make some php test in my form and i've followed a tutorial but i don't get the result i wanted. I want to make sure that the borne's value isn't null. Then send the value to another page "exo.php" to use it.
The problem is when i add the php code inside input's value for example it's not cosindring it as php code but as a string so it prints the php code.
this is my code :
<?php
if (isset($_GET["submit"])) {
$borneErr="";
$borne="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["borne"])) {
$borneErr = "Missing";
}
else {
$borne = $_POST["borne"];
}
}
}
?>
<form class="" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<label> Borne : <input type="text" name="borne" value="<?php echo htmlspecialchars($borne);?>">
<span class="error">* <?php echo $borneErr;?></span> </label>
<button type="submit" value="OK"> Send !</button>
</form>
this is the result i get in this image below :
Your code raises notice
Undefined variable: borne
on this line:
<label> Borne : <input type="text" name="borne" value="<?php echo htmlspecialchars($borne);?>">
And also this notice
Undefined variable: borneErr
on this line:
<span class="error">* <?php echo $borneErr;?></span> </label>
You can fix that by defining the variable outside of the condition.
The form has a method="POST" attribute.
But you're checking the condition against GET data:
if (isset($_GET["submit"])) {
Also, you're checking existence of a field submit that is not included in the form data, since it's a <button>. You can either change it to <input> or change your PHP condition to check the borne field.
<input type="submit" value="Send !">
or
if (isset($_POST["borne"])) {
The check against $_SERVER["REQUEST_METHOD"] is now redundant so you can get rid of it.
The code could be simplified and polished even more but I'll leave it so it's easier see those errors fixed.
Working code:
<?php
$borneErr = "";
$borne = "";
if (isset($_POST["borne"])) {
if (empty($_POST['borne'])) {
$borneErr = "Missing";
} else {
$borne = htmlspecialchars($_POST['borne']);
}
}
?>
<form class="" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<label> Borne : <input type="text" name="borne" value="<?php echo $borne; ?>">
<span class="error">* <?php echo $borneErr;?></span> </label>
<input type="submit" value="Send !">
</form>
just add value inside quotation mark "HERE"
<input type="text" name="borne" value="<?php echo htmlspecialchars($borne);?>">
You can simply choose one here
<input type="text" name="borne" value="<?=$_SERVER["PHP_SELF"]?>"><Br>
OR
<input type="text" name="borne" value=""> /* leave it blank cause if blank i will submit automatically on the same page. */

search form prevent remove other query

html:
<form method="GET">
<input type="text" name="k" id="header-search" value="<?=$_GET["k"];?>"/>
<input type="submit" id="header-submit" value="" />
</form>
When current url is:
https://example.com/search/cats?b=5
After click on submit button it remove b query and show like this:
https://example.com/search/cats?k=sometext
But i want this result:
https://example.com/search/cats?b=5&k=sometext
I have other query like b, d and also c maybe add more in future, so this is not a static, maybe url have b maybe d or maybe c or maybe all together or maybe no one.
I tried this but looks like no changes:
action="<?=$_SERVER['REQUEST_URI'];?>"
You can add the variables inside hidden inputs:
<form method="GET">
<?php if(isset($_GET['b']){ ?>
<input type="hidden" name="b" value="<?=$_GET["b"];?>"/>
<?php } ?>
<input type="text" name="k" id="header-search" value="<?php echo isset($_GET["k"]) ? $_GET["k"] : '';?>"/>
<input type="submit" id="header-submit" value="" />
</form>
However this solution will not work very well if you have many different types of variables that may or may not exist all the time. If you add all the variables as hidden, they will all be visible when you submit the form. To prevent this, you will need to check if the variables are isset() and only print them if they are.
Here is a solution that uses hidden fields and handles any amount of get parameters:
<form method="GET">
<?php
foreach($_GET as $key => $value){
// do not make a hidden input for k, there is already a text input for k
if($key != 'k'){
echo '<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
}
}
?>
<input type="text" name="k" id="header-search" value="<?php echo isset($_GET["k"]) ? $_GET["k"] : '';?>"/>
<input type="submit" id="header-submit" value="" />
</form>

Simple form submit to PHP function failing

I'm trying to submit two form field values to a PHP function, on the same page.
The function works perfect manually filling the two values.
<?PHP // Works as expected
echo "<br />"."<br />"."Write text file value: ".sav_newval_of(e, 45);
?>
On the form I must be missing something, or I have a syntax error. The web page doesn't fail to display. I found the below example here: A 1 Year old Stackoverflow post
<?php
if( isset($_GET['submit']) ) {
$val1 = htmlentities($_GET['val1']);
$val2 = htmlentities($_GET['val2']);
$result = sav_newval_of($val1, $val2);
}
?>
<?php if( isset($result) ) echo $result; //print the result of the form ?>
<form action="" method="get">
Input position a-s:
<input type="text" name="val1" id="val1"></input>
<br></br>
Input quantity value:
<input type="text" name="val2" id="val2"></input>
<br></br>
<input type="submit" value="send"></input>
</form>
Could it be the placement of my code on the form?
Any suggestions appreciated.
You need to name your submit button, or check for something else on your if(isset($_GET['submit'])) portion:
<form action="" method="get">
Input position a-s:
<input type="text" name="val1" id="val1" />
<br></br>
Input quantity value:
<input type="text" name="val2" id="val2" />
<br></br>
<input type="submit" name="submit" value="send" />
</form>
OR keep same form, but change php to:
<?php
if( isset($_GET['val1']) || isset($_GET['val2'])) {
$val1 = htmlentities($_GET['val1']);
$val2 = htmlentities($_GET['val2']);
$result = sav_newval_of($val1, $val2);
}
?>
You can you hidden field as:
<input type='hidden' name='action' value='add' >
And check on php by using isset function that form has been submitted.

Use $_POST to get input values on the same page

Sorry if this is a rather basic question.
I have a page with an HTML form. The code looks like this:
<form action="submit.php" method="post">
Example value: <input name="example" type="text" />
Example value 2: <input name="example2" type="text" />
<input type="submit" />
</form>
Then in my file submit.php, I have the following:
<?php
$example = $_POST['example'];
$example2 = $_POST['example2'];
echo $example . " " . $example2;
?>
However, I want to eliminate the use of the external file. I want the $_POST variables on the same page. How would I do this?
Put this on a php file:
<?php
if (isset($_POST['submit'])) {
$example = $_POST['example'];
$example2 = $_POST['example2'];
echo $example . " " . $example2;
}
?>
<form action="" method="post">
Example value: <input name="example" type="text" />
Example value 2: <input name="example2" type="text" />
<input name="submit" type="submit" />
</form>
It will execute the whole file as PHP. The first time you open it, $_POST['submit'] won't be set because the form has not been sent.
Once you click on the submit button, it will print the information.

passing value in hidden field from one page to another in php

I have a simple registration form.
I want to pass value entered in one page to other in a text field.
how to pass and access it from next page in php.
this is my first php page.
Thanks in advance.
You can add hidden fields within HTML and access them in PHP:
<input type="hidden" name="myFieldName" value="someValue"/>
Then in PHP:
$val = $_POST['myFieldName'];
If you're going to ouput this again you should use htmlspecialchars or something similar to prevent injection attacks.
<input type="hidden" name="myFieldName" value="<?=htmlspecialchars($_POST['myFieldName']);?>"/>
Suppose this the form input in page A
<form name="" action="" method=post enctype="multipart/form-data">
<input type="text" name="myvalue" value="">
<input type=submit>
</form>
In page B
In the page you want to get values put this code
<?PHP
foreach ($_REQUEST as $key => $value ) {
$$key=(stripslashes($value));
}
?>
<form name="" action="" method=post enctype="multipart/form-data">
<input type="text" name="myvalue" value="<?PHP echo $myvalue" ?>">
<input type=submit>
</form>
So yo can use or attach variable value to another form do what else you want to do
use following code, that should help you.
<form action ="formhandler.php" method ="POST" >
<input name = "inputfield" />
<input type="submit" />
</form>
on formhandler.php file yo need to enter following code to get the value of inputfiled.
$inputfield = isset($_POST['inputfield'])?$_POST['inputfield']:"";
// now you can do what ever you want with $inputfield value
echo($inputfield);

Categories