Question

  • Creator
    Topic
  • #4213414

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

    Locked

    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

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.

    • #4219723

      Reply To: How to use session variables to in properties of HTML form input

      by alexwat231 ·

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

      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.

Viewing 2 reply threads