Access to PHP $_GET and $_POST in wordpress - php

In a wordpress page I have a form where, after the pressure of submit botton, the same page is reloaded and it sends an email with the information in the form. The problem is that, as soon as I try to save the page, if in the php code there is a $_GET[],$_POST[] or $_REQUEST[] array, wordpress does not let me to save the changes, and the last code I wrote (those with $_variables) disappears. If I make an external php script and I call it in the wordpress page, I get "Internal Server Error" when I connect to the page.
I tried with two different php wp plugins, and I have the same problem in both.
Does anyone know why?
EDIT:
Here is an example of code I tried, it is the simpest example I found and it is still not working:
<?php
function test() {
echo $_POST["user"];
}
if (isset($_POST[])){ //If it is the first time, it does nothing
test();
}
?>
Here is the form:
<form action="" method="post">
<input type="text" name="user" placeholder="enter a text" />
<input type="submit" value="submit" onclick="test()" />
</form>
Anyway it is as if the wordpress parser recognizes the variables and refuses to save, if I wrote, for instance, only the following code `
<?php
$_GET[];
?>
it give me the same problem, but writing
<?php
$_HELLO[];
?>
though it returns error when I call the page in the browser (obviously considering that $_HELL does not exist), it let me save.
EDIT 2: Very strange, if I print all the post array with
`<?php
var_dump($_POST);
?>`
it works, and it prints "array(1) { ["user"]=> string(3) "123" }",
BUT IF I TRY TO ACCESS TO THE ELEMENT WITH THE KEY "USER" USING [] IT GIVES ME ERROR!

$_POST and $_GET are also work in wordpress too..
Remove the square bracket[ ] on $_POST, it will work.
<form action="" method="post">
<input type="text" name="user" placeholder="enter a text" />
<input type="submit" value="submit" onclick="test()" />
</form>
<?php
function test() {
echo $_POST["user"];
}
if (isset($_POST)){ //If it is the first time, it does nothing
test();
}
?>
Hope this will help you!!

Related

use $_GET['']; value inside $_POST[''] and submit it to the same page and display in the same page

i am new to php and while i am practing i came across a problem. actually,i have two files index1.php and index2.php. in index1.php i have a link with a unique id as
<a href="index2.php?companyid=<?php echo $row('company_id');?>>details</a>
i have got this value in index2.php as
if(isset($_GET['companyid'])){
$companyid = $_GET['companyid'];
}
now i have a search form in the index2.php as
<form method="POST" action="index2.php">
<input type="text" name="search">
<button type="submit" name="submit">submit</button>
</form>
now on button click i want the search results be displayed in the same page as
'index2.php?companyid=$companyid'
but some how if i try to use $_POST['submit']; in the same page it takes me to index2.php and instead of index2.php?companyid=$companyid and also it throws error as undefined index of $companyid if i don't use $_POST['submit']; and echo $companyid; it gives value and works fine. all i want is that to use $companyid' value inside ``$_POST['submit']; as and display the result in the same url as before
if(isset($_POST['submit']){
$companyid //throws an error index of company id
}
any help will be appreciated
First off, it looks like you are not using the company id in the form itself, so it will not be submitted as part of the the POST. You could possibly use:
<form method="POST" action="index2.php">
<?php if (isset($companyid)): ?>
<input type="hidden" name="companyid" value="<?= $companyid; ?>">
<?php endif; ?>
<input type="text" name="search">
<button type="submit" name="submit">submit</button>
</form>
But you would probably also need to change your logic to:
if(isset($_POST['companyid'])){
$companyid = $_POST['companyid'];
}else if(isset($_GET['companyid'])){
$companyid = $_GET['companyid'];
}
As Josh pointed out in the comments, PHP is not able to remember your previous GET request but this can easily be solved by altering the action attribute of the form element. By doing this you can pass on the previous data. This would look a little something like this:
<form method="POST" action="index2.php?companyid=<?php echo $companyid;?>">
<input type="text" name="search">
<button type="submit" name="submit">submit</button>
</form>
This way you will be redirected to index2.php with the URL parameters present and you will be able to retrieve both search and companyid using $_POST and $_GET or use $_REQUEST for both.

Send form elements to image generation function

I'm completely up against a wall with this. I've been trying to learn PHP from tutorials to output two form inputs to my function to generate an image.
I've fixed an issue with my mixing up POST and GET but now my script doesn't function at all and I cant understand why - I'm sure this is hilds play for any developer but I'm in a bit over my head.
This is my app.php
<?php
if (isset($_POST['submit'])) {
$data = $_POST['name']; // the data from text input.
$colour = $_POST['colour']; // data from colour radio.
}
?>
...
<form action="/app.php" method="post">
<input type="text" name="name"/>
<input name="colour" type="radio" value="1">Red<br>
<input name="colour" type="radio" value="2">Blue<br>
<input name="colour" type="radio" value="3">Green<br>
<input type="submit" name="submit" value="Submit">
</form>
<img src="pngfile.php?data=<?php print $data;?>" alt="">
Calls pngfile.php
<?php
require_once 'functions.php'; // Requires and includes do not need brackets.
$textdata = $_POST['data'];
$colourdata = $_POST['colour'];
process($textdata,$colourdata);
exit;
?>
Which in turn calls functions.php
<?php
/* Generate Image */
function process($textdata, $colourdata)
{
...
All of this was working perfectly before but the only change I have added is updating all elements to use POST and also adding in the code across the three files to add the selected colour to post. However with this tweaked code, I get no image output, even though I know my main image function works fine, so it must be my app.php and pngfile.php at fault.
Can anyone please give me some guidance on where I am going wrong?
Your problem is that you're sending this:
<img src="pngfile.php?data=<?php print $data;?>" alt="">
But your code is looking for this:
$textdata = $_POST['data'];
$colourdata = $_POST['colour'];
There's no post, and there's certainly no $_POST['colour']. There is a $_GET['data'] however; I think that's what you're looking for. Data passed as part of the URL is part of a GET request, and is available in $_GET. $_POST is for data sent with a POST request.

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.

PHP $_POST give empty value

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/>";
}
?>

Script doesnt put information

I have written a script in php to replace in newtopic button in phpbb3
in other question, a user says me this:
In your submit.php, you can retrieve the forum ID using $_GET['f']. Now, to pass it on to application.php, you can use a hidden input field:
<form method="post" action="application.php" accept-charset="utf-8" >
$id = htmlspecialchars($_GET['f']);
<input type="hidden" name="forum_id" value="<?php echo $id; ?>"/>
When you click on the submit button, the forum ID value will also get POSTed, and you'll be able to retrieve it in application.php code using the $_POST['forum_id'].
and my code goes as here:
<form method="post" action="application.php" accept-charset="utf-8" >
$id = htmlspecialchars($_GET['f']);
<input type="hidden" name="forum_id" value="<?php echo $id; ?>"/>
.............
<fieldset class="submit-buttons">
<input value="Submit" class="button2" type="submit">
</fieldset>
This code is embedded in submit.php to use phpbb3 template.
and application.php goes as here
So I click on new topic button, and I redirect to submit.php?mode=post&f=3 and in that php there is embedded the html, the problem is that with the solution, I receive the next error:
"The forum you selected does not exist" and the addresswar goes as: viewforum.php?f=&sid=a69fb9f491d2adc11c4be3a6dac02774
so I think that forum_id (in thos case is "3" (&f=3) is not correctly sent throught php scripts
I would appreciate some help
You need to add $id = htmlspecialchars($_GET['f']); inside the <?php ?> tag,
<?php $id = htmlspecialchars($_GET['f']); ?>

Categories