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.
Related
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!!
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.
Explanation
I have a plain HTML form with a few fields
I post it to my controller function
In the function I var_dump the POST data
public function actionReceive()
{
echo "<pre>";
var_dump($_POST);
echo "</pre>";
exit();
}
On screen php shows the POST data is empty
In Firebug I can see the POST data
Question
Why is the POST data not displayed in the var_dump?
When I post it directly to a view file in site/page the POST is displayed with var_dump.
why are you using var_dump($_POST) ??
If you want to pass data from one page to another in PHP just create 2 files. Let's sets first file name as send.html, and another one as receive.php.
Now put these codes into send.html file:
<form method="post" action="catching-var.php">
1. <input type="text" name="name1"/><br/>
2. <input type="text" name="name2"/><br/>
3. <input type="text" name="name3"/><br/>
4. <input type="text" name="name4"/><br/>
<input type="submit" name="submit"/>
</form>
Also put these lines into receive.php file:
<?php
$name0 = $_POST['name0'];
$name1 = $_POST['name1'];
$name2 = $_POST['name2'];
$name3 = $_POST['name3'];
$name4 = $_POST['name4'];
echo $name0.'<br/><br/>';
echo $name1.'<br/><br/>';
echo $name2.'<br/><br/>';
echo $name3.'<br/><br/>';
echo $name3.'<br/><br/>';
?>
Put some values into the HTML textbox fields and click the Submit button.
EDIT: If you do this in Yii Framework all of these operations and codes are same. But you must paste the PHP codes in action. That's all.
Please tell me; in which step you have problem?
Thanks. Best regards.
I have a web site that allows people to upload a csv file and then it loads it into a postgres database. uploading the file is fine and i capture the file name and location ../Data/Uploads/mycsv.csv as $_POST['fname'].
I'm trying to use this variable in $file=file($_POST['fname']) but cant get it to work however if i hard code it in as $file=file("../DATA/Uploads/mycsv.csv") it works. I have attached the code in question. Thanks in advance for any help
Also to clarify echo $_POST['fname']; returns ../DATA/Uploads/mycsv.csv, which is the same as the hard coded value.
please bear with me as im only relatively new to this. I have attached the 2 html forms being used as well. the top one passes the $fname variable containing the file name and path from the php code used to upload the file.
<Form Method="post" Action="../PHP/Loadcsv.php">
<input type="text" value="<?php echo htmlspecialchars($fname);?>" name="fname">
<br />
<Input Type="submit" Value="Continue">
</Form>
this is the php copy the csv into the database
<?PHP
if ($_POST['submit']) {
$file = file(printf($_POST['fname'])); //****doesnt work******
//$file = file("../DATA/Uploads/csv_trial1.csv"); //********This works******
$db = pg_connect("host=localhost dbname=blah user=me password=you");
pg_exec($db, "COPY personaldetails FROM stdin");
foreach ($file as $line) {
$tmp = explode(",", $line);
pg_put_line($db, sprintf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", $tmp[0], $tmp[1], $tmp[2], $tmp[3], $tmp[4], $tmp[5], $tmp[6], $tmp[7]));
}
pg_put_line($db, "\\.\n");
pg_end_copy($db);
?>
below is the html to run the above php.
<form id='form' method='post' action='' >
<input type="submit" name="submit" />
</form>
after running a whole lot of echo to find where the variable is reaching, i dont think it is reaching the inside of the if statement possibly due to the next use of post??
**update**
So after a little playing and bouncing ideas almost literally off my office walls.... i was on the right track and Devon was right too, my problem was the 2 post requests the answer was to have a php variable $filename = $_POST['fname']; to take the variable from the first form and put this into the input for the second form
<form id='form' method='post' action='' >
<input type="hidden" value="<?php echo htmlspecialchars($filename);?>" name="fname">
<input type="submit" name="submit" />
I'm sure there may be other ways to achieve this but at the moment it works.
I'm not sure where you came up with printf(), but any print or echo command will output the arguments to the browser and won't return it to the function at hand. You don't need to use anything special to use a variable as an argument. Just: file($_POST['fname']);
Printf specifically outputs a formatted string and returns the length of the string. So this is the equivalent of calling file(integer) where integer is the length of $_POST['fname']'s value.
I currently have an HTML file, with a form in it, that when submitted POSTs the form & calls a simple short PHP file that calls a function within another PHP file using the POSTed variables as parameters. The files are both below. What I am wondering is whether I can somehow skip the middleman PHP file, and simply call the function from my HTML file.
Ideally, this would set the call to the function:
insert_PE(new PE($_POST[Date],$_POST[Participant],$_POST[Time],$_POST[Result],$_POST[Notes]));
as the form action. Does anyone know how/if this can be achieved?
HTML:
<FORM ID="FORM1" METHOD="POST" AUTOCOMPLETE="off" ACTION = "writeToDL.php">
<INPUT TYPE="hidden" NAME="Date" STYLE="WIDTH:0px; " MAXLENGTH="8" TITLE="Enter Date" Value="<?php $dt = date('Y-m-d'); echo $dt ?>"/>
<INPUT TYPE="text" NAME="Time" STYLE="WIDTH:70px; " MAXLENGTH="7" ONCHANGE="validateTime();" />
<SELECT NAME = "Result">
<OPTION VALUE = OK></OPTION>
<OPTION VALUE = C>C</OPTION>
</SELECT>
<SELECT NAME = "Participant" STYLE = "WIDTH: 187">
<OPTION SELECTED VALUE = "">Select...</OPTION>
<?PHP
$allParticipants = getall_participants();
foreach($allParticipants as &$value) {
$val = $value->get_id();
echo "<OPTION VALUE='",$val,"'>";
echo $value->get_first_name()," ",$value->get_last_name();
echo "</OPTION>";
}
?>
</SELECT>
<TEXTAREA NAME='Notes' COLS='28' ROWS='5'></TEXTAREA>
<INPUT TYPE="image" SRC = "images/submit.png" VALUE="Submit Participant"/>
</FORM>
PHP File:
<?php
include_once('database/PE.php');
insert_PE(new PE($_POST[Date],$_POST[Participant],$_POST[Time],$_POST[Result],$_POST[Notes]));
?>
You COULD do something like this:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
include_once('database/PE.php');
insert_PE(new PE($_POST['Date'],$_POST['Participant'],$_POST['Time'],$_POST['Result'],$_POST['Notes']));
} ?>
<html>
... rest of your page here ...
</html>
That way the PHP code only fires if an POST was actually performed. Some would suggest checking for the presence of a form field, but that's unreliable - you might change the form down the road and forget to update the if(). Checking the request method is guaranteed to be 100% reliable.
What I am wondering is whether I can somehow skip the middleman PHP file, and simply call the function from my HTML file.
No. The client only knows about URIs.
A URI can map to a PHP program. Multiple URIs can map to the same PHP program. You can use logic to determine what functions to run for a given URI. You can't avoid having that logic in your program.
One option is to put method="_the_main_php_file_containing_function_to_be_called_"
I hope it works fine.
I think you could use a hidden field on the form, and populate it with the name of the function you want to run on "destination.php". Then a switch statement on "destination.php" could pull the name of the function from POST variable.