简体   繁体   中英

How to take value of an html element to use in php?

I'm trying to use a set of inputs in HTML but I would like to create a PHP onclick function that takes the value of certain elements in HTML when clicked. The problem is I don't think I can use a conventional HTML method="post" form because I'm trying to get the value of something which isn't and I'm trying to get the value of a div .

Specifically, I want to assign a value to each of the squares depending on their color and then use that value to store in a database in PHP. Any help is appreciated.

   <?php
        session_start();
    
        include("database.php"); 
    
        if($_SERVER['REQUEST_METHOD'] == "POST")
        {
            $text_custom = $_POST['text_custom'];
            echo $text_custom;
        }
    
    ?>
    
    <!DOCTYPE html>
    <html>
    
    <head>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
            integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
        <link rel="stylesheet" href="bootstrap customiser.css">
        <link rel="icon" href="favicon.ico" type="image/x-icon">
    </head>
    
    <body>
        <div class="containter">
            <div class="row">
                <div class="col-3 offset-1 d-flex justify-content-end" id="logo">
                    <a href="http://localhost/website/test_1_customiser.php"><img id="logoclick" src="smiding logo.png"
                            alt="logo"></a>
                </div>
    
                <div class="col-7 offset-1" id="navbar">
                    <a href="customizer_bootstrap.php">Customiser</a>
    
    
                    <a href="news.html">News</a>
    
    
                    <a href="about us.html">About us</a>
    
                    <?php
                    if (!empty($_SESSION['logged'])){
                        echo '<a href="account.php">Account</a>';
                    }else{
                        echo '<a href="login.php">Account</a>';
                    }
                    ?>
                </div>
    
            </div>
            <br>
            <br>
            <div class="row">
                <div class="col-md-12 text-center">
                    <h1>Customiser</h1>
                </div>
            </div>
            <div class="row">
                <div class="col-md-4 offset-md-1">
                    <img src="label.png" id="Gin_Label">
                </div>
                <div id="labeltext">
                    text
    
                </div>
                        
                <div class="col-md-4 offset-md-2">
                    <div id="textchanger">
                        <h3>Text Picker</h3>
                        <input type="text" id="textpicker" name="text_custom">
                        <input type="button" id="update" value="Click me!" onclick="changetext()">
    
    
                    </div>
                    <div id="colourchanger" class="row"></div>
                    <h3>Colour Picker</h3>
                    <div class="row">
    
    
                        <div class="col-md-1">
                            <div class="square" id="colourpicker" onClick="invert()"></div>
                        </div>
                        <div class="col-md-1 offset-md-1">
                            <div class="square2" id="colourpicker2" onClick="invert2()"></div>
                        </div>
                    </div>
                    <br>
                    <br>
                    <div class="row">
                        <div class="col-7">
                            <h3>Extra Ingredient</h3>
                            <select name="ingredient">
                                <option value="none">None</option>
                                <option value="lemon">Lemon</option>
                                <option value="orange">Orange</option>
                            </select>
                        </div>
                    </div>
                    <br>
                    <div class="row">
                        <div class="col-5">
                            <form method="post">
                                <button type="submit">Submit</button>
                            </form>
                        </div>
                    </div>
    
    
                </div>
            </div>
        </div>
    
    
    
        </div>
    
    
        </div>
        <script>
            function changetext() {
                let bruh = document.getElementById('textpicker').value;
                document.getElementById('labeltext').innerHTML = bruh;
            }
    
            function invert() {
                document.getElementById("Gin_Label").style.filter = "invert(100%)";
                document.getElementById("labeltext").style.color = "white";
            }
    
            function invert2() {
                document.getElementById("Gin_Label").style.filter = "invert(0%)";
                document.getElementById("labeltext").style.color = "black";
            }
        </script>
    </body>
    
    </html>

although I am not very clear about your question but to use HTML value in PHP you need to put all the HTML tag that consists of the value you wish to save to the database inside a <form action="yourPhpNameWithTheRelatedCode" method="POST"> tag the submit button <button type="submit" name="submit">Submit</button> then in PHP

if (isset($_POST["submit"])){ 
   $value= $_POST["theNameOfTheTagThatYouWishToSaveThatValue"]??"";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM