Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts

    Add class to an element if user has submitted form

    I was wondering how I would add a class to an element if a user has submitted a form. I set the form to:

    PHP Code:
    <form action="<?=$_SERVER['PHP_SELF']?>" method="POST">

    </form>
    and:

    PHP Code:
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        
    // script or php?

    I have jquery imported, but still don't know how to do this.

    Thanks!
    Last edited by RonnyNishimoto; 07-25-2012 at 10:46 PM.

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    it can be done without jquery

    PHP Code:


    <form 
    <?PHP if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        echo 
    ' class="class1" ';
    }
    ?>

    action="<?PHP $_SERVER['PHP_SELF'];?>" method="POST">

    </form>

  • Users who have thanked Arcticwarrio for this post:

    RonnyNishimoto (07-25-2012)

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts
    Sorry, I meant a class far away from the form. Anyone know how to do this?

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Well, you can use that same code anywhere on the submission page... or do you mean on an entirely unrelated page? If so, you probably want a session or cookie, depending on how/why you're setting it.

  • Users who have thanked Keleth for this post:

    RonnyNishimoto (07-25-2012)

  • #5
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    You could do it with javascript. Dis iz nawt rokket siense. Just have some php in the javascript near the bottom of the page that echoes an instruction to setAttribute for the element.

  • #6
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts
    Oh, I don't really know JavaScript, I just finished the PHP tutorial and will be moving onto JavaScript maybe in two weeks. Right now I'm trying to build a test website and needed one class change.

    <?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // script or php?
    }
    <div id="test" class="<!-- add class -->">

    </div>
    <div><!--100 lines --></div>
    <div><!--100 lines --></div>
    <div><!--100 lines --></div>
    <form method="POST" action="<?=$_SERVER['PHP_SELF']?>">

    </form>

  • #7
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts
    OHHHHHHHHHH I think I got it thanks Arctic!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •