javascript - 从选择中获取选定值并发布到 MySQL 表

标签 javascript php html

我是 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>

最佳答案

JavascriptPHP 不能简单地相互实时通信。

试试这个

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/

相关文章:

javascript - 为什么 Angular 让你将 $provide 服务注入(inject)到配置 block 中?

php - 如何在 Magento 中翻译产品名称和信息

php - 使用php将表单中的数据插入mysql数据库

java - 从 Java 调用网页上的 Javascript

Javascript 函数不会在页面加载时执行

javascript - 无法动态附加 css 属性

javascript - 获取 html 标签文本,转换为数字,递增,然后用结果更新标签文本

javascript - $(element).height() = element.clientHeight?

php - 您能显示哪些项目绑定(bind)到 PDO 语句吗?

php - 在子查询中传递两个参数