LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PHP code did not create a cookie and it did not changed the background and text color (https://www.linuxquestions.org/questions/programming-9/php-code-did-not-create-a-cookie-and-it-did-not-changed-the-background-and-text-color-211023/)

Linh 07-29-2004 09:31 AM

PHP code did not create a cookie and it did not changed the background and text color
 
Initially, the code load the page, and default the background and text color to white. I then select a different background and text color, and I clicked the submit button. The page refreshes but the background and text color was not changed, and it never created a cookie on my PC.

On IE 6.0, I set the cookies option to prompt
I selected Tools --> Internet Options --> Privacy -->
Advanced --> set the First-Party Cookies to prompt

=================================
Code:

<?PHP

if ($BeenSubmitted)
 {
  setcookie("BGColor", "$NewBGColor");
  setcookie("TextColor", "$NewTextColor");
  $BGColor = $NewBGColor;
  $TextColor = $NewTextColor;
 }
else
 {
  if (!$BGColor)
    $BGColor = "WHITE";

  if (!$TextColor)
    $TextColor = "BLACK";
 }
?>

<HEAD>
<TITLE>User Customization</TITLE>
</HEAD>


<?
  print ("<BODY BGCOLOR=$BGColor TEXT=$TextColor>\n");
?>


Currently your page looks like this!
<FORM ACTION="linh_cookies.php" METHOD=POST>

Select a new background color:
<SELECT NAME="NewBGColor">
  <OPTION VALUE=WHITE>WHITE</OPTION>
  <OPTION VALUE=BLACK>BLACK</OPTION>
  <OPTION VALUE=BLUE> BLUE </OPTION>
  <OPTION VALUE=RED>  RED  </OPTION>
  <OPTION VALUE=GREEN>GREEN</OPTION>
</SELECT>

Select a new text color:
<SELECT NAME="NewTextColor">
  <OPTION VALUE=WHITE> WHITE</OPTION>
  <OPTION VALUE=BLACK> BLACK</OPTION>
  <OPTION VALUE=BLUE>  BLUE </OPTION>
  <OPTION VALUE=RED>  RED  </OPTION>
  <OPTION VALUE=GREEN> GREEN</OPTION>
</SELECT>

<INPUT TYPE=HIDDEN NAME=BeenSubmitted VALUE=TRUE>
<INPUT TYPE=SUBMIT NAME="SUBMIT" VALUE="Submit!">

</FORM>
</BODY>
</HTML>


coolman0stress 07-29-2004 10:22 AM

Use the global arrays to check for values instead.

These are $_POST, $_GET, $_REQUEST, $_SESSION, etc.

For example, to get the value from a post form use $_POST['NewBGColor']. ($_GET for get forms or just use $_REQUEST which checks them all).

Code:

if(isset($_POST['BeenSubmitted'])) {
  ...
  $BGColor = $_POST['NewBGColor'];
}


gizmo_thunder 07-29-2004 11:33 AM

Yep the above code is correct :), well its'
the new feature they have added in php4 and above
you better check out the version of php you are using
and check out its' documentation.

coolman0stress 07-29-2004 11:37 AM

Yeah, i should've mentioned that the original code is "correct", but some configurations (and new versions) of php don't automatically set the variables ($NewBGColor from $_POST['NewBGColor']) so it's best to use the new method instead. :)

Linh 07-29-2004 12:23 PM

reply
 
Thank you everyone for your help.

The function setcookie("TextColor", "$NewTextColor"); did not put a cookie into my PC.


Is the statement with $_POST is from version 3 or version 4 ?
$BGColor = $_POST['NewBGColor'];

gizmo_thunder 07-29-2004 10:15 PM

i think its' from version 4
thats' what i know :)


All times are GMT -5. The time now is 12:35 PM.