0

I have read several tutorials to try and get my head around this problem. I have my code working fine with the text input box, but I'm trying to pass the radio box to my function as well and use it to get my array value.

I will implement things like form validation and sanitisation later, but now I just want to be able to output the array result within my function, depending on what radio button was selected. This is the code I have tried:

index.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
<input name="colour" type="radio" value="2">Blue
<input name="colour" type="radio" value="3">Green
<input type="submit" name="submit" value="Submit">
</form>
 <img src="pngfile.php?data=<?php print urlencode($data);?>">

pngfile.php

<?php
    require_once 'functions.php';  
   $textdata = urldecode($_GET['data']);
   $colourdata = urldecode($_GET['colour']);
   process($textdata,$colourdata);
   exit;
?>

functions.php

<?php

/* Generate Image */
function process($textdata, $colourdata)
{

$colour_array = [
    "1" => "#9E2A2B",
    "2" => "#3E5C76",
    "3" => "#335C67",
];
...

I am stuck with the piece of the code that will take the radio button value (1/2/3) and then look up the equivalent array value and output the colour, i.e: if radio button with value 1 is selected, then we output 1#9E2A2B.

3
  • 1
    GET != POST.... as $colourdata = urldecode($_GET['colour']); never gets through. Commented Feb 28, 2017 at 21:58
  • Are you refering to the code in the pngfile.php? If I change it to post it doesn't work, but it does with get Commented Feb 28, 2017 at 22:02
  • I edited my comment; use error reporting. Commented Feb 28, 2017 at 22:03

1 Answer 1

1
<form action="/app.php" method="post">

form method POST not GET

$_GET['colour'] edit to $_POST['colour']

in function

function process($textdata, $colourdata)
{

$colour_array = [
    "1" => "#9E2A2B",
    "2" => "#3E5C76",
    "3" => "#335C67",
];

$color = isset($colour_array[$colourdata]) ? $colour_array[$colourdata] : false;
...
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the reply, should this line be post too? $textdata = urldecode($_GET['data']);
@Jimmy: Yes for all the fields of this form since you have chosen the method POST here for all the form: <form action="/app.php" method="post">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.