0

I have a form made in the previous page. In the form, I have a checklist with the options: red, green, blue.

If the user selects red, the next page's background color should be red. If green is selected, then the background color will be green. I am using PHP to do this.

Previous page

<form action="next_page.php method="POST>
  <input type="radio" name="color" value="red">
  <input type="radio" name="color" value="green">
  <input type="radio" name="color" value="blue">
</form>

Next Page

<?PHP 
   $radioVal = $_POST["color"]
 ?>

 <style>
   .body{
     //what goes here
  }
 </style>
Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100

2 Answers2

0

Try this:

<?PHP 
$radioVal = $_POST["color"];

echo "
 <style>
   .body{
     background-color:" . htmlspecialchars($radioVal) . "
  }
 </style>";
 ?>
John
  • 5,132
  • 1
  • 6
  • 17
  • You might add htmlspecialchars or another sanitizer. Also it's good to make if-else statement only for these color values.Your example works, but it's very easy for XSS Injection. – Kristiyan Jun 01 '20 at 23:13
  • @Kristiyan Thanks for the tip. I added the htmlspecialchars to make it more safe for the OP. – John Jun 01 '20 at 23:16
0

In no particular order:

  • Use AJAX to get the data you need from the server.
  • Echo the data into the page somewhere, and use JavaScript to get the information from the DOM.
  • Echo the data directly to JavaScript.

Check out this comment

moosylla
  • 131
  • 5
  • Oh, by reading the link I understood: these are 3 different possibilities to achieve this! You should be more explicit on that. And by their question I think they already have a way to transfer the data, but no clue how to include it. – Pauloco Jun 02 '20 at 17:39