I want to make a PHP variable the value of a hidden form input. The form is inside of my PHP (I'm echoing the form), and nothing that I have tried works.
Here's my code:
echo '
<div id = "login">
<form action = "process.php" method = "POST">
Name: <input type = "text" name = "name" required>
<!-- Here is where I need to make my PHP variable the value: -->
<input type = "text" name = "referer" style = "display: none" value = "$variable">
<input type = "submit" name = "submit" value = "Enter">
</form>
</div>
';
Try this:
?><!-- exit out of php into html-->
<div id = "login">
<form action = "process.php" method = "POST">
Name: <input type = "text" name = "name" required>
<!--Here is where I need to make my PHP variable the value:-->
<input type = "text" name = "referer" style = "display: none" value = "<?=$variable?>">
<input type = "submit" name = "submit" value = "Enter">
</form>
</div>
<?php // enter back into php
The <?= ?> is a php short tag
Also, if you still want to use echo, try this:
//note: I changed the quotes
echo "
<div id = 'login'>
<form action = 'process.php' method = 'POST'>
Name: <input type = 'text' name = 'name' required>
<input type = 'text' name = 'referer' style = 'display: none' value = '$variable'>
<input type = 'submit' name = 'submit' value = 'Enter'>
</form>
</div>
";
See this Q/A for more info
The usual string replacements "$var" don't work here , as it's all contained within a single quote string which doesn't allow for string replacements. You will have to concatenate manually
echo '
<div id = "login">
<form action = "process.php" method = "POST">
Name: <input type = "text" name = "name" required>
<input type = "text" name = "referer" style = "display: none" value = "' . $variable . '">
<input type = "submit" name = "submit" value = "Enter">
</form>
</div>
';
Try this sample here https://eval.in/506642
echo "
<div id = 'login'>
<form action = 'process.php' method = 'POST'>
Name: <input type = 'text' name = 'name' required>
//Here's where I need to make my PHP variable the value:
<input type = 'text' name = 'referer' style = 'display: none' value = '$variable'>
<input type = 'submit' name = 'submit' value = 'Enter'>
</form>
</div>
";
Related
I'm trying to build a registration/login form using PHP (PHP 7.2.10), for now I'm just trying to be able to insert new users in my DB, and to log in with one of the users
<?php
//Connecion to the DB
try {
$bdd = new PDO('mysql:host=localhost; dbname=dbtest; charset=utf8', 'root', '123456');
} catch (Exception $e) {
echo "Error: ".$e;
}
//Registration form
//variables definition
if(isset($_POST['registration'])){
$name = $_POST['name'];
$first_name = $_POST['first_name'];
$nickname = $_POST['nickname'];
$age = $_POST['age'];
$sex = $_POST['sex'];
$passwd = $_POST['passwd'];
$passwd2 = $_POST['passwd2'];
$email = $_POST['email'];
$address = $_POST['address'];
$date = date('d/m/Y');
//checking mandatory fields are filled
if(!empty($nickname) AND !empty($email)){
//checking entered email is valid
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
//confirming password
if($passwd == $passwd2){
// checking the entered email address doesnt already exist in the DB
$TestEmail = $bdd->query('SELECT id FROM test WHERE email = "'.$email.'"');
if($TestEmail->rowCount() < 1 ){
$bdd->query('
INSERT INTO test
(name, first_name, sex, nickname, age, address, email, passwd)
VALUES
("'.$name.'", "'.$first_name.'", "'.$sex.'", "'.$nickname.'", "'.$age.'", "'.$address.'", "'.$email.'", "'.$passwd.'", "'.$date.'")
');
} else $return = "There is already a user registered with this email address";
} else $return = "Entered passwords are not matching";
} else $return = "Please enter a valid email address";
} else $return = "At least one field is missing";
}
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Registration/Login form</title>
</head>
<body>
<?php if(isset ($_POST['registration']) AND isset($return)) echo $return; ?>
<!-- Registration/login form -->
<form action="" method="POST">
<input type = "text" name = "name" placeholder = "Name"><br>
<input type = "text" name = "first_name" placeholder = "First Name"><br>
<input type = "text" name = "nickname" placeholder = "Nickname"><br>
<input type = "text" name = "age" placeholder = "Age"><br>
<select name = "sex" placeholder = "Sex"><br>
<option value = "Male">Male</option>
<option value = "Female">Female</option>
</select><br>
<input type = "text" name = "address" placeholder = "Address"><br>
<input type = "email" name = "email" placeholder = "Email address"><br>
<input type = "password" name = "passwd" placeholder = "Your Password"><br>
<input type = "password" name = "passwd2" placeholder = "Confirm your Password"><br>
<input type="submit" name = "registration" value="Register">
</form>
<hr>
<?php if(isset ($_POST['login']) AND isset($return)) echo $return; ?>
<form action="" method="POST">
<input type = "email" name = "email" placeholder = "Email address"><br>
<input type = "password" name = "mdp" placeholder = "Your Password"><br>
<input type="submit" name = "login" value="Log in">
</form>
</body>
</html>
When trying to register a new user, I fill all the fields, then click on "register", but nothing happens, it just empties all the fields, and no data is inserted in my DB, though I get no error message.
When trying to log in, I get the following error on top of my form : "Notice: Undefined index: passwd in C:\wamp64\www\Hakikar\index.php on line 61", and besides, the message "At least one field is missing", though I filled both email and password fields
If you name an input from the user in one html file using and tags, how do you use it in another html file?? I want to use only html. No javascript or anything.
The main file:
<HTML>
<HEAD>
<TITLE>
Harry Potter
</TITLE>
</HEAD>
<BODY text = "red" bgcolor = "green">
<CENTER><H1><B><U>Harry Potter</U></B></H1></CENTER>
Please register for HPhmR by <A href = form.html>Clicking Here</A>
</BODY>
</HTML>
Form.html:
<HTML>
<HEAD>
<TITLE>
HPhmR Resgistering
</TITLE>
</HEAD>
<BODY bgcolor = "turquoise">
<FORM action = "harry potter.html" method = "post" name = "HPhmR">
<P>Please enter your username :
<INPUT type = "text" size = "30" name = "username" maxlength = "20">
</P>
<P>Please enter your password :
<INPUT type = "password" size = "30" name = "password" maxlength = "15">
</P>
<P>Please choose your favourite character:<BR>
<SELECT name = "favcha" size = "8" MULTIPLE>
<OPTION value = "Dumbledore">Albus Dumbledore</OPTION>
<OPTION value = "Voldemort">Lord Voldemort</OPTION>
<OPTION value = "Harry">Harry Potter</OPTION>
<OPTION value = "Ron">Ron Weasely</OPTION>
<OPTION value = "Hermione">Hermione Granger</OPTION>
<OPTION value = "Sirius">Sirius Black</OPTION>
<OPTION value = "Bellatrix">Bellatrix Lestrange</OPTION>
<OPTION value = "Draco">Draco Malfoy</OPTION>
</SELECT>
</P>
<P>Select your age range :
<INPUT type = "radio" name = "10-15" value = "1">10 to 15 yrs
<INPUT type = "radio" name = "15-20" value = "2">15 to 20 yrs
<INPUT type = "radio" name = "20-30" value = "3">20 to 30 yrs
<INPUT type = "radio" name = "30-45" value = "4">30 to 45 yrs
<INPUT type = "radio" name = "45-60" value = "5">45 to 60 yrs
<INPUT type = "radio" name = "60+" value = "6">Above 60 yrs
</P>
<INPUT type = "submit" value = "Submit and Register">
</FORM>
</BODY>
</HTML>
How do I use the input from the user in form.html? I want to use that input and do something else in the main file.
You can not do that using just html.but you can use php or asp.net to send forms from one file to another.
When having two separate login fields on a webpage, my browser (Google Chrome) assumes that both of them require the same credentials, which is not a valid assumption. I would have liked my browser to store the credentials to each of them separately.
<form id='login-form' method = "post" action = "/login.html">
<ul>
<li><input id = "username" name = "username" type = "text" placeholder = "Username"/></li>
<li><input id = "password" name = "password" type = "password" placeholder = "Password"/></li>
<li><input id = "button" type = "submit" value = "Login"/></li>
</ul>
</form>
On another HTML page:
<form id='dx-login-form' method = "post" action = "/dxLogin">
<ul>
<li><input id = "dx_username" name = "dx_username" type = "text" placeholder = "Username"/></li>
<li><input id = "dx_password" name = "dx_password" type = "password" placeholder = "Password"/></li>
<li><input id = "button" type = "submit" value = "Login"/></li>
</ul>
</form>
You can give the second login form different autocomplete identifiers so it won't auto-fill from the first login form.
<li><input id = "dx_username" name = "dx_username" type = "text" placeholder = "Username" autocomplete = "dx_username" /></li>
<li><input id = "dx_password" name = "dx_password" type = "password" placeholder = "Password" autocomplete = "dx_password" /></li>
For more info: https://developers.google.com/web/updates/2015/06/checkout-faster-with-autofill
I am not able to correctly apply the decimal rounding, which is selected by the user before calculating a total price, in this angular js app.
HTML
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="lolCtrl">
<form class = "col-md-6">
<div class = "form-group">
<label for = "prezzo"> Prezzo </label>
<input type="number" class = "form-control" placeholder = "Prezzo" ng-model="price">
</div>
<div class = "form-group">
<label for = "percentuale"> Percentuale </label>
<input type="number" class = "form-control" placeholder = "Percentuale" ng-model="percent">
</div>
<div class = "form-group">
<label for = "precisione"> n° decimali </label>
<select class = "form-control" ng-model = "selectPrecision" ng-options="num.value as num.precision for num in decimali"></select>
</div>
</form>
<button class = "btn btn-default" ng-click="percentuale()">Prezzo</button>
<p>Il risultato è:<span ng-model = "selectPrecision">{{risultato | number : 7}}</span></p>
</div>
</body>
JS
var app = angular.module('myApp', []);
app.controller('lolCtrl', function($scope){
$scope.price = 20.232223;
$scope.percent = 20;
$scope.decimali= [
{value:0, precision: "0 decimali"},
{value:1, precision: "1 decimali"},
{value:2, precision: "2 decimali"}
];
$scope.percentuale = function (){
$scope.risultato = $scope.price + $scope.price* $scope.percent/100;
}
});
I have fiddled a lot with this example because I don't want to create a custom filter but it lead me nowhere near the result I want:
Plunkr
I've put a fixed number of 7 decimals for the purpose of giving you working code. I would like it to be dynamic without custom filters because, well I just have to pass a parameter to an already existing framework filter function.
How it's done?
Just change your
<p>Il risultato è:<span ng-model = "selectPrecision">{{risultato | number : 7}}</span></p>
to
<p>Il risultato è:<span ng-model = "selectPrecision">{{risultato | number : selectPrecision}}</span></p>
Here is my HTML code. In the browser, I click on the text fields and a box shows for file input. It is like I selected the type = "file" input field. Why does my HTML code do this?
<div id = "section">
<form action = "receive.php" method = "post">
<!-- Image to upload -->
<label for = "item"> Item: <input id = "item" type = "file" name = "items" accept = "image/*">
<!-- Text to enter -->
<label for = "mail"> Email: <input id = "mail" type = "text" name = "email">
<label for = "word"> Words: <input id = "word" type = "text" name = "words">
<input type = "submit" value = "Submit" name = "submit">
</form>
</div>
You need to close your <label> tags.
Otherwise, your entire form is in the <label> for the upload control, and clicking on the label will click the control.
You haven't closed your elements properly, most likely the <label> elements.
<div id = "section">
<form action = "receive.php" method = "post">
<!-- Image to upload -->
<label for = "item"> Item: <input id = "item" type = "file" name = "items" accept = "image/*"></label>
<!-- Text to enter -->
<label for = "mail"> Email: <input id = "mail" type = "text" name = "email"></label>
<label for = "word"> Words: <input id = "word" type = "text" name = "words"></label>
<input type = "submit" value = "Submit" name = "submit">
</form>
</div>