Question Locked

How to use session variables to in properties of HTML form input

0 helpful
6 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

In the PHP plugin file, I'm setting two session variables:
$_SESSION['snameclr'] ="black";
$_SESSION['sNameEnab'] =False;

I want to use these variables in an HTML form:
Snameclr will be the background of an element;
sNameEnab specifies whether the input is enabled.

This is the HTML line defining the input:
<td><input type="text" name="sname" id="sname" maxlength="25" value="<?PHP
echo $_SESSION['sname'];?>" DISABLED required
style="background-color<?php $_SESSION['snameclr']?>
;"

The WordPress page says “unexpected token "}" which I presume because the use of snameclr is ioncorrect; how can I correct this?
How can I use sNameEnab to set whether the input is enabled? Thanks for any ideas

6 replies

by apn72 edited

I changed the line to:
<td><input type="text" name="sname" id="sname" maxlength="25" value="<?PHP
echo $_SESSION['sname'];?>" DISABLED required
style="background-color:<?php echo $_SESSION['snameclr'];?>
"></td>
Background colour now working but am still puzzled about setting enabled/disabled.

by kees_b edited

Since you write it's DISABLED, it's disabled. And $_SESSION[‘sNameEnab’] (which is set to false) isn't used at all in the code. So it stays disabled.

And in the code you use $_SESSION[‘sname’], but you don't tell about its setting.

by apn72

sname is the input that want to be enabled or disabled, dependant upon sNameEnab. I can't what to replace the "DISABLED" keyword with to achieve that. I tried DISABLED=<?php echo $_SESSION[‘sNameEnab’];?> but it doesn't work.

by kees_b edited

Input fields can have the attribute "disabled". Then they are disabled. There's no attribute "enabled" because that's the default. So to enable it, leave out the "disabled".

https://www.w3schools.com/tags/att_disabled.asp tells to write a javascript to remove it dynamically.
https://www.w3schools.com/JSREF/met_element_removeattribute.asp shows how to do that.

Let me add that the combination disabled (you can't fill it) and required (you must fill it) seems strange.

by hassaansiddiqui200215 edited

To use the session variables in your HTML form, you need to correct the way you're setting the background color and enabling the input field. Here's a revised HTML line:

<td><input type="text" name="sname" id="sname" maxlength="25" value="<?php echo $_SESSION['sname']; ?>" <?php echo ($_SESSION['sNameEnab'] ? '' : 'disabled'); ?> style="background-color: <?php echo $_SESSION['snameclr']; ?>;">

In this code, we use a conditional statement (<?php echo ($_SESSION['sNameEnab'] ? '' : 'disabled'); ?>) to check the value of sNameEnab and determine whether the input should be enabled or disabled. We also corrected the style attribute to properly set the background color. This should resolve the "unexpected token '}'" error and correctly use the session variables in your HTML form.

by alexwat231

It looks like you're encountering a syntax error due to the incorrect usage of the PHP session variable $_SESSION['snameclr'] in your HTML code. Additionally, you're missing the echo statement to output the value of $_SESSION['snameclr'] in the HTML attribute.

To fix the issue, you need to add echo before $_SESSION['snameclr'] and ensure that the HTML attribute style is properly formatted. Here's the corrected HTML line:

`html
<td><input type="text" name="sname" id="sname" maxlength="25" value="<?php echo $_SESSION['sname']; ?>" <?php echo $_SESSION['sNameEnab'] ? '' : 'disabled'; ?> required style="background-color: <?php echo $_SESSION['snameclr']; ?>;">
`

This line will set the background color of the input element based on the $_SESSION['snameclr'] variable and enable or disable the input based on the $_SESSION['sNameEnab'] variable. Make sure that these session variables are properly set before using them in the HTML code.

This discussion is closed to new replies.