Empty $_POST in Wordpress Archive Page - php

I have a custom form in my wordpress theme archive.php file but I can't get the post of that form. It's empty.
I have these:
<?php print_r($_POST); ?>
<div class="filtros">
<h3>Búsqueda de documentos</h3>
<form action="#" method="post">
<input type="text" id="name" name="f_name" placeholder="Buscar" value="<?php echo $_POST['f_name']; ?>" />
<div class="dates">
<input type="text" id="inicio" class="date" name="f_inicio" placeholder="Fecha de inicio" value="<?=$_POST['f_inicio']?>" /> /
<input type="text" id="final" class="date" name="f_final" placeholder="Fecha final" value="<?=$_POST['f_final']?>" />
</div>
<div class="submit"><input type="submit" value="Buscar" /></div>
</form>
</div>
Even if I pass the variables throw the URL I cant get that vars with $_GET.
¿Any idea?

While it's not advisable from a security point of view, you can change the method on your form from method="post" to method="get" to have variables posted as $_GET variables instead.

Change action="#" to action="<? echo $_SERVER['REQUEST_URI']?>"
Try change <?= to <? echo

You may need to point the action="myfile.php" to specific file.. See the solutions here:
$_POST returns empty on form submit in wordpress

Related

Unable to keep data in form after submitting

I've been searching online on how to keep data in the form after submitting it. But after trying for awhile it still doesn't work as expected
This is the code that I tried:
<form action="process_login" method="post" target="_self">
<div class="login-field">
<input type="text" id="login-email-field" name="login-email-field" value="<?php echo isset($_POST["login-email-field"]) ? $_POST["login-email-field"] : ''; ?>" required />
<label class="login-email-label" for="login-email-field">Email/Username</label>
</div>
<div>
<button class="submit-button" type="submit">
Login
</button>
</div>
</form>
I also tried replacing the input (with a 'TEST' string in the value field if the POST is empty) but the 'TEST' string did not appear after submitting the form.
<input type="text" id="login-email-field" name="login-email-field" value="<?php echo isset($_POST["login-email-field"]) ? $_POST["login-email-field"] : 'TEST'; ?>" required />
Any help would be appreciated thanks!
Action specifies a URL to which the form’s data is sent when submitted.
If you want to stay on the same page you can leave it out
<form method="post" target="_self">
or set the name of the actual page.
<form action="actualPage.php" method="post" target="_self">

PHP POST does not redirect properly

I have two different sites and want to pass data via a form from the first to the second page.
The form looks as follows (simplified):
<form method="POST" action="https://othersite.me/login.php">
<label class="title">
E-Mail
</label>
<input name="luser" type="email">
<label class="title">
Password
</label>
<input name="lpasswd" type="password">
<input type="submit" value="Login">
</form>
The php code othersite.me/login.php looks as follows:
<?php
$email=Format::input($_POST['luser']?:$_GET['e']);
$passwd=Format::input($_POST['lpasswd']?:$_GET['t']);
?>
<form method="POST" action="login.php">
<label class="title">
E-Mail
</label>
<input name="luser" type="email" value="<?php echo $email; ?>">
<label class="title">
Password
</label>
<input type="password" value="<?php echo $passwd; ?>">
<input type="submit" value="Submit">
</form>
Now I expect that when I enter some data on the first page, the data is transferred to otherpage.me/login.php and displayed in the appropriate fields.
Curiously, after pressing the submit button, the website is redirected to othersite.me/login.php for less than a second and then automatically to othersite.me/index.php.
If I use GET instead of POST, the form works as expected and stays on othersite.me/login.php.
How is this possible and how can I fix that. Do you have some ideas?
Why do you need two php file to do this? You can use action="<?php echo $_SERVER['PHP_SELF']; ?>" in your login.php and start processing the submitted data there. You can follow this instruction
I don't have reputation to comment yet so I will answer.
In the othersite.me/login.php if you have permission paste the code below
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
This will return all warnings and fatal errors

Filling form data by using url extension; Why does this not work?

I have been trying to pre-fill the subject input with information generated in another page, but have been having difficulties with it despite reading a lot of resources about it and seeing examples. I have tried a variety of links, including my most recent attempt with http://www.myurl.com/folder/index.php/contactform?subject=test, but even that doesn't work. Anybody have any advice? Also, if you want to test it out before answering, the page experiencing the problem is the contact page of this website. I've removed information from below to make it more general. Thanks in advance for any and all of the help.
<form id="contactform" method="post">
<input name="recipient" type="hidden" value="myemail" />
<input name="subject" type="hidden" value="Contacter" />
<p id="contactname">
<label>Name:</label>
<input name="name" type="text" />
</p>
<p id="contactemail">
<label>Email:</label>
<input name="email" type="text" />
</p>
<p id="title">
<label>Subject:</label>
<input name="title" type="text" />
</p>
<p id="contactmessage">
<label>Message:</label>
<textarea name="message"></textarea>
</p>
<p id="submit">
<input type="button" value="Send" />
</p>
<input name="redirect" type="hidden" value="myredirectpage" />
</form>
Lets say your page URL is some thing like below
http://www.example.net/index.php?var1=Something&var2=10&var3=ok
You can use $_GET to get the values of var1, var2,and var3 from the above url
In index.php use the below code to fetch url data
echo $_GET['var1'] // Something
echo $_GET['var2'] // 10
echo $_GET['var3'] // ok
Go through this link http://php.net/manual/en/reserved.variables.get.php
With php you can do this with a session.
On the other page(not the form) you can do $_SESSION['subject'] = 'your subject';
On the form page you can acces this cookie ( make sure you have started the session on top of the page with session_start():
<p id="title">
<label>Subject:</label>
<input name="title" type="text" value="<?= $_SESSION['subject'] ?>"/>
</p>

Showing form data after submit

I have a form with a submit button and a handler that stores data in the database. Problem is when the form is submitted, all data is cleared from the input fields. Is there a way to still show them after submit? What changes do I need to make to my form_submit function?
function mymodule_form_submit($form, &$form_state) {
//how to retain the input in the form
}
I'm looking for the most "drupalish" way to get this done?
As indicated by this previous StackOverflow question you can accomplish this with $form_state['storage'] and $form_state['rebuild'].
You can access the data using $_REQUEST['form_variable_name'] where form_variable_name is the name of the html input tag.
You then need to render the page back putting this value into the input tags value field.
<form method="POST" action="/account/contactdetails/">
<div>
<label>First name:</label>
<input type="text" name="firstname" value="<?php echo $_REQUEST['firstname']; ?>" />
</div>
<div>
<label>Last name:</label>
<input type="text" name="lastname" value="<?php echo $_REQUEST['lastname']; ?>" />
</div>
<input type="submit" value="Save" />
</form>

How to access the form's 'name' variable from PHP

I'm trying to create a BMI calculator. This should allow people to use either metric or imperial measurements.
I realise that I could use hidden tags to solve my problem, but this has bugged me before so I thought I'd ask: I can use $_POST['variableName'] to find the submitted variableName field-value; but...I don't know, or see, how to verify which form was used to submit the variables.
My code's below (though I'm not sure it's strictly relevant to the question):
<?php
$bmiSubmitted = $_POST['bmiSubmitted'];
if (isset($bmiSubmitted)) {
$height = $_POST['height'];
$weight = $_POST['weight'];
$bmi = floor($weight/($height*$height));
?>
<ul id="bmi">
<li>Weight (in kilograms) is: <span><?php echo "$weight"; ?></span></li>
<li>Height (in metres) is: <span><?php echo "$height"; ?></span></li>
<li>Body mass index (BMI) is: <span><?php echo "$bmi"; ?></span></li>
</ul>
<?php
}
else {
?>
<div id="formSelector">
<ul>
<li>Metric</li>
<li>Imperial</li>
</ul>
<form name="met" id="metric" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="form/multipart">
<fieldset>
<label for="weight">Weight (<abbr title="Kilograms">kg</abbr>):</label>
<input type="text" name="weight" id="weight" />
<label for="height">Height (<abbr title="metres">m</abbr>):</label>
<input type="text" name="height" id="height" />
<input type="hidden" name="bmiSubmitted" id="bmiSubmitted" value="1" />
</fieldset>
<fieldset>
<input type="reset" id="reset" value="Clear" />
<input type="submit" id="submit" value="Submit" />
</fieldset>
</form>
<form name="imp" id="imperial" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="form/multipart">
<fieldset>
<label for="weight">Weight (<abbr title="Pounds">lbs</abbr>):</label>
<input type="text" name="weight" id="weight" />
<label for="height">Height (Inches):</label>
<input type="text" name="height" id="height" /
<input type="hidden" name="bmiSubmitted" id="bmiSubmitted" value="1" />
</fieldset>
<fieldset>
<input type="reset" id="reset" value="Clear" />
<input type="submit" id="submit" value="Submit" />
</fieldset>
</form>
<?php
}
?>
I verified that it worked (though without validation at the moment -I didn't want to crowd my question too much) with metric; I've added the form but not the processing for the imperial yet.
To identify the submitted form, you can use:
A hidden input field.
The name or value of the submit button.
The name of the form is not sent to the server as part of the POST data.
You can use code as follows:
<form name="myform" method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="frmname" value=""/>
</form>
You can do it like this:
<input type="text" name="myform[login]">
<input type="password" name="myform[password]">
Check the posted values
if (isset($_POST['myform'])) {
$values = $_POST['myform'];
// $login = $values['login'];
// ...
}
The form name is not submitted. You should just add a hidden field to each form and call it a day.
In the form submitting button (id method of form is post):
<input type="submit" value="save" name="commentData">
In the PHP file:
if (isset($_POST['commentData'])){
// Code
}
For some reason, the name of the submit button is not passed to the superglobal $_POST when submitted with Ajax/jQuery.
Use a unique value on the submit button for each form like so
File index.html
<form method="post" action="bat/email.php">
<input type="text" name="firstName" placeholder="First name" required>
<input type="text" name="lastName" placeholder="Last name" required>
<button name="submit" type="submit" value="contact">Send Message</button>
</form>
<form method="post" action="bat/email.php">
<input type="text" name="firstName" placeholder="First name" required>
<input type="text" name="lastName" placeholder="Last name" required>
<button name="submit" type="submit" value="support">Send Message</button>
</form>
File email.php
<?php
if (isset($_POST["submit"])) {
switch ($_POST["submit"]) {
case "contact":
break;
case "support":
break;
default:
break;
}
}
?>
As petervandijck.com pointed out, this code may be susceptible to XSS attacks if you have it behind some kind of log-in system or have it embedded in other code.
To prevent an XSS attack, where you have written:
<?php echo "$weight"; ?>
You should write instead:
<?php echo htmlentities($weight); ?>
Which could even be better written as:
<?=htmlentities($weight); ?>
You can use GET in the form's action parameter, which I use whenever I make a login/register combined page.
For example: action="loginregister.php?whichform=loginform"
I had a similar problem which brought me to this question. I reviewed all the preceding answers, but ultimately I ending up figuring out my own solution:
<form name="ctc_form" id="ctc_form" action='' method='get'>
<input type="hidden" name="form_nm" id="form_nm">
<button type="submit" name="submit" id="submit" onclick="document.getElementById('form_nm').value=this.closest('form').name;">Submit</button>
</form>
It seamlessly and efficiently accomplishes the following:
Passes the form name attribute via a hidden input field, without using the fallible value attribute of the submit button.
Works with both GET and POST methods.
Requires no additional, independent JavaScript.
You could just give a name to the submit button and do what needs to be done based on that. I have several forms on a page and do just that. Pass the button name and then if button name = button name do something.
Only the names of the form fields are submitted, but the name of the form itself is not. But you can set a hidden field with the name in it.

Categories