Background
Web-based contact form.
Problem
The $_POST array is empty. When errors are enabled, no errors (except empty array values) are found. The code was tested and working at one point, then left untouched until I posted this question. The host might have performed an upgrade.
Software
PHP 5.2.17
Apache 2.0.63
SSI
HTML Form
The HTML form is as follows:
<form method="post" action="contact.php" id="commentForm">
<label for="name">Name</label>
<input type="text" name="name" id="name" maxlength="64" /><br />
<label for="email">Email</label>
<input type="text" name="email" id="email" maxlength="320" /><br />
<label for="message">Message</label>
<textarea name="message" rows="10" cols="40" id="Message"></textarea><br />
<label for="human">40 + 2 =</label>
<input type="text" name="human" id="human" size="10" maxlength="3" /><br />
<p align="center">
<input type="submit" name="submit" value="Send" class="submit-button" />
</p>
</form>
PHP Code
The following code is called when the form is submitted:
$reason = 'default';
error_reporting( 0 );
ini_set( 'display_errors', 0 );
ini_set( 'register_globals', 0 );
ini_set( 'allow_url_fopen', 0 );
ini_set( 'expose_php', 0 );
ini_set( 'magic_quotes_gpc', 0 );
function not_contacted() {
global $reason;
// Redirects to computer, name, email, or message.
//
header( 'Location: ../error-'.$reason.'.shtml' );
}
function wms_error_handler($errno, $errstr, $errfile, $errline) {
not_contacted();
return true;
}
function wms_shutdown() {
if( is_null( $e = error_get_last() ) === false ) {
not_contacted();
}
}
set_error_handler( "wms_error_handler" );
register_shutdown_function( 'wms_shutdown' );
$name = trim( stripslashes( $_POST["name"] ) );
Logging
echo $_SERVER["REQUEST_METHOD"]; == GET
print_r( $_GET ); == Array ( )
print_r( $_POST ); == Array ( )
print_r( $_REQUEST ); ==
Array ( [__utma] => 181723617.1357984856.1311884601.1313715852.1313720411.12 [__utmz] => 181723617.1313720411.12.10.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=jigo [__utmc] => 181723617 [__utmb] => 181723617.3.10.1313720411 ) `
file_get_contents('php://input') == Empty
Questions
What could be causing the PHP POST variable values to be empty?
Why is the POST method being transmogrified into a GET method?
I think it is a php.ini or httpd.conf conflict, but cannot be sure (it is a hosted domain).
Thank you.
Update
The following test works.
test.shtml
<html>
<body>
<form method="post" action="test.php">
<input type="hidden" name="test" value="test" />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
test.php
<?
echo $_POST["test"];
?>
I also had a similar issue.
The problem was the data were posted to the http url, but there was a redirection to the https version of the url. Datas in $_POST are lost during the redirect.
I hope this can help others
Remove the following line from the .htaccess file:
RewriteRule ^(.*)$ http://www.whitemagicsoftware.com/$1 [R=301,L]
Related
I've got a basic email form that I want to use a custom handler on. I tried following this tutorial https://blog.osmosys.asia/2016/05/07/handling-form-submissions-in-wordpress/ but it seems that my custom hook is not being called.
<form class="" action="<?php echo admin_url('admin-post.php') ?>" method="post">
<input type="hidden" name="action" value="bb_submit_email">
<input type="email" id="email" name="email" value="example#email.com" required>
<?php wp_nonce_field('submit-email', '_mynonce'); ?>
<input type="submit" class="float-right btn btn-primary form-button" value="Submit">
</form>
<?php
add_action('admin_post_bb_submit_email', 'bb_handle_email_submit');
add_action('admin_post_nopriv_bb_submit_email', 'bb_handle_email_submit');
function bb_handle_email_submit() {
$logger = wc_get_logger();
$logger->add('submit-email-debug', 'triggered!');
if ( !isset($_POST['_mynonce']) || !wp_verify_nonce($_POST['_mynonce'], 'register-user')) {
return;
}
wp_remote_post('example.com', $_POST );
}
I tried using wp_ajax instead but that didn't work either. Anyone see something I'm missing here?
Update: I don't know if this helps but I checked the POST Request data and it has the following:
action: bb_submit_email
email: example#email.com
_mynonce: a2c0e80de7
_wp_http_referer: /
That all looks right to me?
I moved this
<?php
add_action('admin_post_bb_submit_email', 'bb_handle_email_submit');
add_action('admin_post_nopriv_bb_submit_email', 'bb_handle_email_submit');
function bb_handle_email_submit() {
$logger = wc_get_logger();
$logger->add('submit-email-debug', 'triggered!');
if ( !isset($_POST['_mynonce']) || !wp_verify_nonce($_POST['_mynonce'], 'register-user')) {
return;
}
wp_remote_post('example.com', $_POST );
}
to functions.php and it started working. Honestly not sure why?
i have the following form
<form action="/wp-content/themes/wallstreet/welcome.php" method="post" class="basic-grey">
<h1>Form
<span>Please fill all the texts in the fields.</span>
</h1>
<label>
<span>Your Nickname* :</span>
<input id="name" type="text" name="name" placeholder="It will appear in the text" />
</label>
<label>
<span>Your Email* :</span>
<input id="email" type="email" name="email" placeholder="Valid Email Address" />
</label>
<label>
<span>Message* :</span>
<textarea id="messagebook" name="messagebook" placeholder="The text that will appear" maxlength="80"></textarea>
</label>
<label>
<span>Code* :</span>
<input id="code" type="text" name="code" placeholder="The Code That we sent to your email" maxlength="8" />
</label>
<label>
<span> </span>
<input type="submit" class="button" value="Send" />
</label>
</form>
which uses the following php, this php basically posts the message value into a txt file
<?php
$var = $_POST['messagebook'];
file_put_contents("/var/www/wordpress/wp-content/themes/wallstreet/data.txt", $var . "\n", FILE_APPEND);
exit();
?>
but i want the submit button only work if my code field matches with the codes that are stored in a txt file like this
zACHAS5r
rKUzob3X
omqYjVQZ
BeF375BG
rFKQomMX
y8EVBTGH
Z7icxNoD
wnZ5qBvK
ftbPiCZa
sXJKDETK
wYDVLDPd
AjURjBdZ
LZR4fbtk
gmFY89TV
BAWDxpZ2
bGLLd9Az
qg4C93wN
YJnrDh2c
jwH6hV9h
tm3S4f5j
MU2ikfbu
ZXnUpfmY
hijZPTk4
C2oWha3T
irTg9oUA
jmjLDvL3
jUbiBtJo
gCCAQx6Z
Theorically i could make it work with this code, but i dont know where to implement it
function is_valid($code)
{
return in_array($code , explode(' ',file_get_contents('coderoute')));
}
EDIT1: Currrently i have this, and i get this error
<?php
function is_valid($code)
{
return in_array($code , explode(' ',file_get_contents("/wp-content/themes/wallstreet/codes.txt")));
}
$code = $_POST['code'];
if (is_valid($code)) {
$var = $_POST['messagebook'];
file_put_contents("/var/www/wordpress/wp-content/themes/wallstreet/data.txt", $var . "\n", FILE_APPEND);
}
exit();
?>
PHP Warning:
file_get_contents(/wp-content/themes/wallstreet/codes.txt): failed to
open stream: No such file or directory in
/var/www/wordpress/wp-content/themes/wallstreet/welcome.php on line 4,
$code = $_POST['code'];
$message_book = $_POST['messagebook'];
if(is_valid($code)) {
file_put_contents('/var/www/wordpress/wp-content/themes/wallstreet/data.txt', "{$message_book}\n", FILE_APPEND);
exit();
}
function is_valid($code) {
$codes = file('/var/www/wordpress/wp-content/themes/wallstreet/codes.txt', FILE_IGNORE_NEW_LINES);
return in_array($code, $codes);
}
You've mentioned PHP Warning of No such file exists. You could provide absolute path of codes.txt to check if it works right.
Code you need to check is in $_POST['code'].
So pass it as argument to is_valid function:
<?php
$code = $_POST['code'];
if (is_valid($code)) {
$var = $_POST['messagebook'];
file_put_contents("/var/www/wordpress/wp-content/themes/wallstreet/data.txt", $var . "\n", FILE_APPEND);
}
exit();
?>
You can use a JavaScript array with the codes, this array will be filled with PHP, if the entered code is not in the array, the submit button will not submit the form.
Copy-paste next code in a PHP file and open it in your browser :
<html>
<head>
<script type="text/javascript">
function check_code () // ◄■ FUNCTION CALLED FROM THE FORM.
{ var arr = [ <?php // ▼ FILL JAVASCRIPT ARRAY WITH CODES FROM FILE ▼
$arr = explode( PHP_EOL,file_get_contents('coderoute.txt') );
echo "'" . implode( "','",$arr ) . "'";
?> ];
var code = document.getElementById("code"); // ◄■ FIELD IN FORM.
if ( arr.indexOf( code.value ) == -1 ) // ◄■ SEARCH CODE IN ARRAY.
{ alert("Code not found.");
return false; // ◄■ FORM WILL NOT BE SUBMITTED.
}
return true; // ◄■ FORM WILL BE SUBMITTED.
}
</script>
</head>
<body>
<form action="somescript.php" onsubmit="return check_code()"> <!-- ◄■ JS FUNCTION -->
<input type="text" id="code" name="code"/> <!-- ◄■ CODE FIELD -->
<br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
Notice how the JavaScript array is filled with PHP, the PHP script reads the codes from the file and echo them as the array items of the JavaScript array. Right click the page to see the source code and watch how the JavaScript array was filled.
I'm new to PHP and I'm trying to handle a form so I added a if condition to check if the submit button was pressed and if the other elements in the form are set. For some reason it causes the page to not load (it loads, but shows a blank page) so I can't actually submit anything..
When I tried removing the php code, it loaded fine. What am I doing wrong here?
<body>
<?php
if(isset(filter_input(INPUT_POST, 'aglogin')) && isset(filter_input(INPUT_POST, 'agname')) && isset(filter_input(INPUT_POST, 'agpass')))
{
echo 'Submitted..';
}
?>
<form method="post">
Username: <input type="text" id="agname" name="agname"/>
<br>
Password: <input type="password" id="agpass" name="agpass"/>
<br>
<input type="submit" id="aglogin" name="aglogin" value="Login">
</form>
</body>
Do not use isset(); because filter_input() returns true if variable is set and return false if it is not set.
<body>
<?php
if(filter_input(INPUT_POST, 'aglogin') && filter_input(INPUT_POST, 'agname') && filter_input(INPUT_POST, 'agpass'))
{
echo 'Submitted..';
}
?>
<form method="post">
Username: <input type="text" id="agname" name="agname"/>
<br>
Password: <input type="password" id="agpass" name="agpass"/>
<br>
<input type="submit" id="aglogin" name="aglogin" value="Login">
</form>
You could try this
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){
$valid=( array_key_exists('aglogin',$_POST ) && array_key_exists('agname',$_POST ) && array_key_exists('agpass',$_POST ) ) ? true : false;
if( $valid ) echo "Form submitted successfully";
}
?>
For some reason I can't get the text input from the form. I'm successfully able to get the file.
<form action="upload.php" method="post" enctype="multipart/form-data" id="msform">
<fieldset>
<h2 class="fs-title">Upload Photo</h2>
Select Image: <input type="file" name="image">
Photo Name: <input type="text" name="photoName">
Photo Name2: <input type="text" name="photoName">
<input type="submit" value="Upload Image" name="submit" class="submit action-button">
</fieldset></form>
This is my code to get the form data:
if ( isset( $_FILES['image'] ) ) {
// save file to Parse
$file = ParseFile::createFromData( file_get_contents( $_FILES['image']['tmp_name'] ), $_FILES['image']['name'] );
$file->save();
$foo = $_POST['photoName'];
echo $foo;
$foo does not display. I've tried using $_GET and $_REQUEST
Your second input overrides the first. Try to make a:
var_dump($_POST);
at the beginning of the php script, so you will see what is arriving as a parameter
I have a form where you can send an email to someone, if i reload the page it sends anouther email. How do I make it so it does not do that? A forum said to change it to GET, that did not work either. mail function is not showing up.
<form action="index.php" method="GET">
<textarea name="comment" cols="16.5" rows="5" style="resize: none;" maxlength="240"> </textarea>
<input type="submit" value="Send Comment" />
<?php
if (isset($_GET['comment'])) {
$com = mysql_real_escape_string($_GET['comment']);
if (!empty($com)) {
if (strlen($com) <= 240 && strlen($com) >= 5) { $mail = mail('__________', 'Comments', $com);
}
}
}
?>
</form>
Send your form with POST to another page: say upload.php, then in upload.php you add this code after you have checked the form:
header( 'Location: form_page.php' );
that way the form page can be reloaded without having this issue.
Add a GET parameter (say for eg: sent) to the link (index.php) and check if it is not set before sending the mail.
<form action="index.php?sent=true" method="GET">
<textarea name="comment" cols="16.5" rows="5" style="resize: none;" maxlength="240"></textarea>
<input type="submit" value="Send Comment" />
<?php
if (!isset($_GET['sent'])) {
// Your code for sending the mail here...
}
?>
</form>