1

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?

Jimmy
  • 12,087
  • 28
  • 102
  • 192

1 Answers1

1

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.

miken32
  • 42,008
  • 16
  • 111
  • 154
  • Thank you for the reply. So when you say my code is looking for $data;, does that mean I should change my line to this? ```$data = $_POST['data'];``` instead rather than ```$textdata = $_POST['data'];``` – Jimmy Mar 01 '17 at 18:35
  • `$_POST` is an array that's filled when you submit an HTTP POST request. There is no such request happening here. See http://stackoverflow.com/questions/679013/get-vs-post-best-practices – miken32 Mar 01 '17 at 18:37
  • I don't see how it's a roundabout away, since I would rather not save to disk and to I have to call my function as part of the image – Jimmy Mar 01 '17 at 20:40
  • I was just referring to the three files and how you're passing data back and forth between them, but you're right it's unnecessary commentary. Does it make sense re GET vs POST? – miken32 Mar 01 '17 at 20:55