PHP $_POST give empty value - php

I don't know what happened but there's no value return.
Please check if i made any mistake

Value is blank if you have no check for null or blank value on transfer.php
Case 1 : You are access file in same flow then no need to modify any thing its wokring.If you are access file without press submit button.directly using url then post is blank.
e.g. http://localhost:8080/stackoverflow/transfer.php
Case 2 : MIME type issue, enctype="text/plain" in the form, this should fix the issue.
from_post.php
<html>
<body>
<form action="transfer.php" method="post" enctype="text/plain">
<input type="text" name="name" value="">
<input type="submit" name="submit" value="submit">
</form>
transfer.php
Here you want to check is that submit button if fired or not.
<?php
if(isset($_POST['submit']))
{
$u=$_POST['name'];
if(isset($u) || (!is_empty())){
echo $u;
}
else
{
echo "the post doesn't have any value";
}
echo "<br/>";
}
?>

Related

Send form data to the same page not functionning

I'm a newbie in PHP, and I would like to send datas from a form and display it into the same page, here is my code for better understanding:
<form method="post" action="same_page.php">
<input type="text" name="owner" />
<input type="submit" value="Validate" />
</form>
<?php
if(isset($_GET['owner']))
{
echo "data sent !";
}
?>
So normally, after having entered some random text in the form and click "validate", the message "data sent!" Should be displayed on the page. I guess I missed something, but I can't figure out what.
You forgot to add submit name in your form.You are using POST as method so code should be
<form method="post" action="">
<input type="text" name="owner" />
<input type="submit" name="submit_value" value="Validate" />
</form>
<?php
if(isset($_POST['submit_value']))
{
echo '<pre>';
print_r($_POST);
}
?>
Will display your post values
You are using a POST method in your form.
<form method="post" action="same_page.php">
So, change your code to:
if (count($_POST) && isset($_POST['owner']))
Technically, the above code does the following:
First checks if there are content in POST.
Then, it checks if the owner is set.
If both the conditions are satisfied, it displays the message.
You can actually get rid of action="same_page.php" as if you omit it, you will post to the same page.
Note: This is a worst method of programming, which you need to change.
You should Replace $_GET['owner'] with $_POST['owner'] as in your form you have specified method='post'
Replace:
$_GET['owner']
With:
$_POST['owner']
Since you are using the post method in your form, you have to check against the $_POST array in your PHP code.

Displaying the form if the post is empty or result if it has already been submitted

This is my first post and I'm complete beginner so please be gentle :)
I'm trying to create a form that after submitting an account name would check and return a CNAME of the host (account+domain.com)
The problem is that I want to do it all on the same website so it will either display the form if nothing has been posted or display the result otherwise.
This is what I've created, it seems that I'm not calling the POST correctly, but I can't really get what am I doing wrong.
Please help
<?php
if(isset($_POST[DomainSubmit])){
$AccountName = $_POST[ClientDomain];
$CName = dns_get_record($AccountName."domain.com", DNS_CNAME);
echo '<h1>'.$CName.'<h1>';
}
echo'<form action="index.php" method="POST" ">
<input type="text" name="ClientDomain">
<input type="submit" name="DomainSubmit">
</form>'
?>
Try to add else so it will be displayed one or another stuff
<?php
if(isset($_POST['DomainSubmit']) && isset($_POST['ClientDomain'])){
$AccountName = $_POST['ClientDomain'];
$CName = dns_get_record($AccountName."domain.com", DNS_CNAME);
echo '<h1>'.$CName.'<h1>';
} else {
echo'<form action="index.php" method="POST" ">
<input type="text" name="ClientDomain">
<input type="submit" name="DomainSubmit">
</form>'
}
?>
Edit:
You forgot to properly write array POST (missing quotes)
$_POST[DomainSubmit]
And it should be
$_POST['DomainSubmit']

PHP - shouldn't 'if (!$_POST)' be enough (without having to specify the field name) to detect if data is being passed through a form with 1 field?

so I've got the latest version of Xampp installed and in the htdocs directory I have an htm page containing a very simple form with just a single text field and a submit button which, when clicked, links to a php page that prints a message saying "What you typed is:" followed by what was typed. If nothing was actually typed in the field, after clicking on the submit button, the php page will display an error message saying "Error: you didn't type anything".
Here is the code for the htm page:
<html>
<head>
</head>
<body>
<center>
<form action="p1.php" method="post">
Type something:
<input type="text" name="nom">
<input type="submit" value="SEND">
</form>
</center>
</body>
</html>
And here is the initial code for the php page:
<html>
<head>
</head>
<body>
<?PHP
if (!$_POST) {echo "Error: you didn't type anything";}
else {echo "What you typed is: " . $_POST["nom"];}
?>
</body>
</html>
So with this php code, if I type anything in the field and click the submit button, the php page will display "What you typed is:" followed by what was typed but if I don't actually type anything in the field and click the submit button, the php page will display "What you typed is:" followed by nothing instead of displaying "Error: you didn't type anything".
However, I discovered that if I changed the "if (!$_POST)" to "if (!$_POST["nom"])", then if I didn't type anything in the field, the php page would display "Error: you didn't type anything"...problem solved.
But this surprised me, as I have seen in my course material an example (it is referred as a self-calling form or something along those lines) where "if (!$_POST)" is used.Here it is:
<html>
<head>
<title>Me llamo a mi mismo...</title>
</head>
<body>
<?
if (!$_POST){
?>
<form action="auto-llamada.php" method="post">
Nombre: <input type="text" name="nombre" size="30">
<br>
Empresa: <input type="text" name="empresa" size="30">
<br>
Telefono: <input type="text" name="telefono" size=14 value="+34 " >
<br>
<input type="submit" value="Enviar">
</form>
<?
}else{
echo "<br>Su nombre: " . $_POST["nombre"];
echo "<br>Su empresa: " . $_POST["empresa"];
echo "<br>Su Teléfono: " . $_POST["telefono"];
}
?>
</body>
</html>
So why isn't "if (!$_POST)" not working in my case? (using Mozilla as the browser)
when posting like this and having empty fields, it still gets saved as value.
var_dumping this
<form method="post" action="fgc.php">
<input type="text" name="horse">
<input type="submit">
</form>
will return:
array(1) { ["horse"]=> string(0) "" }
In PHP, the $_POST superglobal is always defined, regardless of whether or not the method was actually POST, or if any data was posted. However, if it isn't a POST, or if there is no data, that array will be empty.
If you convert an array to a boolean, the value will be false if there are no elements in the array.
<?php
var_dump(isset($_POST)); // Always TRUE
var_dump(!!$_POST); // TRUE if data was posted (even if empty fields), FALSE otherwise
The reason your documentation says to use !$_POST is that often times the page will be loaded with the GET method, in which case, $_POST will be an empty array.
try this code
if($_SERVER['REQUEST_METHOD'] == 'POST'){ // if the form was submitted
if(isset($_POST['name']) && !empty($_POST['name'])){ // basic validation
// # Don't forget XSS sanitizing !
$name = htmlspecialchars($_POST['name']);
// Add data to DB or something
}else{
echo 'Error: Required field "name"';
}
}

PHP file upload isset failing

there seem to be a lot of people asking this question and not getting an answer.
is there an issue with checking if a file upload part of a form has been filled in or not?
is this because its a global variable and is allways set?
the problem im having is checking if a file input ytpe has been filled in or not.
the code below shows various examples of checking to see if the file field is filled in by the user or not.
they all fail to spot when its been filled in:
<?php
if (isset($_POST['add']))
{
//if (!isset($_POST['pic'])) { echo"pic not filled in";}else{echo"pic filled in";}
if (!isset($_POST['pic'])) { echo"pic not filled in";}
if (isset($_POST['pic'])) { echo"pic filled in";}//end of check to see if picture has been filled in
//if (!isset($_POST['userfile[]'])) { echo"pic not filled in";}
//if (isset($_POST['userfile[]'])) { echo"pic filled in";}//end of check to see if picture has been filled in
//$pictureName = $_REQUEST['pic'];
//if ($pictureName == ''){echo"pic is blank";}
//else{echo"pic is NOT blank";}
//if ($_POST['pic'] == ""){echo"pic is blank";}
//else {echo"pic is NOT blank";}
}
else
{
/////////render form
?>
<form enctype="multipart/form-data" action="" method="post" id="save"><fieldset>
<input type="text" name="fileName" id="fileName" value=""/>
<input type="file" name="userfile[]" id="pic" />
<input name="add" id="save_button" type="submit" value="Update"/>
</fieldset></form>
<?php
}
?>
because your using 'pic' not 'userfile'. is the html name that makes the post key value, not the id.
it should be userfile not userfile[] unless you have a multi input form
P.S I see no large number of unanswered questions of this type.
i have found the answer ive been looking for finally.
a massive thanks to You whos answer to this question helped me a lot:
the problem i was having was that if you check to see if a field in the form isset it will always be set once the submit button has been pressed.
if, like me, you want to check if something has actually been entered in to the field then i would say you need to do an if(!empty) check.
if the enctype="multipart/form-data" is added to the form this will stop working for file type fields.
please see my example below and thanks again to You:
<h1>test</h1>
<p>check to see if a field in a form as been filled in:</p>
<?php
if (isset($_POST['info'])) {
// do some stuff
echo"info isset. it will always be set when you click the submit button</br>";
if(!empty($_POST['info'])){ echo"info has been filled in</br>";}else{ echo"info is still empty</br>";}
if(!empty($_POST['filename'])){ echo"filename has been filled in</br>";}else{ echo"filename is still empty</br>";}
}
else
{
?>
<form method="post">
<input type="text" name="info" /></br>
<input type="file" name="filename" /></br>
<input type="submit" />
</form>
<?php }?>

Form: POST a string & get the result in the same form field?

Is to possible to POST a string in the form field and get converted string result in the same form field?
I use the code:
<?php
$string='';
if (isset($_POST['string']))
$string=$_POST['string']
if (isset($_POST['DoStuff']))
{
$string = doStuffWithThisString($string);
}
if (isset($_POST['DoOtherStuff']))
{
$string = doOtherStuffWithThisString($string);
}
?>
<form action="" method="post">
<!-- blank action attribute will post form to the current page-->
<input type="text" value="<?=$string?>" name="string" />
<!-- <?=$string?> is the same as <?php echo $string; ?> -->
<input type="submit" value="Do Stuff" name="DoStuff" />
<input type="submit" value="Do Other Stuff" name="DoOtherStuff" />
</form>
but get the result above form field...
Are you sure short tags are enabled?
See: http://php.net/manual/en/ini.core.php
If they are not, just use:
<?php echo $string; ?>
I don't see why it wouldn't work.
Depending on the browser, the button names may not be "DoStuff" and "DoOtherStuff". For example, in IE it will be $_POST['DoStuff_x'] and $_POST['DoStuff_y'].
do a print_r($_POST); to see what the form data is being posted as.
If you would use the same name in the submit fields, upon page reload in $_POST['name'] you would get the value you clicked on.
I think that's the solution to the issue, but can someone confirm this ?

Categories