Get value in a textfield using GET method PHP - php

I have a PHP file named "delete-wish-form.php" with a form that has the following fields:
wishId (hidden field) The value of this field should be equal to a GET variable that is passed in via the URL.
So my question is if the URL is delete-wish-form.php?wishId=1 then I want my hidden field to look like this:
CODE:
<!DOCTYPE HTML>
<html>
<title>Delete Wishes</title>
<body>
<h2>Wishes Form</h2>
<?php
$wishId = $_GET['wishId'];
?>
<form method="get" action="process-delete-wish-form">
<input type="hidden" name="wishId">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

You should check if the wishId is define in your url param to avoid thrown error in form, and use htmlspecialchars to avoid xss attack on GET parameter
<?php
$wishId = '';
if(isset($_GET['wishId'])) {
$wishId = htmlspecialchars($_GET['wishId'],ENT_QUOTES);
}
?>
and then in your input hidden field
<input type="hidden" name="wishId" value="<?php echo $wishId;?>">

Related

How to acess form details in php page using html 'id' attribute

i have a form having employeeNo,name and a submit button. when the form is submitted it redirects to a php page, Now the problem is i want to access the values of the form by using 'id' attribute?if possible i need the html and php code.
Is there a way to solve this Problem?
enter code here<html>
<head>
</head>
<body>
<form action="test.php" method="">
<input type="text" name='employeeNo' id="input1">
<br>
<input type="text" name='name' id="input2">
<br>
<input type="submit" id="input3">
</form>
</body></html>
In your test.php file you need to retrieve the values. Since you don't define any method in your form, by default it should be "GET"
Your php file could look like the following:
<?php
$employee_no = $_GET['employeeNo'];
$name = $_GET['name'];
I would suggest defining your form method as POST though, like the following:
...
<form action="test.php" method="POST">
...
so your php file will retrieve values from form like this:
<?php
$employee_no = $_POST['employeeNo'];
$name = $_POST['name'];
You may also look at this for further explanation:
https://www.w3schools.com/php/php_forms.asp

Call php using arguments from a text box

I want to make a website that has a text box and if an user enters "hello" and clicks submit I want to show example.com/test.php?link=hello
How can I do that? The php is already written I just need to make something with html or php to be able to show example.com/test.php?link=whatUserEnters
Thanks!
The form should have the method "get" defined. Then on the test.php page you just
<form method="get" action="test.php">
...
<input type="text" name="link"/>
</form>
and on the test.php
<? echo $_GET['link']; ?>
or
<?= $_GET['link']?>
Edit with the POST solution:
<form method="post" action="test.php">
...
<input type="text" name="link"/>
</form>
and on the test.php
<? echo $_POST['link']; ?>
<!DOCTYPE html>
<html>
<body>
<!--just create a text box, give an id to it.-->
<input type="text" id="text_to_display" />
<input type="button" value="Submit" onClick="concat();" />
<!--Create a label or div assign a separate id to it too.-->
<p id="concatenated_text"></p>
<script>
//Create a function in javascript.
function concat() {
//Get the value in function using that id.
var text = document.getElementById("text_to_display").value;
//Concatenate the value with the string and put it in div using id.
var final_text = "example.com/test.php?link="+text;
// you can use location.href to use it for link purpose
location.href = final_text;
}
</script>
</body>
</html>

PHP form not sending data to $_POST

<!DOCTYPE html>
<html>
<body>
<form action="test.php" method="post">
<input name="test" type="text">
<input type="password" name="data">
<input type="submit">
</form>
</body>
</html>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$name = $_POST['test'];
$pass = $_POST['data'];
}
?>
I am trying to make a simple form in PHP that users will use for login. When using this code, nothing ever gets put inside the $_POST. I am debugging using PHPstorm and I can tell that the request method is definitely POST, but no data is getting passed through. What am I doing wrong?
Try adding a slash at the begening of the action parameter of the form tag.
<form action="/test.php" method="post">
Try to remove "action" from form. It worked for me.
Code: http://nimb.ws/6pXjnz
Output:http://nimb.ws/PvuxZs

Linking html to php with $_POST

I have been trying to create a form that reads a post from an HTML form and displays an element from that post IF it detects that the post exists.
However, each time the post is submitted, it simply reloads the form as though no post were provided.
<!DOCTYPE html>
<html>
<head>
<title>Upload from Manifest</title>
</head>
<body>
<?php
if (isset($_POST['manifest'])) {
echo 'we are in the IF';
echo($_POST['manifest']);
}
?>
<h1>Submission from manifest into main db</h1>
<div class="container offset-top120">
<form method="post" action="https://nhsggc.cogiva.com/prism/loadFromManifest.php" enctype="multipart/form-data">
<input id="manifest" type="text" />
<input id="submit" value="Submit" type = "submit" />
</form>
</div>
</body>
</html>
Your form is going to either a different page (https://nhsggc.cogiva.com/prism/loadFromManifest.php so check for that first) if you wanted it to go to same page, you can give the action as just '#', or put in the whole URL like you have.
You're missing the name attribute from your submit input and text input. Read up on the name attribute!
<input id="manifest" type="text" name="manifest">
<input id="submit" value="Submit" type="submit" name='submit' />
Then your PHP should look like this:
<?php
if (isset($_POST['submit'])) {
echo 'Inside an if';
echo $_POST['manifest'];
}
Then it should work.

$_POST superglobal does not contain the expected data after an HTML form submission

<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>

Categories