Question

  • Creator
    Topic
  • #4213414

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

    by apn72 ·

    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

You are posting a reply to: How to use session variables to in properties of HTML form input

The posting of advertisements, profanity, or personal attacks is prohibited. Please refer to our Community FAQs for details. All submitted content is subject to our Terms of Use.

All Answers

  • Author
    Replies
    • #4213418

      Background-color is now working

      by apn72 ·

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

      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.

      • #4213447
        Avatar photo

        Re: disabled

        by kees_b ·

        In reply to Background-color is now working

        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.

    • #4213813

      . Here’s a revised HTML line

      by hassaansiddiqui200215 ·

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

      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.

Viewing 1 reply thread