简体   繁体   中英

Store name and value of a Button in a mysql table row

I've been hanging on this problem for 3 days and just can't solve it. It seems relatively simple, but unfortunately, I can't do it, because I'm a noob.

I would like to use the button's onclick function to get its 2 pieces of information

```html
<input id="'.$row['id'].'" type="button" value="Pause" name="'.$row['name'].'" onclick="pushDataToDB()">```
  1. name = "'. $ row [' name '].'" [the name reference from a database table]
  2. and value="Pause"

this two informations i would like to store in some other database table. I have tried to solve this problem in different ways.

I have tried to store in different ways the event.target.name & event.target.event javascript output in PHP Variables and then use it in the mysql insert line but i failed.

I have tried to post the value to the ajax.php then store the value in a PHP variable and use this as Value to push it to the database, but this also doesn't work

index.php
    <?php 
    
    include_once ('dbh.php');
    
    
    if(isset($_POST['name'])){
    $sqlAufPause = "INSERT INTO aktuellaufpause (name, pausenart) VALUES ('$name', 'BP')";
        
    }
    
    
    if ($conn->query($sqlAufPause) === TRUE) {
      echo "New record created successfully";
    } else {
      echo "Error: " . $sql . "<br>" . $conn->error;
    }
    
    
    
    ?>
    
script.js:
 function pushDataToDB() { $.get("ajax.php"); return false; }
ajax.php
 <?php include_once ('dbh.php'); if(isset($_POST['name'])){ $sqlAufPause = "INSERT INTO aktuellaufpause (name, pausenart) VALUES ('$name', 'BP')"; } if ($conn->query($sqlAufPause) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } ?>

I would say this is the reason, why the name Value is empty, but i don't know how to fix ist..

emptyNameValue

You can use JQuery + AJAX to perform asynchronous POST

<?php
$con = mysqli_connect("localhost", "root", "", "testdb");
$query = mysqli_query($con, "SELECT * FROM mytable WHERE id = 1");
$row = mysqli_fetch_assoc($query);
print_r($row);
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>

<body>
    <div class="button2">
        <form id="frm">
            <input id="<?php echo $row['id'] ?>" type="button" value="Pause" name="<?php echo $row['name'] ?>" onclick="pushDataToDB()">
        </Form>
    </div>
</body>
<script type="text/javascript">
    function pushDataToDB() {
        var name = "<?php echo $row['name'] ?>"; 
        var value = "Pause";
        $.ajax({
            url: "ajax.php",
            type: "POST",
            data: {
                "name": name,
                "value": value
            },
            success: function(e) {
                if (e == "1") {
                    alert("Success!");
                } else {
                    alert("error!");
                }
            }
        });
    }
</script>

</html>

ajax.php

<?php

$con = mysqli_connect("localhost", "root", "", "testdb");
$name = $_POST['name'];
$value = $_POST['value'];
$query = "INSERT INTO mytable(name, value) values('$name','$value')";

$result = mysqli_query($con, $query);

if ($result) {
    echo "1";
} else {
    echo "Error!";
}

EDITTTT****

I changed

var name = "<?php echo $row['id'] ?>"; 

to

var name = "<?php echo $row['name'] ?>"; 

since it is the "name" you want stored in the database

I solved the problem this way. Thank you so much!!!

index.php

 <?php
            $sql = "SELECT * FROM `DB-TABLENAME` ORDER BY `VALUE1`, `VALUE2`";
            $result = mysqli_query($conn, $sql);

            if (mysqli_num_rows($result) > 0) {
                while ($row = mysqli_fetch_assoc($result)){
                                echo "<tr>";
                    echo "<td>";
                    echo '<input style="margin: 0 auto 6px 17px;" type="checkbox" id="scales" name="scales">';
                    echo "</td>";
                    echo "<td>";
                    echo $row['VALUE1'];
                    echo "</td>";
                    echo "<td>";
                    echo $row['VALUE2'];
                    echo "</td>";
                    echo "<td>";
                    echo '<div class="button1">
                    <input id="'.$row['id'].'" type="button" value="BP" name="'.$row['VALUE2'].'" onclick="pushDataToDB()">
                    </div>';    
                    echo "</td>";
                    echo "<td>";
                    echo '<div class="button2">
                     <form id="frm">
                    <input id="'.$row['id'].'" type="button" value="Mittagspause" name="'.$row['name'].'" onclick="pushDataToDB()">
                    </form>
                    </div>';    
                    echo "</td>";
                    echo "</tr>";
                    }
            } else {
                echo "there are no comments";
                

            }?>

<script type="text/javascript">
    function pushDataToDB() {
        var name = event.target.name; 
        var value = event.target.value;
        $.ajax({
            url: "ajax.php",
            type: "POST",
            data: {
                "name": name,
                "value": value
            },
             success: function(e) {
                 if (e == "1") {
                     alert("Success!");
                 } else {
                     alert("error!");
                 }
             }
        });
    }
</script>

ajax.php

<?php 

include_once('dbconnectioncredentials.php');

$con = mysqli_connect($servername, $username, $password, $dbname);

$name = $_POST['VALUE1'];
$value = $_POST['VALUE2'];
$query = "INSERT INTO aktuellaufpause (VALUE1, VALUE2) VALUES ('$name','$value')";

$result = mysqli_query($con, $query);

 if ($result) {
     echo "1";
 } else {
    echo "Error!";
}

?>

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