PHP - cannot get value when input exists - php

I'm trying to use POST to get values from a form. I moved the part out to a test page:
<form name="form" action="" method="post">
<input type="text" name="subject" id="subject" value="enter something" />
</form>
// following that
<?php
if (isset($_POST["subject"]))
echo $_POST["subject"];
else
echo "input is not set";
?>
The echo is always "input is not set" regardless I set the value of input or not. And the tag "subject" does exist. This confused me. Why can't I get the value?

You should seperate your HTML form from your $_POST processing. This means that you provide the user a page which contains your posted form and then submit of this form is posted to a (different) page.
For example your form is (on form.php):
<form id="form" action="process_form.php" method="post">
<input type="text" name="subject" id="subject" value="" />
<input type="submit" value="Submit">
</form>
Then you create a file process_form.php:
<?php
if (isset($_POST['subject'])) {
echo $_POST['subject'];
}
else {
echo 'input is not set.';
}
?>

Related

Can't get form value with PHP

I am trying to get the value of a form (text field) with _POST, and store it to a text file but it doesn't work. Here's my code:
HTML
<form>
<input type="text" name="test" id="test" value="">
<input type="button" onclick="location.href='example.com/index.php?address=true';" value="ODOSLAŤ" />
</form>
PHP
if (isset($_GET['address'])) {
$email = $_POST['test'];
$myfile = fopen("log.txt","a") or die("Error.");
fwrite($myfile, "\n".$email);
// this prints nothing
echo $email;
}
I can't get the value of that text field. Nor GET nor POST doesn't work for me. What am I doing wrong?
You are missing a post method in your form.
<form method="post" action="example.com/index.php?address=true">
<input type="text" name="test" id="test" value="">
<input type="submit" value="ODOSLAŤ" />
</form>
You also have to change the following line.
if (isset($_GET['address'])) {
With
if (isset($_POST['address'])) {
You want to post after triggering an action as FreedomPride mentioned
Conclusion
You want to declare for example a post methodif you know that you would like to post that data in the future.
As FreedomPride also mentioned :
You are using a GET, but if a user does not input anything your script won't work, there by it is recommended to use a POST
You are not submitting your form.
Change your code as follows....
<form method="post" action="example.com/index.php?address=true">
<input type="text" name="test" id="test" value="">
<input type="submit" value="ODOSLAŤ" />
</form>
You'll get what you want....
This is what you're missing as Tomm mentioned.
<form method="post" action="yourphp.php">
<input type="text" name="address" id="test" value="">
<input type="submit" name="submit"/>
</form>
In your PHP, it should be post if it was triggered
if (isset($_POST['address'])) {
$email = $_POST['test'];
$myfile = fopen("log.txt","a") or die("Error.");
fwrite($myfile, "\n".$email);
// this prints nothing
echo $email;
}
Explanation :-
In a form, an action is required to pass the action to the next caller with action. The action could be empty or pass it's value to another script.
In your PHP you're actually using a GET. What if the user didn't input anything. That's why it's recommended to use POST .

Not Getting the desired Output in PHP

My code looks completely correct.I just copied it from New Boston php tutorial,its working there. But its not working properly here. Every time after the submission else block get executed and it produces the output "Please fill the form". If I filled all the fields than if block should get executed, and it should print the 'contact_name', 'contact_email', 'contact_text', but its not.I am writing this code in netbeans.
here is the code:
<?php
if(isset($_POST['contact_name']) && isset($_POST['contact_email'])
isset($_POST['contact_text']))
{
echo $contact_name=$_POST['contact_name'];
echo $contact_email=$_POST['contact_email'];
echo $contact_text=$_POST['contact_text'];
}
else
{
echo 'Please fill the form';
}
?>
<form action="index.php" method="POST">
Name:<br><input type="text" name="=contact_name"><br><br>
Email address:<br><input type="text" name="contact_email"><br><br>
Message:<br>
<textarea name="contact_text" rows="6" cols="30"></textarea><br><br>
<input type="submit" value="Send">
</form>
Replace
Name:<br><input type="text" name="=contact_name"><br><br>
With
Name:<br><input type="text" name="contact_name"><br><br>
You have an error in your contact_name field:
Change this:<input type="text" name="=contact_name">
to this: <input type="text" name="contact_name">
There is an error of name="=contact_name"
Remove extra =
name="contact_name"

How to stop submit being shown in the address bar?

How do i stop the submit being shown in the search bar? am i doing something wrong? if i use post method it works fine, but recently i've tried using the get method and it prints both submit and the textbox text in the bar, i tried removing the name="submit" and it didn't do anything when i clicked it. SO how do i stop submit being shown in the address bar?
at the moment it give me $submit=submit, :(
ty all.
<form action="" method="get">
<input type="text" name="website" placeholder="Website Name">
<input type="submit" name="submit">
</form>
<?php
if(isset($_GET['submit']))
{
if(!empty($_GET['website']))
{
//do stuffs here
}
else
{
//else echo out that nothing was entered.
echo "nothing entered";
}
}
?>
Just use:
!empty($_GET)
In your first if statement
So your code should look something like this:
<form action="" method="get">
<input type="text" name="website" placeholder="Website Name">
<input type="submit">
</form>
<?php
if(!empty($_GET)) {
if(!empty($_GET['website'])) {
//do stuffs here
} else {
//else echo out that nothing was entered.
echo "nothing entered";
}
}
?>
To remove the variables from the URL bar, you need to use the POST method instead of the GET method.
So, this form code should work for you:
<form action="" method="POST">
<input type="text" name="website" placeholder="Website Name">
<input type="submit" name="submit">
</form>
You'll also need to alter your PHP code to use $_POST instead of $_GET.
The downside to this is that it won't show the submitted website in the URL bar, meaning users can't bookmark a submitted form. However, this may also be intended.
Alternative Solution
You can remove the name from the submit, and use isset($_GET) to test whether the form was submitted instead.
Here is an example of how this could be done:
<form action="" method="get">
<input type="text" name="website" placeholder="Website Name">
<input type="submit">
</form>
<?php
if(isset($_GET) && count($_GET) > 0) {
if(!empty($_GET['website']))
{
//do stuffs here
} else {
//else echo out that nothing was entered.
echo "nothing entered";
}
}
?>

how to insert in a table like shown in this image where tusername is come from previous page

i have page where i send tusername and get on 2nd page where a form is created.
i want to add multiple data in a table using that form. where id is requested and sub_code is entered by us table will save data like this.
http://i.stack.imgur.com/Zf8wg.jpg
<?php
include("db.php");
$tusername =$_REQUEST['tusername'];
#mysql_query("update INTO `techsub`(sub_code, tusername)
VALUES ('$sub_code','$tusername') where tusername='$tusername'");
?>
<html>
<body>
<form method="post">
Subject One: <input type="text" name="sub_code"><br>
Subject Two: <input type="text" name="sub_code"><br>
Subject Three: <input type="text" name="sub_code"><br>
Subject Four: <input type="text" name="sub_code"><br>
<input type="submit" value="Assign">
</form>
</body>
</html>
I think this can help you out
<?php
include("db.php");
// Lets test if the form has been submitted
if(isset($_POST['SubmitCheck'])) {
// The form has been submited
$tusername =$_REQUEST['tusername'];
foreach($_POST['sub_code'] as $sub_code) {
//insert your data
#mysql_query("update INTO `techsub`(sub_code, tusername)
VALUES ('$sub_code','$tusername') where tusername='$tusername'");
}
}
else {
// The form has not been posted
}
?>
<form id="Form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Subject One: <input type="text" name="sub_code[1]"><br>
Subject Two: <input type="text" name="sub_code[2]"><br>
Subject Three:<input type="text" name="sub_code[3]"><br>
Subject Four: <input type="text" name="sub_code[4]"><br>
<input type="hidden" name="SubmitCheck" value="Assign">
<input type="Submit" name="Form1_Submit" >
</form>

Post Form DATA to another page after PHP Validation

I have this issue with my validation and posting the data to another page.
Here is my form:
Signup.php
<form id="regForm" action="<?php echo htmlspecialchars($_SERVER["submit.php"]);?>" method="post" name="regForm">
<label for="fname">First Name:</label><input name="fname" type="text" size="25" maxlength="35" value="<?php if(isset($_POST['fname'])){echo $_POST['fname'];}?>"/><br/>
<label for="mdname">Middle initial:</label><input name="mdname" type="text" size="10" maxlength="35" value="<?php if(isset($_POST['mdname'])){echo $_POST['mdname'];}?>"/><br/>
<label for="lname">Last Name:</label><input name="lname" type="text" size="25" maxlength="35" value="<?php if(isset($_POST['lname'])){echo $_POST['lname'];}?>"/><br/>
<br/>
<label> </label><input type="submit" name="Signup" class="formButton" value="Signup" /></form>
And here is my submit.php which will validate the signup.html input
submit.php
function msg($status,$txt)
{
return '{"status":'.$status.',"txt":"'.$txt.'"}';
}
// we check if everything is filled in and perform checks
//check if fname is empty
if(!$_POST['fname'])
{
die(msg(0,"<p>Please enter your first name.</p>"));
}
//check if lname is empty
if(!$_POST['lname'])
{
die(msg(0,"<p>Please enter your last name.</p>"));
}
Now, my issue is this, in my "submit.php" file, I want to know what codes to put after the form fields validation that would enable me post the input data to another page because, I plan making it a two-page signup form. Let's say my next page is signup-2.html
how do I post the data after validation to the next page? I know how to retrieve the posted data on the next page like using Session or Echo the $_POST data but, my main issue is....how do I make the form post the data after the validation messages in my submit.php file?
use header :
header("Location: your page url?fname=$_POST['fname']&lname=$_POST['lname']");
but before this do not echo or print anything otherwise it won't redirect to that page.
you can use the data on destination page like this:
$_GET['fname']
example:
submit.php
function msg($status,$txt)
{
return '{"status":'.$status.',"txt":"'.$txt.'"}';
}
// we check if everything is filled in and perform checks
//check if fname is empty
if(!$_POST['fname'])
{
die(msg(0,"<p>Please enter your first name.</p>"));
}
//check if lname is empty
if(!$_POST['lname'])
{
die(msg(0,"<p>Please enter your last name.</p>"));
}
header('Location:download.php?fname='.$_POST['fname']."&lname=".$_POST['lname']);
view.php
<html>
<body>
<form action="submit.php" method="post">
<input type='text' id="fname" name="fname"/>
<input type='text' id="lname" name="lname"/>
<input type="submit" id="button" value="submit"/>
</form>
</body>
</html>
download.php
<?php
echo "First Name........".$_GET['fname'];
put these three file in same directory and run view.php. you will be ok.

Categories