我对PHP HTML相当陌生,并在此站点上找到了一些帮助,但是无法将代码作为脚本的一部分工作

单击“提交”按钮后,记录将发布到数据库表(事务),但“字段”(代码)为空,它应包含3个字母的大写字符串,例如美国广播公司

ALERT显示该值存储在变量$ code中,但是一个空字符串被发布到表中

这位新手非常感谢您的帮助

<?php
// Include config file
require_once 'config.php';

// Define variables and initialize with empty values
$code =  "";
$code_err = "";

// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST")
{
//  Validate Code
$code = strtoupper($code);



// Check input errors before inserting in database
if(empty($code_err)){
    // Prepare an insert statement
    $sql = "INSERT INTO Transactions (Code)
        VALUES (?)";

    if($stmt = mysqli_prepare($link, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "s", $param_code);

        // Set parameters
        $param_code = $code;

        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            // Records created successfully. Redirect to landing page
            $url = 'http://localhost:8888/portfolio/index.php';
            echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
            exit();
        } else{
            echo "Something went wrong. Please try again later.";
        }
    }

    // Close statement
    mysqli_stmt_close($stmt);
}

}

// Close connection
mysqli_close($link);


?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Create Record</title>
    <link rel="stylesheet"      href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
    <link href="style1.css" rel="stylesheet" type="text/css"/>
    <style type="text/css">
        .wrapper{
            width: 450px;
            margin: 0 auto;
        }
    </style>
    <script>
        function getValue(obj){
            alert(obj.value);
            $code=(obj.value);
            **alert($code);**
        }
    </script>

    <!--Display heading at top center of screen-->
    <div>
        <center><h3>Peter's Portfolio - Shares</h3></center>
    </div>  <!-- end of Div -->
</head>

<body style="background-color:#fcf8d9">
     <div class="wrapper">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-6">
                    <div class="page-header">
                        <h2>Create Transaction Record</h2>
                    </div>

                    <form class="form-horizontal" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                        <!-- ASX CODE   -->
                        <div class="form-group">
                            <label for="name" class="control-label col-xs-6">ASX Code:</label>
                            <div class="col-xs-6">
                                <?php
                                    $conn = new mysqli('localhost', 'root', 'root', 'Portfolio') or die ('Cannot connect to db');
                                    $result = $conn->query("SELECT Code, Coy_Nm from Companies ORDER BY Code");
                                    echo "<html>";
                                    echo "<body>";
                                    echo "<select Name='Code' ID='Code'onchange='getValue(this)'>";
                                        while ($row = $result->fetch_assoc()) {
                                            unset($Code, $Coy_Nm);
                                            $Code = $row['Code'];
                                            $Coy_Nm = $row['Coy_Nm'];
                                            echo '<option value="'.$Code.'">'.$Coy_Nm.'</option>';
                                        }
                                    echo "</select>";
                                    echo "<input type='hidden' value='submit'>";
                                    //$code=$_POST['Code'];
                                    //echo $code;
                                ?>



                            </div>
                        </div>

                        <!--Submit and Cancel Buttons-->
                        <input type="submit" class="btn btn-primary" value="Submit">
                        <a href="index.php" class="btn btn-default">Cancel</a>
                    </form>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

最佳答案

Javascript和PHP不能简单地彼此实时通信。

尝试这个

function getValue(obj){
        alert(obj);// check if obj has something in it before proceeding to "value"
        alert(obj.value);
}

关于javascript - 从选择中获取选定的值并发布到MySQL表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48416513/

10-10 08:38