$_POST array is always empty - php

I cant figure out why this very simple test of using the input from the form doesnt work..
<?php
echo "TEST";
echo "<pre>" . print_r($_POST, true) . "</pre>";
if(isset($_POST['SubmitButton'])){ //check if form was submitted
$input = $_POST['inputText']; //get input text
echo "Success! You entered: ".$input;
}
?>
<html>
<body>
<form action="" method="post">
<input type="text" name="inputText"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>
When I display the array it shows that it is empty. When I enter something in the input field and click submit, nothing changes.
The demo page
I would be very grateful if anyone has an idea, thanks.

2 things
SubmitButton needs a value="something" then change
echo "<pre>" . print_r($_POST, true) . "</pre>";
to
echo "<pre>" . print_r($_POST) . "</pre>";
You only need the "true" if you want to return that as a var, like
$blob = print_r($_POST, true);
echo "<pre>" . $blob . "</pre>";

If anyone has a similar problem in future (I don't think this is the solution for this specific case):
I was debugging an application and php api since the past 4 hours, printing my output everywhere but I couldn't figure out what the issue was.
The production system was working fine while our testing environment made was not accepting any of my post arrays.
In the end it turned out, that I had a wrong url opened from the application (http:// instead of https://), so a .htaccess file redirected all my requests to https but removed all post information.
Hope this helps anyone.

Related

Is this the "right" way to call for a txt file in php

so I'm working on a php example and the solution I've come up with works. I'm just not sure if it's the "best way" or maybe the "right way" to do it.
I'm making a guest book the html and the first php is provided:
HTML:
<h2>Enter your name to sign our guest book</h2>
<form method="POST" action="SignGuestBook.php">
<p>First Name <input type="text" name="first_name"/></p>
<p>Last Name <input type="text" name="last_name"/></p>
<p><input type="submit" value="Submit"/></p>
</form>
<p>Show Guest Book</p>
PHP:
<?php
if (empty($_POST['first_name']) || empty ($_POST['last_name']))
echo "<p>You must enter your first and last name.
Click your browser's back button to return to the Guest Book.</p>\n";
else {
$FirstName = addslashes($_POST['first_name']);
$LastName = addslashes($_POST['last_name']);
$GuestBook = fopen("guestbook.txt", "ab");
if (is_writeable("guestbook.txt")) {
if (fwrite($GuestBook, $LastName . ", " . $FirstName . "\n"))
echo "<p>Thank you for signing our guest book!</p>\n";
else
echo "<p>Cannot add your name to the guest.<p>";
}
else
echo "<p>Cannot write to the file.<p>\n";
fclose($GuestBook);
}
?>
So here, i'm clicking to see who signed the guest book and displaying the txt contents. Everything works perfectly, my solution just feels too simple (maybe). Since I'm still learning, I'd rather learn how to do something the right or better way. So if I should approach this differently, please let me know:
<?php
echo "<pre>";
readfile("guestbook.txt");
echo "</pre>";
?>
The biggest problem of your code is potential loss of clarity which one is the first name and which one is the last name. You save it as {first name},{last name}, but what if I put a comma there in my first or last name?
I'd suggest using a well-known format such as CSV or JSON, which solve problems like this.
I'd go with JSON, which is very easy to work with in PHP:
$data = [
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
];
$json = json_encode($data, JSON_PRETTY_PRINT);
file_put_contents('guestbook.json', $json);
Then, you can retrieve your data by doing the opposite:
$json = file_get_contents('guestbook.json');
$data = json_decode($json, true); // true makes it an array
Also, please notice how I'm simply using file_put_contents(). It's a one-liner for putting contents into a file in PHP.

Set new values on the url

I have a url which is showing two values posted using a GET method in php.
I can get the values from the url using this example -
suppose the url is http://example.com/?name=Hannes
My code
<?php
echo 'Hello ' . htmlspecialchars($_GET["name"]) . '!';
?>
And the output Hello Hannes!
My question is- how can I set the values to the url on button click method post
eg.
<form method="POST">
if(isset($_POST['$btn'){
htmlspecialchars($_GET["name"])="Lucky"; /*If there is something like this*/
}
</form>
Thank you.
Try with -
if(isset($_POST['$btn'){
header('location:yourpage.php?name='.$yourValue);
exit;
}

Form to CSV and "Thanks" in new tab using POST

I'm trying to accept a form and write it to a CSV (invisible to the people submitting the form, but I can look at it as a compilation of everyone's entries on the server when I feel like it). Every time someone enters the form, it will become a new line on the CSV. To show that the people are actually submitting, a new tab will pop up with a little "thank you" like message and their submission so they can make sure it's theirs. Yes, I do have a JS form validation that works perfectly, but since that doesn't have a problem I left it out to save space.
Here is my current problem. In Firefox, I just get a blank new tab and nothing changes on my--blank--CSV, which is titled testForm.csv. In Chrome, a new tab opens that contains all the code on my php document, and my CSV stays blank.
Here's the snippet of my HTML:
<html>
<body>
<form name="Involved" method="post" action="postest.php" target="_blank" onsubmit="return validateForm();">
Name: <br><input type="text" name="name" title="Your full name" style="color:#000" placeholder="Enter full name"/>
<br><br>
Email: <br><input type="text" name="email" title="Your email address" style="color:#000" placeholder="Enter email address"/>
<br><br>
How you can help: <br><textarea cols="18" rows="3" name="help" title="Service you want to provide" style="color:#000" placeholder="Please let us know of any ways you may be of assistance"></textarea>
<br><br>
<input type="submit" value="Submit" id=submitbox"/>
</form>
</body>
<html>
Here is postest.php:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$help = $_POST['help'];
$csvData = $name . "," . $email . "," . $help . '\n';
echo "Thank you for your submission! We'll get back to you as soon as we can!";
echo "I'm " . $name . ", my email is " . $email . ", and I can help in that: \n" . $help;
$filepointer = fopen('testForm.csv','a');
if ($filepointer){
fwrite($filepointer,$csvData);
fclose($filepointer);
exit();
}
?>
I checked out this question about echoing to see if that was my problem. I asked this question before and nobody seemed to find anything wrong with my code other than the obvious $_POSTEST problem. This page looked like what I was going for, but wasn't. This question kind of had what I was going for but didn't actually have the POST code and the answer was one of the most useless things I've ever read (in a nutshell: "Just do it. It isn't that complicated." and some links to other SO questions, which I followed). They brought me here and here. I put exit(); after fclose() like it seemed to work for the first one (it did nothing). With the second, the user's code was too far removed from the codes I've been looking at for me to change my code over to what he/she was doing. I've been searching a lot, and doing extensive googling, but I'm going to cut my list of research here because nobody wants to read everything; this is just to prove I tried.
Let me know if there's anything else you need; I am a complete php novice and it's probably something very basic that I missed. On the other hand, I'm not seeing any major differences between my code and others' at this point.
Try something like this :
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$help = $_POST['help'];
$filepointer = fopen('testForm.csv','a');
fputcsv($filepointer, array($name,$email, $help));
echo "Thank you for your submission! We'll get back to you as soon as we can!";
echo "I'm " . $name . ", my email is " . $email . ", and I can help in that: \n" . $help;
?>
This is the error :-
---> $filepointer = fopen('testForm.csv','a');
$fp = fopen('testForm.csv','a');
if ($fp){
fwrite($fp,$csvData);
fclose($fp);
exit();
}
And the real issue is developing without
display_errors = On
log_errors = On
Look for these parameters in the php.ini file, and turn them on, unless you are developing on a live server, in which case, you really should set up a test environment.
and then not looking at the php error log
UPDATE
There was only one line to change actually, here is the complete code.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$help = $_POST['help'];
$csvData = $name . "," . $email . "," . $help . '\n';
echo 'Thank you for your submission! We\'ll get back to you as soon as we can!';
echo '\"I\'m \"' . $name . ", my email is " . $email . ", and I can help in that: \n" . $help;
$fp = fopen('testForm.csv','a'); // only line changed
if ($fp){
fwrite($fp,$csvData);
fclose($fp);
exit();
}
?>
Your error is really basic and I am ashamed of you. Your problem is obviously that you have not been using a server, nor do you have a PHP package installed on your computer. When you told your computer target="_blank" and method="post", it knew what you wanted, being HTML. However, not having anything that parsed PHP, it had no idea how to read your code and came up as a blank page in Firefox and a block of code in Chrome.
You, indeed, have no idea what you are doing.

Cannot get posted variables to echo in the form

It seems my code is correct, however the posted variables in the form will not echo in the update user settings page in the form. I have echoed the posted ids from the input in the database but I cannot get the variables to show.
I have done this in Codeigniter fine but am trying to do it in pure php with my own framework.
$users = new Users($db); comes from my init.php file that is called at the beginning of the file below.
when I
<?php var_dump($user['first_name'])?>
I get Null
<input type="text" name="first_name" value="<?php if (isset($_POST['first_name']) )
{echo htmlentities(strip_tags($_POST['first_name']));} else { echo
$user['first_name']; }?>">
Hoi Stephen,
Try print_r($_POST["first_name"]); instead of var_dump();
or just for all:
print_r($_POST);
best regards ....
add this at the top of your html page
#extract($_REQUEST);
and put is just to check and after checking remove the below line
print_r($_REQUEST);
hope this help .

A form that spits out the input

I can't for the life of me find a form that doesn't email the results that you submit.
I'm looking to find a form that I can have users enter simple data that i can then spit back out at them in different arrangements. If they submit First and Last, I'll spit out, amongst other things, FirstLast#domain.com. I'm willing to scrounge the code manually to do this, but I cant find a simple form that would allow me to do this.
Edit: PHP or similar simple languages. I've never touched .NET before.
Form:
<form action="process.php" method="post">
First: <input type="text" name="first" />
Last: <input type="text" name="last" />
<input type="submit" />
</form>
Next page:
<?php
$first = $_POST['first'];
$last = $_POST['last']
echo $first . "." . $last . "#domain.com";
?>
See http://www.w3schools.com/php/php_forms.asp for more examples and explanation
Regardless of how you get it, always remember to never trust user input.
<?php
$sfirst = htmlentities($_POST['first']);
$slast = htmlentities($_POST['last']);
echo $first . "." . $last . "#domain.com";
?>
Also, running a validator on the final result may be helpful. But please don't write your own email address validator.
What language/platform/environment are you working in?
I guess you might be looking for a hosted script or webform (the way that people will host web-to-mail scripts I suppose) but I doubt there would be one out there that does this.
But if you have a specific framework to work in, e.g. PHP or .net, please update the question and let us know which.
Thing that simple doens't even need server-side support.
<form onsubmit="magic(this);return false">
<p><label>First <input name=first/></label>
<p><label>Last <input name=last/></label>
<input type="submit">
<div id="output"></div>
</form>
<script type="text/javascript">
var output = document.getElementById('output');
function toHTML(text)
{
return text.replace(/</g,'<');
}
function magic(form)
{
output.innerHTML = toHTML(form.first.value + form.last.value) + '#domain.com';
}
</script>
If I get your question right, sounds like this might do what you need..
Note: This PHP code doesn't require any knowledge of the fields in the form that submits to it, it just loops through all of the fields, including multiple-choice fields (like checkboxes), and spits out their values.
<?php
// loop through every form field
while( list( $field, $value ) = each( $_POST )) {
// display values
if( is_array( $value )) {
// if checkbox (or other multiple value fields)
while( list( $arrayField, $arrayValue ) = each( $value ) {
echo "<p>" . $arrayValue . "</p>\n";
}
} else {
echo "<p>" . $value . "</p>\n";
}
}
?>

Categories